防止 Propel 插入空字符串 [英] Prevent Propel from inserting empty strings

查看:51
本文介绍了防止 Propel 插入空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当未设置列时,如何防止 Propel ORM 插入空字符串?

How can I prevent Propel ORM from inserting empty strings when a column is not set?

CREATE TABLE user (  
  uid INTEGER PRIMARY KEY AUTO_INCREMENT,  
  email VARCHAR(255) NOT NULL UNIQUE,  -- No default value
  ...  
) Engine InnoDB ... ;  

Propel 允许 $user = new User();$user->save();.我试过设置 SQL_MODE 但它没有帮助.

Propel allows $user = new User(); $user->save();. I have tried setting SQL_MODE but it doesn't help.

推荐答案

正确的方法是在架构中使用验证器,然后在代码中使用 validate() 方法进行检查.举个例子:

The correct way to do this is with a validator in the schema and then a check using the validate() method in your code. Here's an example:

<database ...>
  <table ...>
    <!-- the "required" attribute here only sets the DB property -->
    <column name="email" type="varchar" required="true" />
    ...
    <!-- Adds the unique index in the DB (but nothing in PHP code!) -->
    <unique>
      <unique-column name="email" />
    </Unique>
    ...
    <validator column="email">
      <!-- this validator rule makes the $obj->validate() method fail on null -->
      <rule name="required" message="The email is required!" />
      <!-- this validator rule makes the $obj->validate() method fail on empty string -->
      <rule name="minLength" value="1" message="The email cannot be blank!" />
      <!-- you could add a regular expression to only match email addresses here -->
      <rule name="match" value="/regular expression/" message="Please enter a valid email address!" />
      <!-- adds a validation that the field is unique before trying to update DB -->
      <rule name="unique" message="That email address is not unique!" />
    </validator>
  </table>
</database>

然后在您的 preSave() 代码中,您可以执行以下操作:

Then in your preSave() code you could do something like this:

class User extends BaseUser {
  ...
  public function preSave(PropelPDO $con = null) {
    // does the object pass all validations?
    if (!$this->validate()) {
      $errors = array();
      // something failed, go through each failure and capture message:
      foreach ($this->getValidationFailures() as $failure) {
        $errors[] = $failure->getMessage();
      }
      // throwing an Exception will stop the save() from occurring
      throw new InvalidArgumentException(implode("||", $errors));
    }

    return true; // if you get here, go ahead and save
  }
}

在您的脚本中,您可以像这样调用 save():

In your script you would call save() like so:

...
$user = new User();
try {
  // try to save (could fail)
  $user->save();

} catch (InvalidArgumentException $e) {
  // we have errors, split the exception message to get each one separately
  $errorMessages = preg_split(/\|\|/, $e->getMessage());
  // handle the messages however you need to
}

阅读有关 Propel 文档中的验证器的更多信息.

Read more about Validators in the Propel documentation.

这篇关于防止 Propel 插入空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆