by prettyscripts on 2010-05-19 15:27:49
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('', 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:
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; | |
} |