2013-06-12 1 views
0

내 등록 양식을 사용하여 sfDoctrineGuard 테이블에 새 사용자를 추가하려고합니다. 이것은 내가 만든 구성 기능입니다 :로그인 한 사용자와 동일한 그룹으로 새 사용자 생성

public function configure() { 
    // Remove all widgets we don't want to show 
    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->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')); 

    // Setup proper password validation with confirmation 
    $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.'))); 
} 

가 지금은 사용자가 로그인하는 것과 같은 그룹 (들)과 그 새 사용자를 작성해야하지만 방법을 몰라. 나는 this post을 읽었지 만, getGroups()을 사용하면 기본값 인 groups_list을 설정한다는 의미일까요? 이 작업을 수행하는 가장 좋은 방법은 무엇입니까?

답변

2

이 작업을 수행 할 수있는 여러 가지 방법이 있습니다 ... 다른 필드에 대한 유효성 검사가 수행되고 사용자 개체가 저장되면 그룹 추가를 권합니다. 그래서 당신은 양식의 save() 기능을 재정의 할 수 있습니다 거기에 추가 :

<?php 

class YourUserForm extends PluginsfGuardUserForm 
{ 
    /** 
    * A class variable to store the current user 
    * @var sfGuardUser 
    */ 
    protected $current_user; 

    public function configure() 
    { 
    // Remove all widgets we don't want to show 
    unset(
     $this['is_super_admin'], 
     $this['updated_at'], 
     $this['groups_list'], 
     $this['permissions_list'], 
     $this['last_login'], 
     $this['created_at'], 
     $this['salt'], 
     $this['algorithm'] 
    ); 

    // save the currrent user for use later in the save function 
    $this->current_user = sfContext::getInstance()->getUser()->getGuardUser(); 

    $id_empresa = $this->current_user->getSfGuardUserProfile()->getIdempresa();; 
    $this->setDefault('idempresa', $id_empresa); 

    $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')); 

    // Setup proper password validation with confirmation 
    $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) 
    { 
    // call the parent function to perform the save and get the new user object 
    $new_user = parent::save($con); /* @var $user sfGuardUser */ 

    // add our groups here by looping the current user's group collection 
    foreach($this->current_user->getGroups() as $group) /* @var $group sfGuardGroup */ 
    { 
     // we could use $user->addGroupByName($group->name); here, but it would mean 
     // an extra db lookup for each group as it validates the name. we know the 
     // group must exist, so can just add directly 

     // create and save new user group 
     $ug = new sfGuardUserGroup(); 
     $ug->setsfGuardUser($new_user); 
     $ug->setsfGuardGroup($group); 

     $ug->save($con); 
    } 

    // make sure we return the user object 
    return $new_user; 
    } 
} 

이 필요하지 않습니다, 나는 현재 사용자를 저장하는 클래스 변수를 설정할 볼 수 있지만이 계속 전화하는 것을 저장 sfContext::getInstance()->getUser()->getGuardUser()이며 양식이 복잡 해지면 좋은 습관입니다.

희망적으로 도움이됩니다. :)

+0

좋은 대답 +1 이제 모든 게시물을 메인 포스트로 저장하는 대신, 그룹에 "그룹 1"이라고 말하면서 저장할 수 있습니다. 방법? – Reynier

+0

감사합니다. @Reynier! 그룹 이름을 안다면'foreach' 루프를'$ new_user-> addGroupByName ('당신의 그룹 이름');' – moote

+0

우수로 대체하십시오. 한가지 마지막 질문 :'sfGuardUserProfile' 테이블과의 관계가 있습니다. 새로운 생성 된 사용자를 위해'$ id_empresa'를 설정하는 것을 좋아합니다. 어떻게? 'save()'메소드에서'BasesfGuardUser' 클래스에있는'setSfGuardUserProfile()'메소드에 접근 할 수 있습니까? – Reynier

관련 문제