symfony过滤器从[field = value]更改为[field LIKE%value%]的行为 [英] symfony filter change behaviour from [field=value] to [field LIKE %value%]
问题描述
等于
的值到过滤表单中提供的值。我想更改其行为,在数据库表中搜索与表单(%LIKE%
)中提供的字段值匹配的行/行。 我知道可以通过向过滤器类添加一个 addFieldnameColumnQuery
方法,但是我想知道的是有没有另一种方法?
该字段恰好是一个外键,我希望它像一般的文本字段一样工作。
更新:这是一个愚蠢的错误。我需要将一个sfWidgetFormFilterInput分配给widgetSchema,但是我正在使用sfWidgetFormInput,它导致它寻找相等而不是匹配。
我从来没有听说过另一种方式...看看 sfFormFilterDoctrine
类位于 lib \vendor\symfony\lib\plugins \sfDoctrinePlugin\lib\form
以了解该系统的工作原理,以及如何编写 addFieldnameColumnQuery
更新
要更改某个字段的过滤器行为,请从外部 myfield
键到正常的文本,您可以简单地设置小部件,并在 MymoduleFormFilter $中覆盖
getFields()
c $ c>类有一些这样的代码:
public function configure()
{
$ this-> setWidget('myfield',new sfWidgetFormFilterInput());
$ this-> setValidator('myfield',new sfValidatorPass(array('required'=> false)));
}
public function getFields()
{
$ fields = parent :: getFields();
$ fields ['myfield'] ='Text';
return $ fields;
}
相反,如果您定义要在连接表中搜索的字段,则还没有根据您在generator.yml中写的字段名称设置小部件
filter:
display:[...一些字段...,myfield]
最后添加 addMyfieldColumnQuery(Doctrine_Query $查询,$字段,$值)
与连接和其中代码。对不起,因为我使用Propel,所以没有代码片段。
In my filter, a field's behaviour is to search in the DB table for a row that has the value of the field equal
to the value provided in filtering form. I'd like to change its behavior to search in DB table for a row/rows that have field value matching to that provided in the form(%LIKE%
).
I know it can be done by adding a addFieldnameColumnQuery
method to the filter class but What I want to know is, is there another way?
The field happens to be a foreign key and I want it to work like a normal text field.
UPDATE: this was a silly mistake. I needed to assign a sfWidgetFormFilterInput to the widgetSchema but I was using sfWidgetFormInput which was causing it to look for equality instead of matching.
I never hear about another way... have a look at sfFormFilterDoctrine
class located in lib\vendor\symfony\lib\plugins\sfDoctrinePlugin\lib\form
to understand how this system works and how you can write your addFieldnameColumnQuery
UPDATE
To change just the filter behaviour for a field, say myfield
, from foreign key to normal text you can simply set the widget and override getFields()
in MymoduleFormFilter
class with some code like this:
public function configure()
{
$this->setWidget('myfield', new sfWidgetFormFilterInput());
$this->setValidator('myfield', new sfValidatorPass(array('required' => false)));
}
public function getFields()
{
$fields = parent::getFields();
$fields['myfield'] = 'Text';
return $fields;
}
Instead if you define a field to search in a joined table you have yet to set the widget according to the field name you wrote in generator.yml
filter:
display: [... some fields ..., myfield]
and finally add addMyfieldColumnQuery(Doctrine_Query $query, $field, $values)
with inside the join and where code. Sorry, no snippet for this because I use Propel.
这篇关于symfony过滤器从[field = value]更改为[field LIKE%value%]的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!