symfony 2.7中的一对多关系

时间:2016-10-07 09:21:07

标签: php symfony

我有两个实体,如下所示

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

我希望在实体一的基础上得到实体二的记录 如下所示,

$resultRow->getCallRequestComment()->getComment()

它给了我如下的错误:

Attempted to call an undefined method named "getComment" of class "Doctrine\ORM\PersistentCollection"

任何正文都可以帮助我在不使用左连接的情况下获取此值。

1 个答案:

答案 0 :(得分:1)

实体与OneToMany相关,因此getCallRequestComment会返回CallRequestComments个实体的集合,而不是一个。您应该将它们作为数组循环处理:

foreach ($resultRow->getCallRequestComment() as $callRequestComment) {
    $comment = $callRequestComment->getComment();
    // do something with $comment
}