MassiveDev
MassiveDev

Reputation: 11

Doctrine n:m relation Undefined index: joinColumns in BasicEntityPersister.php

My question is strongly related to this one Undefined index: inverseJoinColumns while trying to define ManyToMany relationship between two entities

I am trying to find all the books which have no section. ->findBy(array("section"=>null)

Here is my Entity config: 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;

Here is my Entity config: Sections

/**
 * Bidirectional (INVERSE SIDE)
 *
 * @ORM\ManyToMany(targetEntity="EditedBooks", mappedBy="section")
 */       
private $editedBook;

The Error that I am getting

Undefined index: joinColumns in BasicEntityPersister.php

I tryied it also with only

    /**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="Sections", inversedBy="editedBook")
 * @ORM\JoinTable(name="edited_book_has_section")
 */ 

or switching the definition but then I receive this error

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.

Workaround: Today I tried to fix my problem with the querybuilder in my editedBooksRepository

        $query = $qb->select('books')
        ->from('Bundle:EditedBooks', 'books')
        ->leftJoin('books.section', 'sec')
        ->addSelect('COUNT(sec.id) AS sec_count')
        ->andWhere('sec_count = 0');

But I receved the following error:

An exception occurred while executing '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 sections s1_ ON s1_.id = e2_.sections_id WHERE sclr17 = 0':

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sclr17' in 'where clause'

but to me it seems like sclr17 exists, am I missing something?

The ugly Workaround I know it is not the right thing to do but sometimes a man has to to what a man has to do:

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

With the dirty fix it is now working but I am glad for any other solution.

Upvotes: 1

Views: 1597

Answers (2)

Tobias
Tobias

Reputation: 312

Can't you just check with null?

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

Upvotes: 0

oligan
oligan

Reputation: 634

Here is a working example

    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;

Notice how the mappedby and inversedby, as well at the attributes have an "s" at the end. (Groupe => private $groupe s ). Maybe it will help you

Upvotes: 2

Related Questions