使用LeftJoin在Sonata Admin中优化原则查询 [英] Optimizing doctrine queries in Sonata Admin with LeftJoin

查看:161
本文介绍了使用LeftJoin在Sonata Admin中优化原则查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何优化由sonata admin在列表中编辑的数据库查询,并编辑视图?



我留下了在我的实体存储库中使用querybuilder的一些查询,这已经帮助了很多,把我的查询从100+降到了大约22。



但剩下的查询是通过使用formbuilder和listmapper自动发生的。



有没有可以进一步优化这些类的查询?
甚至不知道在这一点查询是什么...我试图覆盖findBy,findAll方法的存储库,但他们似乎使用像

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

不知道如何添加一个连接语句...



我想这不仅仅是在奏鸣曲管理员中,而且在前端使用内置的EntityRepository find,findAll,findOneBy等功能的查询。 p>

解决方案

在您的管理类中,覆盖 createQuery() / p>

  public function createQuery($ context ='list')
{
$ query = parent :: createQuery ($背景);

$ query
- > addSelect(...)
- > leftJoin(...)//使用$ query-> getRootAlias()这里
;

return $ query;
}

对于编辑视图,它有点更棘手,因为您需要覆盖sonata控制器通过扩展 Sonata\AdminBundle\Controller\CRUDController ,然后覆盖 editAction()方法。 / p>

控制器名称可以在服务声明的第三个参数中指定。


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.

解决方案

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.

这篇关于使用LeftJoin在Sonata Admin中优化原则查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆