Symfony: how would you reverse the “notnull:true”

2019-08-26 19:31发布

sfGuardUser model of sfDoctrineGuardPlugin is defined this way:

sfGuardUser:
  actAs: [Timestampable]
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    username:
      type: string(128)
      notnull: true
      unique: true

As you can see 'username' has the feature "notnull:true". Now i want to create a register form that is not using 'username' but the email address of the user.

When a user wants to register, it is showed this:

Validation failed in class sfGuardUser 1 field had validation error: * 1 validator failed on username (notnull)

Any idea?

Javi

4条回答
可以哭但决不认输i
2楼-- · 2019-08-26 19:41

In symfony 1.4 final classes are generated to main lib directory so you could modify them in project.

Therefore in lib/model/doctrine/sfDoctrineGuardPlugin/sfGuardUser.class.php file you are able to overwrite setUp() method which is responsible for model definition:

class sfGuardUser extends PluginsfGuardUser
{
  public function setUp()
  {
    parent::setUp();

    $this->hasColumn('username', 'string', 128, array(
      'type' => 'string',
      'notnull' => false,
      'unique' => true,
      'length' => 128,
    ));  
  }
}
查看更多
Emotional °昔
3楼-- · 2019-08-26 19:48

Some of the plugin schema fields are easily adjustable without code tweaks, while others will throw an error.

You could try removing "notnull: true" from the username field in the schema, but it may throw an error (haven't tried). In this case, you can either tweak the plugin code (=headache), or ensure that a value is always saved to that field, such as a zero. This way it's never null.

查看更多
看我几分像从前
4楼-- · 2019-08-26 19:55

I finally overwrite the schema with another schema that says "notnull:false". This is possible from sf1.3.

查看更多
成全新的幸福
5楼-- · 2019-08-26 19:55

you could fix lib/form/doctrine/sfGuardPlugin/sfGuardUserForm.class.php in following way:

class sfGuardUserForm extends PluginsfGuardUserForm
{
  protected function doUpdateObject($values)
  {
    $this->getObject()->set('username', $values['email']);

    parent::doUpdateObject($values);
  }
}
查看更多
登录 后发表回答