Zend framework2 ZfcUser-如何修改用户对象
我在我的模块(module.php)中修改了ZfcUser注册表表单,其中包含两个新字段“firstname”和“lastname”,如下所示:Zend framework2 ZfcUser-如何修改用户对象,zend-framework2,zfcuser,Zend Framework2,Zfcuser,我在我的模块(module.php)中修改了ZfcUser注册表表单,其中包含两个新字段“firstname”和“lastname”,如下所示: public function onBootstrap($e) { $events = $e->getApplication()->getEventManager()->getSharedManager(); $zfcServiceEvents = $e->getApplication()-&
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());
这意味着这个属性不存在。
你能解释一下我做错了什么吗?好的,解决它。在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\Entity\User:
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;
}
}
本教程的所有步骤都完成了,可以解决它了。在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\Entity\User:
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;
}
}
完成本教程的所有步骤很好。配置
'enable\u default\u entities'=>false
非常重要;它跳过默认用户表的生成。好。配置'enable\u default\u entities'=>false
非常重要;它跳过默认用户表的生成。