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

℡╲_俬逩灬. 提交于 2019-12-24 21:26:11

问题


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


回答1:


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




回答2:


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



回答3:


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:


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


来源:https://stackoverflow.com/questions/2561389/symfony-how-would-you-reverse-the-notnulltrue-in-a-schema-of-a-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!