【问题标题】:Reference ID not updated with Doctrine association参考 ID 未随 Doctrine 关联更新
【发布时间】:2014-09-29 09:47:26
【问题描述】:

我可能确实对教义关联有所了解。

我有一个头等舱:

  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;
    }
   ....
  }

还有一个Class,就是cmets所链接的元素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->cmets 为空。必须正常填写$comment->id。

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

也许我不懂机制。

注意:SGDB 是 Oracle。

【问题讨论】:

    标签: php doctrine-orm associations


    【解决方案1】:

    尝试像这样持久化 $commentId:

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

    【讨论】:

      【解决方案2】:

      不,我不能先对 $commentId 执行“持久化”,因为对 $comment 类的“持久化”会为 $cmets 的 Id 启动 Oracle 序列。 (我不确定我说的是否很干净......)

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

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

      我知道 Doctrine 在保存记录之前使用序列,在持久命令中。也许我可以在不刷新的情况下进行持久化,然后在 $commentId 记录结束时进行刷新。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-17
        • 2011-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多