我正在使用sonataAdminBundle和symfony2
我做了这个剧本。它过滤搜索框候选项。
但这个query_builder被忽略了。
还有其他需要检查的要点吗?
public function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('user',null,
array(
'query_builder' =>
function (DoctrineORMEntityRepository $rep) {
return $rep->
createQueryBuilder('s')
->where('s.id','1');
})
添加。。。
我正在将此脚本用于新条目,它可以工作
$formMapper
->with('General')
->add('teacher',
null,
array(
'query_builder' =>
function (DoctrineORMEntityRepository $rep) {
return $rep->
createQueryBuilder('s')
->join('s.groups', 'g') // Assuming the association on your user entity is 'groups'
->where('g.name = :group')->setParameter('group','TeacherGroup');
})
)
但是对于数据网格映射器,它不起作用。
$datagridMapper
->add('teacher',null,
array(
'query_builder' =>
function (DoctrineORMEntityRepository $rep) {
return $rep->
createQueryBuilder('s')
->join('s.groups', 'g') // Assuming the association on your user entity is 'groups'
->where('g.name = :group')
->setParameter('group','TeacherGroup');
}))
我通过以下方式实现了这一点
$datagridMapper
->add('user',null,
array(
'field_type' => 'entity',
'field_options => array(
'query_builder' => function (DoctrineORMEntityRepository $rep) {
return $rep->
createQueryBuilder('s')
->where('s.id','1');
}
)
});
你应该
在第四个参数中使用'query_builder',如下所示:
$datagridMapper
->add('teacher',null,null,
array(
'query_builder' =>
function (DoctrineORMEntityRepository $rep) {
return $rep->
createQueryBuilder('s')
->join('s.groups', 'g') // Assuming the association on your user entity is 'groups'
->where('g.name = :group')
->setParameter('group','TeacherGroup');
}))
查看有关此内容的文档。您的语法错误。http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/filter_field_definition.html#callback