Doctrine Query Builder Bug?

泪湿孤枕 提交于 2019-12-11 19:51:23

问题


I have run into an issue in Doctrine. I have built the following query with queryBuilder

    $qb = $query = $this->repoLibrary->createQueryBuilder('l');

    $query = $qb
            ->innerJoin('l.productVariant', 'v')
            ->innerJoin('v.product', 'p')
            ->innerJoin('p.taxons', 't', 'WITH', 't.id IN (:array)')
            ->where('l.user = :user')
            ->groupBy('l.id HAVING count(DISTINCT t.id) >= :count')
            ->setParameter('user', $user)
            ->setParameter('array', $s)
            ->setParameter('count', count($taxons))
            ->getQuery();

Here is the query that is logged prior to execution:

  SELECT s0_.id AS id0, s0_.consumed_at AS consumed_at1, s0_.created_at AS created_at2, s0_.updated_at AS updated_at3, s0_.user_id AS user_id4, s0_.variant_id AS variant_id5 
  FROM src_library s0_ 
  INNER JOIN src_variant s1_ ON s0_.variant_id = s1_.id 
  INNER JOIN src_product s2_ ON s1_.product_id = s2_.id 
  INNER JOIN src_taxon_product s4_ 
  ON s2_.id = s4_.product_id 
  INNER JOIN src_taxon s3_ ON s3_.id = s4_.taxon_id
  AND (s3_.id IN (1,4)) 
  WHERE s0_.user_id = 1 
  GROUP BY s0_.id HAVING count(DISTINCT s3_.id) = ? ["1,4",1,2] 

When I execute this query (after inserting the parameters seen above) directly in MySQL it works perfectly, returning the 2 results that I'm looking for.

However, when it is executed by Doctrine it returns an empty array.

Any Ideas??


回答1:


After scouring the interwebs I found the following answer. The problem is with the 'IN' clause. As articulated here:

https://groups.google.com/forum/#!topic/doctrine-dev/-_cINyk2dvs

My problem was being caused by the fact that I was building the 'IN' array as a string.

$s = "1,4"

instead of

$s = array(1,4);

This made all the difference in the world, and also made me feel like a n00b.




回答2:


Your code looks fine and should work. I can't see your whole code but I'm guessing that it is returning an empty array because you have not actually executed the prepared sql statement yet. You should call the "getResult()" to do this.

Try this:

$qb = $query = $this->repoLibrary->createQueryBuilder('l');

$query = $qb
        ->select('l, v.id, p.id')
        ->innerJoin('l.productVariant', 'v')
        ->innerJoin('v.product', 'p')
        ->innerJoin('p.taxons', 't', 'WITH', 't.id IN (:array)')
        ->where('l.user = :user')
        ->groupBy('l.id HAVING count(DISTINCT t.id) >= :count')
        ->setParameter('user', $user)
        ->setParameter('array', $s)
        ->setParameter('count', count($taxons))
        ->getQuery()
        ->getResult();


来源:https://stackoverflow.com/questions/23089490/doctrine-query-builder-bug

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