2013-06-18 60 views
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的正確方法是什麼?

回答

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