是否使用HYDRATE_ARRAY强制“获取已加入"关系以包含其ManyToOne关系的标识? [英] Force 'fetch joined' relations to include IDENTITY of their ManyToOne relations using HYDRATE_ARRAY?

查看:52
本文介绍了是否使用HYDRATE_ARRAY强制“获取已加入"关系以包含其ManyToOne关系的标识?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询,其中我将许多表连接到原始的Person实体.一个 Person 可能具有多个 Child 关系(OneToMany),一个 Child 可能具有一个 School ,他们去(ManyToOne).问题是,我不需要连接到每个孩子的整个 School 实体,只需要已经存储在 Child 上的他们的 id .

I have a query in which I'm joining a number of tables to my original Person entity. A Person may have multiple Child relations (OneToMany), and a Child may have a School they go to (ManyToOne). Problem is, I don't need the entire School entity that connects to each child, only their id, which is already stored on Child.

我正在使用 Paginator 遍历结果,并使用HYDRATE_ARRAY来减少将数据解析为实体对象的ORM的开销.但是未获取的关系的 id 字段不会以这种方式返回,因此,School的 id 也不会返回.

I'm using a Paginator to iterate through the results and I use HYDRATE_ARRAY to reduce overhead of the ORM parsing data to entity objects. But the id fields of unfetched relations are not returned this way, and thus, the School id isn't either.

我也可以加入 School 实体,但是由于身份已经存储在 Child 记录中,所以我不明白为什么我应该通过降低数据库联接另一个表.将结果作为实体对象获取也可以解决该问题,但是也会以性能为代价.如何获得 id 来包装结果,而不必不必要地加入 School 实体或不必将结果作为对象进行混合?

I may join the School entity too, but since the identity is already stored on the Child records, I don't see why I should further reduce performance by having the database join another table. Fetching results as entity objects would also solve the problem, but also at the cost of performance. How can I get the id to apper the results without having to unnecessarily join the the School entity or having to hydrate the results as objects?

$query = $em->getRepository(Entity\Person::class)->createQueryBuilder('p');

$query
  ->select([
    'p as person',
    'w.name as workplace_name',
    'c',
  ])
  ->leftJoin('p.children', 'c') //Entity\Child
  ->leftJoin('p.workplace', 'w') //Entity\Company
  //...

;
$paginator = new Paginator($query);
$paginator->getQuery()
  ->setHydrationMode(\Doctrine\ORM\Query::HYDRATE_ARRAY);

推荐答案

您可以使用

You can use Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS to include the foreign key column values in the result:

$paginator->getQuery()
->setHint(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true)
->setHydrationMode(\Doctrine\ORM\Query::HYDRATE_ARRAY);

其中:

includeMetaColumns查询提示使元列(例如外键和区分符列)被选择并作为查询结果的一部分返回.

The includeMetaColumns query hint causes meta columns like foreign keys and discriminator columns to be selected and returned as part of the query result.

参考

这篇关于是否使用HYDRATE_ARRAY强制“获取已加入"关系以包含其ManyToOne关系的标识?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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