symfony: customize admin generator filter

by prettyscripts on 2010-05-19 15:27:49

phpsymfony

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:

Code:

filter:
  display:    [name, is_suspended]

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

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(''10),
        ));
    }
 
    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:

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;
    }

Tags: admin, filter, symfony

3 comments

Comment by Jaime Suez @ 2010-06-16 03:20:38
*****
Great Post! Thanks!
Comment by ejsidisi @ 2011-05-18 21:39:08
Awesome, I just need to! Thanks!!!
Comment by Erdene @ 2011-10-02 04:09:55
*****
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 comment


Your email address will not be revealed on this site.
PoorExcellent
note: all comments are moderated. do not spam and do not advertise. only comments relevant to the post will be published.
(Line breaks become <br />)
(For my next comment on this site)
(Allow users to contact me through a message form -- Your email will not be revealed!)