0

我正在开发一个使用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我的问题是:设置默认值以便在创建用户时设置配置文件的正确方法应该是什么?

4

1 回答 1

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;
    }
    ...
于 2013-06-19T06:56:56.630 回答