Symfony: Customize admin generator filter

I want to create a yes / no choice field if a field is null or not.

eg, a membership table with suspend_date field. I want to be able to filter list of member who is or not suspended.

In filter part of /path/to/application/module/config/generator.yml:

filter:
  display:    [name, is_suspended]  

To add a field in filter box, in /lib/filter/doctrine/MemberFormFilter.class.php:

public function configure()
    {
        $this->widgetSchema['is_suspended'] = new sfWidgetFormChoice(array(
            'choices' => array(
                            ''  => 'yes or no',
                            1   => 'yes',
                            0   => 'no'),
        ));
        $this->validatorSchema['is_suspended'] = new sfValidatorChoice(array(
                'required'      => false,
                'choices'       => array('', 1, 0),
        ));
    }
 
    public function getFields() {
        $fields = parent::getFields();
        $fields['is_suspended'] = 'is_suspended';
        return $fields;
    }
 
    // make sure the function name format is add<Field>ColumnQuery
    public function addIsSuspendedColumnQuery($query, $field, $value) {
        Doctrine::getTable('Member')->applyIsSuspendedFilter($query, $value);
    }

A filter method for table, in /lib/model/doctrine/MemberTable.class.php:

static public function applyIsSuspendedFilter($query, $value) {
        $alias = $query->getRootAlias();
        switch ($value) {
        case '0':
            $query->addWhere($alias . '.suspended_at is null');
            break;
        case '1':
            $query->addWhere($alias . '.suspended_at is not null');
            break;
        }
        return $query;
    }

3 thoughts on “Symfony: Customize admin generator filter

  1. Erdene Reply

    Great post, I trying in my case.

    I have one problem.

    static public function applyNameFilter($query, $value) {
    $alias = $query->getRootAlias();
    $value->

    $query->addWhere($alias . ‘.name like ‘%’.$value.’%’ ‘);

    return $query;
    }

    This function returns following query SELECT COUNT(*) AS num_results FROM tblname t WHERE t.name like ‘%Array%’

    My question is how I get my real value from $value. Here value is array. I need only one text value from my search field.

    Please help me

Leave a Reply

Your email address will not be published. Required fields are marked *