symfony filter change behaviour from [field=value] to [field LIKE %value%]

随声附和 提交于 2019-12-08 09:45:45

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/8503938/symfony-filter-change-behaviour-from-field-value-to-field-like-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!