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
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 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With