我正在使用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
的正确方法是什么?
答案 0 :(得分:1)
您必须再次保存$new_user
对象:$new_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;
}
...