0

我希望能够在用户注册后自动创建用户配置文件。我附加了一个创建用户配置文件对象的后持久化函数,但我无权访问实体管理器来持久化该对象。解决这个问题的最佳方法是什么?

//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();

}
4

1 回答 1

0

解决方案:

/**
 * @ORM\PostPersist()
 */
public function PostPersistAction(LifecycleEventArgs $eventArgs)
{
    $entity = $eventArgs->getEntity();
    $em = $eventArgs->getEntityManager();

    if ($entity instanceof User) {
        //create a user profile when a user is created.
        $userprofile = new Profile();
        $userprofile->setCreatedAt(new \DateTime("now"));
        $userprofile->__set('user', $this);

        $em->persist($userprofile);
        $em->flush();
    }

}
于 2013-11-15T12:47:12.127 回答