简体   繁体   中英

One to many relation ship in symfony 2.7

I have two entities as below

1) entity one

/**
* @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) entity two

 /**
  * @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;
}

and i want to get entity two's record on base of entity one as below ,

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

it's give me erroe like below:

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

can any body help me toget this value without using left join.

Entities are related as OneToMany , so getCallRequestComment returns collection of CallRequestComments entities, not one. You should process them as array in loop:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM