参考ID未使用Doctrine关联更新

时间:2014-09-29 09:47:27

标签: php doctrine-orm associations

我可能对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->评论,则关联不是很有用。

也许我不了解机制。

注意:SGDB是Oracle。

2 个答案:

答案 0 :(得分:0)

尝试持久保存$ commentId也是这样的:

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

答案 1 :(得分:0)

不,我不能首先对'commentId'做'持久',因为'持续'到$ comment类启动Oracle序列为$ comments的注释。 (我不确定我说的很干净......)

'commentID-> comments'是指向'comment-> id'的链接,并且不为空。

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

我知道Doctrine在保存记录之前使用Sequence,在persist命令中。也许我可以在没有同花顺的情况下坚持下去,并且在$ commentId录音结束时,做一次同花。