symfony filter change behaviour from [field=value]

2019-06-14 05:22发布

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条回答
趁早两清
2楼-- · 2019-06-14 06:08

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.

查看更多
登录 后发表回答