如何在本机查询的结果中获取非映射列?
我的查询:
$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访问其值。是否可以仅为此查询将此列添加到实体中?
答案 0 :(得分:2)
如果您想在Twig模板中显示分数,可以尝试以下步骤:
1)将没有任何映射配置的$ score属性添加到您的音乐实体:
#app/models/slot.rb
class Slot < ActiveRecord::Base
has_many :students
accepts_nested_attributes_for :students
end
#app/controllers/slots_controller.rb
class SlotsController < ApplicationController
def edit
@slot = Slot.find params[:id]
end
def update
@slot = Slot.find params[:id]
@slot.update slot_params
end
private
def slot_params
params.require(:slot).permit(students_attributes: [:present])
end
end
2)将其添加到ResultSetMapper:
class Music {
//Other mappings
protected $score;
//TODO: add getter/setter for $score
}
3)拨打$rsm->addRootEntityFromClassMetadata('YourBundle:Music', 'm');
$rsm->addMetaResult('m', 'score', 'score', false, 'integer'); //first 'score' is your DB alias
:
search.html.twig
对象是您的音乐实体。
可以找到有关纯结果和混合结果的其他信息here。