gpt4 book ai didi

php - 每个实体一个查询

转载 作者:行者123 更新时间:2023-12-03 02:09:13 25 4
gpt4 key购买 nike

想象一下,我们有一个ArrayCollection,其中包含Country的几个Doctrine Entities实例。还要想象一下,我的方法“getName()”实际上是与几种语言的一对多关系(A2lixTranslation支持)。

关键是该ArrayCollection是从ElasticSearch服务构建的,因此当我检索所有这些实体,对其进行迭代并打印其名称时,这只是对每个类别的额外查询。

我真的不知道该如何处理这种情况,因此无法维护150个额外的查询...

示例实现

$countries = // ArrayCollection of Countries, returned by any mapping system.
foreach ($countries as $country) {

/**
* As name is an entity, uses lazy loading in every iteration
* Because I get collection as it comes, I would like to retrieve
* all names in one query. I thought about perform a DQL with a join
* of all countries and their names, so Doctrine will catch'em all
* but only catch my query and results, and do not identify retrieved
* results with my collection, so is not working...
*/
$name = $country->getName();
echo $name;
}

// Could be nice do something like this...

$countries = // ArrayCollection of Countries, returned by any mapping system.
$queryBuilder = $this
->getDoctrine()
->getRepository('ProjectCoreBundle:Country')
->createQueryBuilder('c');

/**
* This query result should only add Cache with results
*/
$queryBuilder
->select('c','t')
->innerJoin('c.countryName','cn','WITH','c.id = cn.Country')
->getQuery()
->getResult();

foreach ($countries as $country) {

/**
* At this poing, as Name relation entity is already loaded and cached
* lazy load will simply return object ( Any query is performed )
*/
$name = $country->getName();
echo $name;
}

最佳答案

我建议您看一下Elasticsearch中的Multi Search API端点:http://www.elasticsearch.org/guide/reference/api/multi-search/。它将允许您准备包含多个查询的单个Web请求; ES将返回与请求中查询数量相同的响应数量。确实缩短了网络通信时间。

我认为替代方法是重组代码/数据/思维方式,以使您可以执行单个查询以获取所需的所有信息,但是我认为这里没有足够的信息来进行深入研究。

关于php - 每个实体一个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18270631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com