【发布时间】:2013-11-15 03:15:52
【问题描述】:
我希望能够在用户注册后自动创建用户个人资料。我附加了一个创建用户配置文件对象的后持久化函数,但我无权访问实体管理器来持久化该对象。解决这个问题的最佳方法是什么?
//user.php
/**
* @ORM\PostPersist()
*/
public function PostPersistAction()
{
//create a user profile when a user is created.
$userprofile = new Profile();
$userprofile->setCreatedAt(new \DateTime("now"));
$userprofile->__set('user', $this);
$em = $this->getEntityManager(); //doesn't work because getServiceLocator()->get('doctrine..') fails
$em->persist($userprofile);
$em->flush();
}
【问题讨论】:
-
为什么从 ServiceLocator 获取 EntityManager 失败?
标签: zend-framework doctrine-orm doctrine zend-framework2