Symfony1 创建新的用户关系不是';没有保存,什么';怎么了?
我正在开发一个使用Symfony1 创建新的用户关系不是';没有保存,什么';怎么了?,symfony1,symfony-1.4,sfdoctrineguard,Symfony1,Symfony 1.4,Sfdoctrineguard,我正在开发一个使用sfDoctrineGuard插件作为基础的模块(即使用sfDoctrineGuard),因此在我的模块中,我开发了以下代码: class UsuariosForm extends sfGuardUserForm { protected $current_user; public function configure() { unset( $this['is_super_admin'], $this['updat
sfDoctrineGuard
插件作为基础的模块(即使用sfDoctrineGuard),因此在我的模块中,我开发了以下代码:
class UsuariosForm extends sfGuardUserForm {
protected $current_user;
public function configure() {
unset(
$this['is_super_admin'], $this['updated_at'], $this['groups_list'], $this['permissions_list'], $this['last_login'], $this['created_at'], $this['salt'], $this['algorithm']
);
$id_empresa = sfContext::getInstance()->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();
$this->setDefault('idempresa', $id_empresa);
$this->current_user = sfContext::getInstance()->getUser()->getGuardUser();
$this->validatorSchema['idempresa'] = new sfValidatorPass();
$this->widgetSchema['first_name'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
$this->widgetSchema['last_name'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
$this->widgetSchema['username'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
$this->widgetSchema['email_address'] = new sfWidgetFormInputText(array(), array('class' => 'input-block-level'));
$this->widgetSchema['password'] = new sfWidgetFormInputPassword(array(), array('class' => 'input-block-level'));
$this->widgetSchema['password_confirmation'] = new sfWidgetFormInputPassword(array(), array('class' => 'input-block-level'));
$this->validatorSchema['password']->setOption('required', true);
$this->validatorSchema['password_confirmation'] = clone $this->validatorSchema['password'];
$this->widgetSchema->moveField('password_confirmation', 'after', 'password');
$this->mergePostValidator(new sfValidatorSchemaCompare('password', sfValidatorSchemaCompare::EQUAL, 'password_confirmation', array(), array('invalid' => 'The two passwords must be the same.')));
}
public function save($con = null) {
if (sfContext::getInstance()->getActionName() == "create" || sfContext::getInstance()->getActionName() == "new") {
$new_user = parent::save($con); /* @var $user sfGuardUser */
$new_user->addGroupByName('Monitor');
}
return $new_user;
}
}
第一个函数允许我拥有自己的表单,而不是sfDoctrineGuard插件表单,第二个函数是重写
save()
方法,用于向我创建的新用户添加默认组。我还想添加一个默认的idempresa
,您可能会注意到(在config()
函数中),但它不起作用,可能是我做错了什么或不知道idempresa
是存储在sfGuardUserProfile
表中的字段,当然还配置了关系等。我这里的问题是:为了在创建用户时设置配置文件,设置默认的idempresa
的正确方法应该是什么 您必须再次保存$new\u user
对象:$new\u user->save($con)
另外,您不必在save()方法中检查action_name,您可以在对象是否为新对象时进行检查。Objectform为此提供了一个方法
<?php
...
public function save($con = null)
{
$new_user = parent::save($con);
if($this->isNew())
{
$new_user->addGroupByName('Monitor');
$new_user->save($con); //this saves the group
}
return $new_user;
}
...