繁体   English   中英

Doctrine2级联实体创建

[英]Doctrine2 cascade entity creation

我有两个实体User和UserProfile。 UserProfile primaryKey值不是AUTO_INCREMENT,它是使用User的主键值的一对一关系。 创建新用户时出现错误:

类型为App \\ PublicBundle \\ Entity \\ User \\ UserProfile的实体缺少为“用户”字段分配的ID。 该实体的标识符生成策略要求在调用EntityManager#persist()之前填充ID字段。 如果要自动生成的标识符,则需要相应地调整元数据映射。

问题是如何保存具有下一个数据库和实体结构的实体:

用户DDL:

CREATE TABLE `user` (
  `intUserID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`intUserID`),
)
CREATE TABLE `user_profile` (
  `intUserID` int(10) unsigned NOT NULL,
  PRIMARY KEY (`intUserID`),
  CONSTRAINT `user_profile_ibfk_1` FOREIGN KEY (`intUserID`) REFERENCES `user` (`intUserID`) ON DELETE CASCADE ON UPDATE CASCADE
)

用户实体:

/**
 * @ORM\Table(name="user")
 * @ORM\Entity
 */
class User
{
    /**
     * @var integer
     *
     * @ORM\Id
     * @ORM\Column(name="intUserID", type="integer", nullable=false)
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $intuserid;

    /**
     * @var \App\PublicBundle\Entity\User\UserProfile
     *
     * @ORM\OneToOne(targetEntity="\App\PublicBundle\Entity\User\UserProfile",
     *      mappedBy="user",
     *      cascade={"persist", "remove"}
     * )
     */
    protected $profile;
}

UserProfile实体:

/**
 * @ORM\Table(name="user_profile")
 * @ORM\Entity
 */
class UserProfile
{
    /**
     * @var \App\PublicBundle\Entity\User\User
     *
     * @ORM\Id
     * @ORM\OneToOne(targetEntity="\App\PublicBundle\Entity\User\User",
     *      inversedBy="profile"
     * )
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="intUserID", referencedColumnName="intUserID")
     * })
     */
    protected $user;
}

注册动作

private function registration(Request $request, $tpl)
{
    $user = new User();
    $form = $this->$form($user, 'Create');
    $form->handleRequest($request);

    if ('POST' === $request->getMethod())
    {
        if ($form->isValid())
        {
            $em->getConnection()->beginTransaction();

            try
            {
                $em = $this->getManager();
                $em->persist($user);
                $em->flush();

                $this->sendSuccessEmail($user);
                $em->getConnection()->commit();
            }
            catch (\Exception $e)
            {
                $em->getConnection()->rollback();
                throw $e;
            }

            return $this->redirect($this->generateUrl('app_profile'));
        }
    }

    return $this->render($tpl, array(
        'entity' => $user,
        'form' => $form->createView()
    ));
}

正确的方法是,在persist和flush之前将其设置为空配置文件,然后再次调用persist和flush:

注册动作

private function registration(Request $request, $tpl)
{
    $user = new User();
    $form = $this->$form($user, 'Create');
    $form->handleRequest($request);

    if ('POST' === $request->getMethod())
    {
        if ($form->isValid())
        {
            $em->getConnection()->beginTransaction();

            try
            {
                $user->setProfile(null);

                $em = $this->getManager();
                $em->persist($user);
                $em->flush();

                $user->setProfile(new UserProfile);
                $user->getProfile()->setUser($user);

                $em = $this->getManager();
                $em->persist($user);
                $em->flush();

                $this->sendSuccessEmail($user);
                $em->getConnection()->commit();
            }
            catch (\Exception $e)
            {
                $em->getConnection()->rollback();
                throw $e;
            }

            return $this->redirect($this->generateUrl('app_profile'));
        }
    }

    return $this->render($tpl, array(
        'entity' => $user,
        'form' => $form->createView()
    ));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM