2014-09-29 23 views
0

有可能是我理解与教义协会。参考编号没有更新与Doctrine协会

我有一个一流的:

class FitComments 
    { 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="ID", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="SEQUENCE") 
    * @ORM\SequenceGenerator(sequenceName="SEQ_FIT_COMMENTS", allocationSize=1, initialValue=1) 
    */ 
    private $id; 
    /** 
    * 
    * @ORM\OneToMany(targetEntity="FitCommentsId",mappedBy="comments",cascade={"persist"}) 
    * 
    */ 
    private $elements; 


    /****/ 
    public function __construct() 
    { 
     $this->elements=new ArrayCollection(); 
    } 
    public function getElements() 
    { 
     return $this->elements; 
    } 
    .... 
    } 

并有另一个类,元素ID列表中的评论链接。

/** 
* FitCommentsId 
* @ORM\Entity 
*/ 
class FitCommentsId 
{ 

    /** 
    * @var integer 
    * 
    * @ORM\Column(name="ID", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="SEQUENCE") 
    * @ORM\SequenceGenerator(sequenceName="SEQ_FIT_COMMENTS_ID", allocationSize=1, initialValue=1) 
    */ 
    private $id; 

    /** 
    * 
    * @ORM\ManyToOne(targetEntity="FitComments",inversedBy="elements") 
    * @ORM\JoinColumn(name="COMMENTS_ID",referencedColumnName="ID") 
    */ 
    private $comments; 

    .... 
} 

我用:

$comments=new FitComment(); 
    $commentId=new FitCommentId(); 
    .... 
    $comments->getElements()->add($commentId); 
    .... 
    $entityManager->persist($comment); 
    $entityManager->flush(); 

但是我有一个错误。 $ commentId->评论为空。它必须正常填写$ comment-> id。

如果我必须手工填写$ commentId-> comments,关联不是很有用。

也许我不明白机制。

注:SGDB是Oracle。

回答

0

尝试坚持的$ commentId也是这样:

$comment = new FitComment(); 
$commentId = new FitCommentId(); 
.... 
$comment->getElements()->add($commentId); 
.... 
$entityManager->persist($commentId); 
$entityManager->persist($comment); 
$entityManager->flush(); 
0

不行,我不能做一个“坚持”到$ commentId第一,因为“坚持”到$评论类发起甲骨文序列的Id $意见。 (我不确定它说的很干净,我说....)

'commentID-> comments'链接到'comment-> id',并且不为空。

我必须先创建$注释,然后创建$ commentId。

我知道Doctrine在保存记录前使用Sequence,在persist命令中。也许我可以一开始就坚持不用刷新,并且在$ commentId录制结束时,做一次冲洗。