如何从 native 查询的结果中获取非映射列?
我的查询:
$query = $this->getEntityManager()->createNativeQuery(
"SELECT m.id, m.title, MATCH(m.title) AGAINST('$slug') AS score "
. "FROM music AS m "
. "ORDER BY score DESC LIMIT 100", $rsm);
score
列未映射到实体中,我无法从 Twig 访问它的值。是否可以仅针对此查询将此列添加到实体中?
最佳答案
如果你想在 Twig 模板中显示分数,你可以尝试以下步骤:
1) 将不带任何映射配置的 $score 属性添加到您的 Music 实体:
class Music {
//Other mappings
protected $score;
//TODO: add getter/setter for $score
}
2) 将其添加到您的 ResultSetMapper:
$rsm->addRootEntityFromClassMetadata('YourBundle:Music', 'm');
$rsm->addMetaResult('m', 'score', 'score', false, 'integer'); //first 'score' is your DB alias
3) 调用你的search.html.twig
:
{{ object.score }}
其中 object 是您的 Music 实体。
可以找到有关纯结果和混合结果的其他信息 here .
关于php - 如何在 Doctrine native 查询的结果中获取非映射列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35005162/