【发布时间】: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