Doctrine2多对一双向关系不起作用

编程入门 行业动态 更新时间:2024-10-21 04:05:17
本文介绍了Doctrine2多对一双向关系不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图在两个实体之间进行双向关联。问题是,从书我可以得到他们的所有者,但是从所有者我不能得到所有的书。

这是代码的重要部分: p>

Acme\BookBundle\Entity\Book;

code> / ** * @ ORM\ManyToOne(targetEntity =Acme\UserBundle\Entity\User,inversedBy =owned_books) * @ ORM\JoinColumn (name =owner_id,referencedColumnName =id) * / protected $ owner; / ** *获取所有者 * * @return Acme\UserBundle\Entity\User * / public function getOwner() { return $ this-> owner; }

Acme\UserBundle\Entity\User; strong>

/ ** * @ ORM\OneToMany(targetEntity =Acme\BookBundle\Entity \Book,mappedBy =owner) * / protected $ owned_books; public function __construct() { $ this-> owned_books = new \Doctrine\Common\Collections\ArrayCollection(); } / ** *获得owned_books * * @return Doctrine\Common\Collections\Collection * / public function getOwnedBooks() { return $ this-> owned_books; }

然后,获取数据:

此作品

$ book = $ this-> getDoctrine() - > getRepository('BookBundle:Book') - > find(1); $ owner = $ book-> getOwner() - > getFirstName();

这不工作(提供致命错误:调用未定义的方法Doctrine\ORM\PersistentCollection :: getName())

$ owner = $ this-> getDoctrine() - > getRepository('UserBundle:User') - > find(1); $ books = $ owner-> getOwnedBooks() - > getName();

有谁知道我做错了什么?谢谢你提前。

解决方案

$ owner-> getOwnedBooks()是一个所有者的集合。尝试用foreach循环循环遍历集合。

$ books = $ owner-> getOwnedBooks(); foreach($ books as $ book){ echo $ book-> getName()。 '< br />'; }

I'm trying to do a bidirectional association between 2 entities. The problem is that from Book I can get their Owner, but from Owner I can't get the books owned.

Here is the important part of the code:

Acme\BookBundle\Entity\Book;

/** * @ORM\ManyToOne(targetEntity="Acme\UserBundle\Entity\User", inversedBy="owned_books") * @ORM\JoinColumn(name="owner_id", referencedColumnName="id") */ protected $owner; /** * Get owner * * @return Acme\UserBundle\Entity\User */ public function getOwner() { return $this->owner; }

Acme\UserBundle\Entity\User;

/** * @ORM\OneToMany(targetEntity="Acme\BookBundle\Entity\Book", mappedBy="owner") */ protected $owned_books; public function __construct() { $this->owned_books = new \Doctrine\Common\Collections\ArrayCollection(); } /** * Get owned_books * * @return Doctrine\Common\Collections\Collection */ public function getOwnedBooks() { return $this->owned_books; }

Then, to get the data:

This Works

$book = $this->getDoctrine() ->getRepository('BookBundle:Book') ->find(1); $owner = $book->getOwner()->getFirstName();

This Doesn't work ( Gives Fatal error: Call to undefined method Doctrine\ORM\PersistentCollection::getName() )

$owner = $this->getDoctrine() ->getRepository('UserBundle:User') ->find(1); $books = $owner->getOwnedBooks()->getName();

Does anyone know what I'm doing wrong? Thank you in advance.

解决方案

$owner->getOwnedBooks() is a collection of Owners. Try to loop through the collection with a foreach loop.

$books = $owner->getOwnedBooks(); foreach ($books as $book) { echo $book->getName() . ' <br/>'; }

更多推荐

Doctrine2多对一双向关系不起作用

本文发布于:2023-10-14 22:06:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492351.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:双向   不起作用   关系

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!