[英]One to many relation ship in symfony 2.7
我有两个实体如下
1)实体一
/**
* @ORM\OneToMany(targetEntity="CallRequestComments", mappedBy="CallRequest")
* @ORM\JoinColumn(name="call_request_id", referencedColumnName="id")
*/
protected $CallRequestComment;
/**
* Add CallRequestComment
*
* @param \Veritas\AdminBundle\Entity\CallRequestComments $callRequestComment
* @return CallRequest
*/
public function addCallRequestComment(\Veritas\AdminBundle\Entity\CallRequestComments $callRequestComment)
{
$this->CallRequestComment[] = $callRequestComment;
return $this;
}
/**
* Remove CallRequestComment
*
* @param \Veritas\AdminBundle\Entity\CallRequestComments $callRequestComment
*/
public function removeCallRequestComment(\Veritas\AdminBundle\Entity\CallRequestComments $callRequestComment)
{
$this->CallRequestComment->removeElement($callRequestComment);
}
/**
* Get CallRequestComment
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCallRequestComment()
{
return $this->CallRequestComment;
}
2)实体二
/**
* @var text
*
* @ORM\Column(name="comment", type="text")
*/
protected $comment;
/**
* Set comment
*
* @param string $comment
* @return CallRequestComments
*/
public function setComment($comment)
{
$this->comment = $comment;
return $this;
}
/**
* Get comment
*
* @return string
*/
public function getComment()
{
return $this->comment;
}
我想在实体1的基础上获得实体2的记录,如下所示,
$resultRow->getCallRequestComment()->getComment()
就像下面给我的错误:
Attempted to call an undefined method named "getComment" of class "Doctrine\ORM\PersistentCollection"
任何机构都可以在不使用左联接的情况下帮助我获得此值。
实体与OneToMany
,因此getCallRequestComment
返回CallRequestComments
实体的集合,而不是一个。 您应该将它们作为数组循环处理:
foreach ($resultRow->getCallRequestComment() as $callRequestComment) {
$comment = $callRequestComment->getComment();
// do something with $comment
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.