开发者

Symfony: how would you reverse the "notnull:true" in a schema of a plugin?

开发者 https://www.devze.com 2022-12-25 15:08 出处:网络
sfGuardUser model of sfDoctrineGuardPlugin is defined this way: sfGuardUser: actAs: [Timestampable] columns:

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 validati开发者_JS百科on error: * 1 validator failed on username (notnull)

Any idea?

Javi


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


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,
    ));  
  }
}


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.


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);
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消