Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined index: joinColumns doctrine + symfony2

I have the following structure of Entities in my project.

class MyEntity
{
 [... some more fields...]
 /**
 * @Type("array<string, string>")
 * @ORM\ManyToMany(targetEntity="Me\MyBundle\Entity\Example")
 * @ORM\JoinTable(name="example_myentity",
 *      joinColumns={@ORM\JoinColumn(name="plan_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="example", referencedColumnName="label")}
 * )
 */
private $example;
}



class Example
{

/**
 * @ORM\Id
 * @ORM\Column(name="label", type="string", length=50, unique=true, nullable=false)
 */
private $label;
}

When i tried to get "$example" using the findby() function from Doctrine i got the following notice:

Undefined index: joinColumns

I tried to debug it, and the problem seems to be in the doctrine file BasicEntityPersister.php in the function

 _getSelectEntitiesSQL($criteria, $assoc = null, $lockMode = 0, $limit = null, $offset = null, array $orderBy = null),

I observed in the stack trace that the second parameter "$assoc" is always null, and I think it's why Doctrine doesn't make the JOIN statement.

Any idea?

Thanks

like image 424
user2528085 Avatar asked Oct 03 '22 04:10

user2528085


1 Answers

I believe this is a bug, possibly this one.

I have the same problem, and the only solution for now seems to be to wait for an update for the BasicEntityPersister or write your query which is pretty straight forward.

First add a repository to your class

/**
 * MyClass
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Acme\DemoBundle\Entity\MyClassRepository")
 */
class MyClass
...

Then in your repositoryclass:

class MyClassRepository extends EntityRepository
{
    public function findByExample( Example $example )
    {
         return $this->getEntityManager()
                ->createQueryBuilder()
                ->select('m')
                ->from('AcmeDemoBundle:MyClass', 'm')
                ->innerJoin('m.examples', 'e')
                ->where('e.id = :exampleid' )
                ->setParameter('exampleid', $example->getId() )
                ->getQuery()
                ->getResult();
    }
}

Then finally you can get all the MyClass instances related to an Example by:

$this->getDoctrine()->getManager()->getRepository('AcmeDemoBundle:MyClass')->findByExample( $example );
like image 119
Frank van Luijn Avatar answered Oct 07 '22 22:10

Frank van Luijn