Reputation: 58
How can I get non-mapped column in result from native query?
My query:
$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);
Column score
isn't mapped in entity and I don't have access to its value from Twig. Is it possible to add this column into entity only for this query?
Upvotes: 3
Views: 3981
Reputation: 635
If you want to show score inside Twig template, you can try following steps:
1) Add $score attribute without any mapping configuration to your Music entity:
class Music {
//Other mappings
protected $score;
//TODO: add getter/setter for $score
}
2) Add it to your ResultSetMapper:
$rsm->addRootEntityFromClassMetadata('YourBundle:Music', 'm');
$rsm->addMetaResult('m', 'score', 'score', false, 'integer'); //first 'score' is your DB alias
3) Call in your search.html.twig
:
{{ object.score }}
Where object is your Music entity.
Additional information about pure and mixed results could be found here.
Upvotes: 1