douran6443 2016-07-08 06:46
浏览 34
已采纳

Symfony2查询生成器添加自定义顺序以进行查询

I am using QueryBuilder to get the query of all the products with all joins in my project.. Then I use pagination to display them in frontend. Sometimes I need to get them in a specific order, but I cant find a way to do that in QueryBuilder..

For example one query returns products by id: 70,71,72,73,74,75 But if I have some custom order I want to display it: 72,74,75,70,71,73

So in my querybuilder I do something like this:

public function querySortedProductsInCategories($id, $type, $slug, $sort)
{
    $qb = $this->createQueryBuilder('p');
    $qb->addSelect(array('p', 'gallery'));
    $qb->addSelect(array('p', 'media'));
    $qb->addSelect(array('p', 'image'));
    $qb->leftJoin("p." . $type, "c");
    $qb->leftJoin('p.gallery', 'gallery');
    $qb->leftJoin('gallery.galleryHasMedias', 'media');
    $qb->leftJoin('media.media', 'image');
    $qb->where("c." . $type. "= :id ");
    $qb->andWhere($qb->expr()->in('p.id', $sort));
    $qb->SetParameter('id', $id);
    return $qb->getQuery();
}

The generated query looks something like this:

'SELECT p, p, gallery, p, media, p, image FROM Mp\ShopBundle\Entity\Product p LEFT JOIN p.subcategory c LEFT JOIN p.gallery gallery LEFT JOIN gallery.galleryHasMedias media LEFT JOIN media.media image WHERE c.subcategory= :id  AND p.id IN('70', '73', '76', '72', '71', '74')'

But the returned array is stil ordered by Id...

If i try to do something like:

    $qb->orderBy("p.id", $sort);

But then ofcourse I get array to string conversion error...

What are the possible ways to do this?

  • 写回答

2条回答 默认 最新

  • donglikuang8145 2016-07-08 12:06
    关注

    You could do the trick with some overhead by using the indexBy parameter of the doctrine's queryBuilder from function

    public function from($from, $alias, $indexBy = null);

    this set the index of the resulting array to be the values of the field in this paramareter.

    Then you can reorder this array in your way

    public function querySortedProductsInCategories($id, $type, $slug, $sort)
    {
        $qb = $this->createQueryBuilder('p');
        $qb->from('Product', 'p', 'p.id';
        [... the rest of your query]
        $result = $qb->getQuery()->getResult();
        // Here you have an array indexed by the id of your products and you can order it using your $sort array of products ids
        uksort($result,
            function ($key1,$key2) use ($sort) {
                $product1Position = array_search($key1,$sort);
                $product2Position = array_search($key2,$sort);
                if ( $product1Position === false || $product2Position === false) {
                    return 0;
                }
                return ($product1Position < $product2Position) ? -1 : 1;
            }
        );
        return $result;
    }
    

    Sources: http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/query-builder.html#high-level-api-methods http://php.net/manual/en/function.uksort.php

    Good luck

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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