Doctrine n:m relation未定义的索引:BasicEntityPersister.php中的joinColumns

时间:2014-09-26 08:08:56

标签: symfony doctrine entity definition bidirectional

我的问题与此Undefined index: inverseJoinColumns while trying to define ManyToMany relationship between two entities

密切相关

我试图找到所有没有部分的书。 - > findBy(阵列("节" = GT;空)

这是我的实体配置: 的 EditedBooks

    /**
 * @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.

解决方法: 今天我试着用我编辑的BooksRepository

中的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 ongoing9,e0_.pages AS pages10,e0_.illustrations AS   illustrations11,e0_.entry_date AS entry_date12,e0_.google_id AS   google_id13,e0_.specialIssue_comment AS specialIssue_comment14,   e0_.deleted AS deleted15,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   FROM edited_books e0_ LEFT JOIN edited_book_has_section e2_ ON e0_.id   = e2_.editedbooks_id LEFT JOIN部分s1_ ON s1_.id = e2_.sections_id WHERE sclr17 = 0':

     

SQLSTATE [42S22]:未找到列:1054未知列' sclr17 '在   ' where 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);
        }
    }

使用脏修复它现在正在工作,但我很高兴任何其他解决方案。

2 个答案:

答案 0 :(得分:2)

这是一个工作示例

    class Audio
    {
      /**
      * @ORM\ManyToMany(targetEntity="Acme\MyBundle\Entity\Groupe",inversedBy="audios")
      */
      private $groupes;

  class Groupe
  {

    /**
    * @ORM\ManyToMany(targetEntity="Acme\MyBundle\Entity\Audio",mappedBy="groupes")
    */
    private $audios;

注意mappedby和inversedby以及属性如何具有" s"在末尾。 (Groupe => private $ groupe s )。也许它会帮助你

答案 1 :(得分:0)

你能不能用null检查?

$query = $qb->select('books')
    ->from('Bundle:EditedBooks', 'books')
    ->leftJoin('books.section', 'sec')
    ->where('sec IS NULL');