dongying195959 2013-02-01 11:57
浏览 30
已采纳

Symfony2,querybuilder的Doctrine请求

I have a basic table article linked to a table category by id (classic), I would like to make a doctrine request to retrieve only 5 articles by category (for all categories)

This request return me all articles of categories, I just want 5 for each

public function getArticlesAndCategs(){
    $qb = $this->createQueryBuilder('c')
            ->orderBy('c.id','DESC')
                ->leftJoin('c.articles', 'a')
                ->addSelect('a')
                ->addOrderBy('a.id','DESC')
            ->getQuery();
    return $qb->execute();                
}

Can you help me for that? thank you

  • 写回答

1条回答 默认 最新

  • duanhui9840 2013-02-01 12:09
    关注

    This is no easy task unfortunately. At least I haven't found a satisfying solution yet (satysfying for me ;) ).

    Three things you can do:

    1. Iterate through the categories and retrieve 5 posts for each category. This of course cause that many db request as many you have categories.

    2. Return all the categories with joined posts and iterate inside php. The drawback - you'll return all the posts, so if you have many, this can mean a lot of memory used.

    3. If you don't have to stick to Doctrine you can try the solution from here: mySQL Returning the top 5 of each category

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化