简体   繁体   English

如何设置表中不存在但在原始SQL中存在的实体字段作为别名?

[英]How to set an entity field that does not exist on the table but does exists in the raw SQL as an alias?

Lets say that we have a query like this one: 假设我们有一个类似这样的查询:

SELECT *, (CUSTOM_EXPRESSION) as virtualfield FROM users

The user's entity itself has the "virtualfield" but the mapping annotations no, since the table doesn't have this field. 用户的实体本身具有“虚拟字段”,但映射注释没有,因为表没有此字段。

Assuming that it is executed as a raw SQL, how do we populate the entity with the field above? 假设它是作为原始SQL执行的,我们如何用上面的字段填充实体?

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. 然后,您可以遍历它,并根据需要在对象上设置属性。

I'm not entirely sure I understand what you are asking. 我不完全确定我了解您的要求。 I assume you want to know how to update users.virtualfield with the (CUSTOM_EXPRESSION)? 我假设您想知道如何使用(CUSTOM_EXPRESSION)更新users.virtualfield? That syntax would be: 该语法为:

update users set virtualfield = (CUSTOM_EXPRESSION)

If you wanted to update all rows. 如果要更新所有行。

If I'm off the mark can you please clarify your question? 如果我不遵守规定,可以请您澄清问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM