[英]How can I reduce the number of queries Doctrine is performing?
我正在构建一个产品管理工具,该product
可以具有任意数量的attributes
, documents
, features
, images
, videos
以及单个type
, brand
和category
。 还有一些其他相关表,但这足以说明问题。
有一个称为ProductModel
的Model类,其中包含这样的方法(为清楚起见已简化):
public function loadValues() {
//Product entity data
$this->id = $this->entity->getId();
$this->slug = $this->entity->getSlug();
// One of each of these
$this->loadType();
$this->loadBrand();
$this->loadCategory();
// Arbitrary number of each of these
$this->loadAttributes();
$this->loadDocuments();
$this->loadFeatures();
$this->loadImages();
$this->loadVideos();
...
}
每个加载方法都会执行一些样板,最终执行此方法:
public function loadEntitiesByProductId($productId=0) {
// Get all the entities of this type that are associated with the product.
$entities = $this->entityManager
->getRepository($this->entityName)
->findByProduct($productId);
$instances = array();
// Create a Model for each entity and load the data.
foreach ($entities as $entity) {
$id = $entity->getId();
$instances[$id] = new $this->childClass();
$instances[$id]->entity = $entity;
$instances[$id]->loadValues();
}
return $instances;
}
对于相关实体是单个表但通常是映射器的情况,这是可以的。 在那些情况下,我将在第一个查询中获得所有映射器实体,然后必须在loadValues()
方法中查询相关实体(通过Doctrine的get<Entity>()
方法)。 此过程的结果是大量查询(通常> 100)。 我需要摆脱无关的查询,但是我希望这样做而不会丢失我在数据模型中使用的惯用语。
有没有办法让EntityManager在使用联接对这些查询进行分组方面做得更好?
我以前的方法存在两个问题:
首先,我从存储库中获取实体,而不是从现有实体中加载它们:
$entities = $this->entityManager
->getRepository($this->entityName)
->findByProduct($productId);
更好的是:
$method = $this->deriveGetMethod($this->entityName);
$entities = $productEntity->$method()
其次,我正在使用$this->entityManager->getRespository...
检索产品实体$this->entityManager->getRespository...
这对于加载小型数据集(单个表或一个或两个关系)可以很好地工作,但是无法获取存储库的findBy
方法来在单个查询中加载关系。 解决方案是使用queryBuilder。
$qb = $this->entityManger->createQueryBuilder();
$query = $this->select('product',/*related tables*/)->/*joins etc.*/
$productEntity = $query->getSingleResult();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.