Monday 5 November 2012

Symfony admin generator add custom criteria to filter

If you have in list field that doesn't exist as a database feild to the formFilter class you need to add your custom criteria for that field.
Note: function needs to be called addYourFieldNameColumnCriteria

Example: /lib/filter/User/UserFormFilterClass
public function addYourFilterColumnCriteria(Criteria $criteria, $field, $values)
  {
    $values = trim($values);
    if (!empty($values))
    {
      $criteria->add(YourPeer::FIELD_NAME, $values);
    }
  }


But sometimes $values returns an ampty array (I am not sure when), then you need to register you type of field and then to access it ina a different way:

public function addYOUR_FIELDColumnCriteria(Criteria $c, $field, $values)
  {
    $value = trim($values['text']);
    if (!empty($value))
    {
      // Your ciriteria
    }
  }
  public function getFields()
 {
  $fields = parent::getFields();
  $fields['YOUR_FIELD'] = 'Text';
  return $fields;
 }
Off course YourFilter, YourPeer, FIELD_NAME,YOUR_FIELD and field type you need to adjust to your needs.

No comments:

Post a Comment