duandai2178 2014-10-19 04:29
浏览 17
已采纳

使用LeftJoin优化Sonata Admin中的学说查询

How do you optimize the DB queries made by sonata admin in the list and edit views ?

i LeftJoined some queries that i made using the querybuilder in my entity repository , this already helped a lot, brought my queries down from 100+ to about 22.

But the remaining queries are the ones that happen automatically by using the formbuilder and listmapper.

Is there anyway i can further optimize the queries made by those classes ? im not even sure at this point where the queries are made... i tried to overwrite the findBy, findAll methods of the repository but they seem to use something like

$persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);
return $persister->load($criteria, null, null, array(), 0, 1, $orderBy);

Not sure how i can add a join statement to that...

And i guess it's not just in the sonata admin but also for queries that i use in the front-end that use the built-in EntityRepository find, findAll, findOneBy etc... functions.

  • 写回答

2条回答 默认 最新

  • dscojuxf69080 2014-10-21 15:32
    关注

    In your admin class override the createQuery() method like this :

    public function createQuery($context = 'list')
    {
        $query = parent::createQuery($context);
    
        $query
            ->addSelect(...)
            ->leftJoin(...) // use $query->getRootAlias() here
        ;
    
        return $query;
    }
    

    For the edit view it's a little bit more tricky because you need to override the sonata controller by extending Sonata\AdminBundle\Controller\CRUDController and then override the editAction() method.

    The controller name can be specified in the third argument of the service declaration.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度