symfony 2.4中sonata admin中用于关系对象的文本网格(下拉列表插入)的datagrid过滤器 [英] datagrid filter for relation object as text field (insted of dropdown) in sonata admin in symfony 2.4
问题描述
我具有与用户"相关的实体动作".在SonataAdminBundle中创建了Admin CRUD控制器.一切正常,除了用户过滤器呈现为下拉列表.我的用户数量达到了8k,并且还在不断增长,因此您必须了解为什么这是一个问题.
我希望用户过滤器是文本输入,并且要提交后才能使用LIKE %username%
I have entity 'Action' with relation to 'User'. Created Admin CRUD controller in SonataAdminBundle. Everything works fine except user filter is rendered as dropdown list. I have 8k user count and growing so you must see why this is a problem.
I want user filter to be text input and on submit to search with LIKE %username%
现在我添加这样的用户过滤器-$datagridMapper->add('user')
.
Right now I add user filter like this - $datagridMapper->add('user')
.
我知道我可以添加过滤器类型和字段类型,但是我找不到正确的组合和选项.在 http://sonata-project上找到的信息. org/bundles/doctrine-orm-admin/master/doc/reference/filter_field_definition.html ,但仍然没有成功.
I know I can add filter type and field type but I am not able to find the right combination and options. Found information on http://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/filter_field_definition.html but still no success.
在Alex Togo回答之后,我使用了以下代码:
Following Alex Togo answer I used this code:
$datagridMapper->add('user', 'doctrine_orm_callback', array(
'callback' => function($queryBuilder, $alias, $field, $value) {
if (empty($value['value'])) {
return;
}
$queryBuilder->leftJoin(sprintf('%s.user', $alias), 'u');
$queryBuilder->where('u.username LIKE :username');
$queryBuilder->setParameter('username', '%'.$value['value'].'%');
return true;
},
'field_type' => 'text'
))
推荐答案
我在项目中需要这样的东西.我使用此.您可以尝试将"field_type"选项设置为"text"(我在工作过的项目中使用"choice"),并添加需要过滤的querybuilder操作.
I needed something like this on a project. I implemented this feature using this. You can try to set the 'field_type' option to 'text' (I used 'choice' in the project I worked at) and add the querybuilder actions you need to filter.
这篇关于symfony 2.4中sonata admin中用于关系对象的文本网格(下拉列表插入)的datagrid过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!