【发布时间】:2014-09-26 08:08:56
【问题描述】:
我的问题与Undefined index: inverseJoinColumns while trying to define ManyToMany relationship between two entities这个问题密切相关
我正在尝试查找所有没有章节的书籍。 ->findBy(array("section"=>null)
这是我的实体配置: 编辑书籍
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Sections", inversedBy="editedBook")
* @ORM\JoinTable(name="edited_book_has_section",
* joinColumns={
* @ORM\JoinColumn(name="edited_book_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="section_id", referencedColumnName="id")
* }
* )
*/
private $section;
这是我的实体配置: 部分
/**
* Bidirectional (INVERSE SIDE)
*
* @ORM\ManyToMany(targetEntity="EditedBooks", mappedBy="section")
*/
private $editedBook;
我遇到的错误
未定义索引:BasicEntityPersister.php 中的 joinColumns
我也试过了
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Sections", inversedBy="editedBook")
* @ORM\JoinTable(name="edited_book_has_section")
*/
或切换定义但我收到此错误
You cannot search for the association field '\Entity\EditedBooks#section', because it is the inverse side of an association. Find methods only work on owning side associations.
解决方法: 今天我试图解决我的editedBooksRepository中的querybuilder的问题
$query = $qb->select('books')
->from('Bundle:EditedBooks', 'books')
->leftJoin('books.section', 'sec')
->addSelect('COUNT(sec.id) AS sec_count')
->andWhere('sec_count = 0');
但我收到以下错误:
执行 'SELECT e0_.id AS id0, e0_.doi 时发生异常 AS doi1, e0_.isbn_print AS isbn_print2, e0_.isbn_electronic AS isbn_electronic3, e0_.publication_date AS publication_date4, e0_.price_print AS price_print5, e0_.price_electronic AS price_electronic6, e0_.summary AS summary7, e0_.title AS title8, e0_.ongoing AS 正在进行9, e0_.pages AS pages10, e0_.illustrations AS 插图11,e0_.entry_date AS entry_date12,e0_.google_id AS google_id13, e0_.specialIssue_comment AS specialIssue_comment14, e0_.deleted AS 已删除15, e0_.specialIssue_id AS specialIssue_id16, COUNT(s1_.id) AS sclr17,e0_.book_series_id AS book_series_id18, e0_.copyright_id AS copyright_id19, e0_.publisher_id AS publisher_id20 从edited_books e0_ 左加入edited_book_has_section e2_ ON e0_.id = e2_.editedbooks_id 左连接部分 s1_ ON s1_.id = e2_.sections_id WHERE sclr17 = 0':
SQLSTATE[42S22]:找不到列:1054 未知列 'sclr17' 在 'where子句'
但对我来说,sclr17 似乎存在,我错过了什么吗?
丑陋的解决方法 我知道这样做是不对的,但有时男人不得不做男人必须做的事:
$noSection = new Sections();
$noSection->setTitle("Sectionless");
//add all the books
$noSection->setEditedBook(new ArrayCollection($books));
//remove the assigned ones
foreach($sections as $section){
$sBooks = $section->getEditedBook();
foreach($sBooks as $b){
$noSection->removeEditedBook($b);
}
}
有了肮脏的修复,它现在可以工作了,但我很高兴有任何其他解决方案。
【问题讨论】:
-
找到这个答案了吗?
标签: symfony doctrine entity definition bidirectional