2015-11-22 171 views
2

我有兩個實體User和UserProfile。 UserProfile primaryKey值不是AUTO_INCREMENT,它是使用來自User的主鍵值的一對一關係。當我創建新的用戶我有一個錯誤:Doctrine2級聯實體創建

Entity of type App\PublicBundle\Entity\User\UserProfile is missing an assigned ID for field 'user'. The identifier generation strategy for this entity requires the ID field to be populated before EntityManager#persist() is called. If you want automatically generated identifiers instead you need to adjust the metadata mapping accordingly.

問題是我如何能夠拯救實體下一個數據庫和實體的結構:

用戶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; 
} 

用戶配置實體:

/** 
* @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() 
    )); 
} 
+0

試圖保持兩個主鍵同步通常比它的價值更麻煩。特別是在使用諸如Doctrine 2之類的對象關係管理器時。只需要按照流程進行操作,併爲UserProfile指定它自己的數據庫ID。或者使用嵌入式值對象。 – Cerad

+0

是的,我想過這個,但我想另一種更正確的方式是存在的 –

回答

0

正確的方式,它的設置前空型材堅持和沖洗,並呼籲堅持,並再次刷新:

註冊行爲

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