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条)

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM