如何设置表中不存在但在原始SQL中存在的实体字段作为别名? [英] How to set an entity field that does not exist on the table but does exists in the raw SQL as an alias?
本文介绍了如何设置表中不存在但在原始SQL中存在的实体字段作为别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们说我们有一个类似这样的查询:
Lets say that we have a query like this one:
SELECT *,(CUSTOM_EXPRESSION)作为来自用户的虚拟字段
用户实体本身具有虚拟字段",但映射注释没有,因为表没有此字段.
The user's entity itself has the "virtualfield" but the mapping annotations no, since the table doesn't have this field.
假设它是作为原始SQL执行的,我们如何用上面的字段填充实体?
Assuming that it is executed as a raw SQL, how do we populate the entity with the field above?
推荐答案
我找到了答案.为此,您需要使用标量值.例如:
I've found the answer. To do so, you need to use a scalar value. For instance:
$rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder($this->getEntityManager());
$rsm->addRootEntityFromClassMetadata('Category', 'c');
$rsm->addScalarResult('depth', 'depth');
// [ ... ]
$results = $q->execute();
// Output will be a two-dimensional array
// array(0 => array(0 => CategoryObject, 'depth' => 'scalar-value', // ... ), // ...)
然后,您可以遍历它,并根据需要在对象上设置属性.
Then, you can loop through it and set the property on the object if you want.
这篇关于如何设置表中不存在但在原始SQL中存在的实体字段作为别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文