原则:选择查询刷新水合实体联接



查询加载主实体并将其与已连接的实体合并,这些实体将被过滤
具有不同筛选的同一查询没有更新加入的实体。

一些数据:

Tiers:
| id | name  |
| 1  | alpha |
| 2  | beta  |
Container:
| id  | tiers_id | category |
| 10  | 1        | A        |
| 20  | 1        | A        |
| 30  | 1        | B        |
| 40  | 1        | B        |

执行2个查询以获得一些层,这些层的容器已加入,先是A类,然后是B类:

$dql = "select t, c
from Tiers t
join t.containers c
where t.id in (?1) and c.category = (?2)";
$result = $em->createQuery($dql)
->setParameter(1, array(1))
->setParameter(2, 'A')
->getResult();
$tiers = $result[0];
$containers = $tiers->getContainers(); // tiers 1 with containers 10 and 20, that's fine !
$result = $em->createQuery($dql)
->setParameter(1, array(1))
->setParameter(2, 'B')
->getResult();
$tiers = $result[0];
$containers = $tiers->getContainers(); // BAD HERE: still get containers 10 and 20, looking for containers 30 and 40.

在第二次查询之后,第1层保留在第一次查询期间装载的容器。这不是人们所期望的
那么,有没有办法在第二次查询后获得容器30和40
也许是在第一次查询后"重置/分离"层实体的容器
或者其他什么。。。


查询中的多重选择用于使所需容器连接的层水合
'getContainers'方法给出了每个层中预期的容器
BDD的成本仅为1个SQL查询,无论搜索的层数量如何。


我认为层不能分离/重新加载,因为它们在查询之前、查询之间和查询之后都会更新,刷新时会抛出这种异常:

Uncaught Exception: Multiple non-persisted new entities were found through the given association graph
* A new entity was found through the relationship 'XXX' that was not configured to cascade persist operations for entity: XXXEntityTiers@00000000257b87500000000018499b62.

在第二次查询之前重置层的容器:

foreach($result as $tiers)
$tiers->nullContainers();

将方法添加到实体\层:

public function nullContainers()
{
this->containers = null;
}

然后第二个查询"刷新"层的容器。

最新更新