2013-07-04 39 views
1

我已經修改ZfcUser我模塊(Module.php)兩個新領域的「名字」和「姓氏」像這樣在註冊表單的用戶對象:ZfcUser - 如何修改

public function onBootstrap($e) 
{ 
    $events   = $e->getApplication()->getEventManager()->getSharedManager(); 
    $zfcServiceEvents = $e->getApplication()->getServiceManager()->get('zfcuser_user_service')->getEventManager(); 

    $events->attach('ZfcUser\Form\Register', 'init', function($e) { 
     $form = $e->getTarget(); 
     $form->add(array(
       'name' => 'firstname', 
       'type' => 'Text', 
       'options' => array(
         'label' => 'First name: ', 
       ), 
     )); 
     $form->add(array(
       'name' => 'lastname', 
       'type' => 'Text', 
       'options' => array(
         'label' => 'Last name: ', 
       ), 
     )); 
    }); 

    $events->attach('ZfcUser\Form\RegisterFilter', 'init', function($e) { 
     $filter = $e->getTarget(); 
     $filter->add(array(
       'name'  => 'firstname', 
       'required' => true, 
       'validators' => array(
         array(
           'name' => 'StringLength', 
           'options' => array(
             'min' => 3, 
             'max' => 255, 
           ), 
         ), 
       ), 
     )); 
     $filter->add(array(
       'name'  => 'lastname', 
       'required' => true, 
       'validators' => array(
         array(
           'name' => 'StringLength', 
           'options' => array(
             'min' => 3, 
             'max' => 255, 
           ), 
         ), 
       ), 
     )); 
    }); 

    $zfcServiceEvents->attach('register', function($e) { 
     $user = $e->getParam('user'); // User account object 
     $form = $e->getParam('form'); // Form object 
     //var_dump($form->get('firstname')->getValue()); die; 
     //var_dump($user); die; 
    }); 

    $zfcServiceEvents->attach('register.post', function($e) { 
     $user = $e->getParam('user'); 
    }); 
} 

登記表看起來確定。我可以看到其他字段和驗證工作正常。 問題是,我不能在用戶對象設置這兩個新領域:

$user->setFirstname($form->get('firstname')->getValue()); 

它說這個屬性不存在。 你能解釋我做錯了嗎?

回答

4

好的是解決它。在我module.config.php我不得不補充一點:

'doctrine' => array(
      'driver' => array(
        // overriding zfc-user-doctrine-orm's config 
        'zfcuser_entity' => array(
          'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver', 
          'paths' => __DIR__ . '/../src/User/Entity', 
        ), 

        'orm_default' => array(
          'drivers' => array(
            'User\Entity' => 'zfcuser_entity', 
          ), 
        ), 
      ), 
    ), 

    'zfcuser' => array(
      // telling ZfcUser to use our own class 
      'user_entity_class'  => 'User\Entity\User', 
      // telling ZfcUserDoctrineORM to skip the entities it defines 
      'enable_default_entities' => false, 
    ), 

然後用我的延長ZfcUser \實體\用戶:

namespace User\Entity; 
use ZfcUser\Entity\User as ZfcUser; 
class User extends ZfcUser 
{ 
    /** 
    * @var string 
    */ 
    protected $firstname; 

    /** 
    * @var string 
    */ 
    protected $lastname; 

    /** 
    * Get firstname. 
    * 
    * @return string 
    */ 
    public function getFirstname() 
    { 
     return $this->firstname; 
    } 

    /** 
    * Set firstname. 
    * 
    * @param string $firstname 
    * @return UserInterface 
    */ 
    public function setFirstname($firstname) 
    { 
     $this->firstname = $firstname; 
     return $this; 
    } 

    /** 
    * Get lastname. 
    * 
    * @return string 
    */ 
    public function getLastname() 
    { 
     return $this->lastname; 
    } 

    /** 
    * Set lastname. 
    * 
    * @param string $lastname 
    * @return UserInterface 
    */ 
    public function setLastname($lastname) 
    { 
     $this->lastname = $lastname; 
     return $this; 
    } 
} 

全部完成THX本教程http://circlical.com/blog/2013/4/1/l5wftnf3p7oks5561bohmb9vkpasp6

+0

好。配置''enable_default_entities'=> false'非常重要;它跳過了默認用戶表的生成。 – kabirbaidhya