douwo4837 2014-10-22 14:24
浏览 265
已采纳

Doctrine QueryBuilder查询:具有多个关联

I'm building a search form for secondhand cars. In the form the user can select checkboxes for the options the car should have. Because the user can select multiple options, i need to search a car that has all options the user has selected.

I'm trying to build a query that checks if the 'car' has the selected options. Right now im able to check if the car has one of the selected options by doing :

// The options filter is something special, the parameter is passed as a comma separated
// String with options ids
        if($param == 'car.options' && $optionsIds){
            $queryBuilder->leftJoin('car.options', 'option');
            $options        = $queryBuilder->expr()->orX();
            foreach(explode(',', $optionsIds) as $id) {
                $options->add($queryBuilder->expr()->eq('option.id', $id));
            }
            $wheres->add($options);
            continue;
        } 

But when i change the 'orX' with an 'andX' the result is always returned empty.

The codesnippet above is part of a larger function.

  • 写回答

1条回答 默认 最新

  • dongshaoxiong0012 2014-10-23 09:42
    关注

    I finally managed to get it working. The trick was was using a subquery :

    $carsWithSelectedOptions = $this->getEntityManager()->createQuery('
        SELECT c.id
        FROM Occasions\Model\Bo\Car AS c JOIN c.options o
        WHERE o.id IN ('.$optionsIds.')
        AND c.id = car.id
        GROUP BY c.id
        HAVING COUNT(o.id) >= '.count(explode(',', $optionsIds))
    );
    
    $wheres->add($queryBuilder->expr()->exists($carsWithSelectedOptions->getDQL()));
    

    Note this is part of a larger query. The full query results in :

    SELECT advert, customer, car, country, brand, model 
    FROM Occasions\Model\Bo\Advert advert 
    LEFT JOIN advert.customer customer 
    LEFT JOIN advert.car car 
    LEFT JOIN customer.country country 
    LEFT JOIN car.model model 
    LEFT JOIN car.brand brand 
    WHERE EXISTS( 
        SELECT c.id FROM Occasions\Model\Bo\Car AS c 
        JOIN c.options o WHERE o.id IN (2,4,5,51) 
        AND c.id = car.id GROUP BY c.id HAVING COUNT(o.id) >= 4)
    AND brand.id = :brandid 
    AND model.id = :modelid
    AND country.country_code = :countrycountry_code
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器