symfony 管理员过滤器加入 [英] symfony admin filter with join
问题描述
我有一个表,heading
,它有一个 import_profile_id
.import_profile
有一个 bank_id
.
I have a table, heading
, that has an import_profile_id
. import_profile
has a bank_id
.
在我的管理员的标题列表页面上,我想添加按 bank_id
过滤的功能.但是,由于 heading
没有 bank_id
- 它需要通过 import_profile
来获取 - 我不能只添加 bank_id
字段并期望它起作用.
On my headings list page in my admin, I'd like to add the ability to filter by bank_id
. However, since heading
doesn't have a bank_id
- it needs to go through import_profile
to get that - I can't just add a bank_id
field and expect it to work.
谁能解释一下如何做到这一点?我发现的最接近的是这篇文章 但我认为它并没有真正解决我的问题.
Can anyone explain how to do this? The closest thing I've found is this post but I don't think it really addresses my issue.
推荐答案
这可以通过使用虚拟列来完成,就像您找到的帖子一样.虚拟列是一种添加新标准以使用 symfony 提供的自动生成过滤器进行过滤的方法.它是这样工作的:
This can be done by using virtual columns like the post you found. The virtual column is a way to add a new criteria to filter using the autogenerated filter provided by symfony. It works like this:
1 - 转到管理模块的 generator.yml 并添加将创建和添加的虚拟列的名称
1 - Go to the generator.yml of the admin module and add the name of the virtual column that will create and add
<!-- apps/backend/modules/module_name/config/generator.yml -->
filter:
[virtual_column_name, and, other, filter, columns]
2 - 在您的 lib/filter/{TableName}FormFilter.class.php(我认为在您的情况下必须是 HeadingFormFilter)中,您必须在 configure() 方法中定义该虚拟列
2 - In your lib/filter/{TableName}FormFilter.class.php (I think in your case must be HeadingFormFilter) you have to define that virtual column in the configure() method
public function configure()
{
//Type of widget (could be sfWidgetFormChoice with bank names)
$this->widgetSchema['virtual_column_name'] = new sfWidgetFormInputText(array(
'label' => 'Virtual Column Label'
));
//Type of validator for filter
$this->validatorSchema['virtual_column_name'] = new sfValidatorPass(array ('required' => false));
}
3 - 覆盖该类的 getFields() 以在过滤器中定义它并设置过滤器功能
3 - Override the getFields() of that class to define it in the filter and set the filter function
public function getFields()
{
$fields = parent::getFields();
//the right 'virtual_column_name' is the method to filter
$fields['virtual_column_name'] = 'virtual_column_name';
return $fields;
}
4 - 最后你必须定义过滤器方法.这个方法必须以 add...ColumnQuery 模式命名,在我们的例子中必须是 addVirtualColumnNameColumnQuery(不是一个愉快的名字选择:P),所以
4 - Finally you have to define the filter method. This method must be named after the add...ColumnQuery pattern, in our case must be addVirtualColumnNameColumnQuery(not a happy name choice :P), so
public function addVirtualColumnNameColumnQuery($query, $field, $value)
{
//add your filter query!
//for example in your case
$rootAlias = $query->getRootAlias();
$query->innerJoin($rootAlias . '.ImportProfile ip')
->andWhere('ip.BankId = ?', $value);
//remember to return the $query!
return $query;
}
完成!您可以通过 bank_id 了解过滤器.
Done! You can know filter by bank_id.
这篇关于symfony 管理员过滤器加入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!