Doctrine Entity findBy Many to Many

时间秒杀一切 提交于 2019-12-06 16:02:15

I know this is an old question, but if anyone else arrives here via Google (like I did), I had to eschew the findBy and use DQL in the repository:

$products = $em->getRepository('Vendor\Bundle\Entity\Product')->findByColours($colours);

And in the repository:

public function findByColours($colours)
{
    $qb = $this->getEntityManager()->createQueryBuilder();
    $qb ->select(array('p'))
        ->from('VendorBundle:Product', 'p')
        ->join('p.colours', 'c', 'WITH', $qb->expr()->in('c.id', $colours));
    $result = $qb->getQuery()->execute();
    return $result;

}

You may need to change the join based on what $colours is. This is assuming it's an array of colour IDs. If it's a string you can forgo the in() or if it's an array of strings you'll need to bind the strings as parameters (see the following link). Clarification on expr() and such is in the Doctrine docs

I don't know why Undefined index: joinColumns occurs, but this is a method to side-step it altogether. Hopefully someone can clarify as to the error, as my solution adds extra work to the Many to Many relationship.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!