Customizing Admin Generator Filters in Symfony Using Doctrine
How to extend the existing filters in the symfony doctrine admin generator
Here is our (simplified) schema:
Group: [id, group_name] User: [username, group_id] Phonenumber: [user_id, num]
Groups contain many users, and users contain many phone numbers.
The prebuilt admin generator will allow you to filter on any of the existing fields in the table for the object that you are viewing, so if we were to visit /phonenumber/index then we would see all the phonenumbers and be able to filter by user.
However, if we want to filter, for instance, by group, then we need to follow a few steps:
- Add group_id to phonenumber/config/generator.yml filter list
config: filter: display: [user_id, group_id] - Add widget/validator to PhonenumberFormFilter
We need to alter the filter form to add the select box for groups, and add the validator so that we can sanitize and get the value back.public function configure() { $this->setWidget('group_id', new sfWidgetFormDoctrineChoice(array('model' => 'Group', 'add_empty' => true))); $this->setValidator('group_id', new sfValidatorDoctrineChoice(array('required' => false, 'model' => 'Group', 'column' => 'id'))); } - Add field to PhonenumberFormFilter
Just so that the filter knows what to filter on we add this method.public function getFields() { return array_merge((array('group_id' => 'ForeignKey''), parent::getFields()); } - Alter query to PhonenumberFormFilter
This is where we alter the actual query. We are adding a join against the user table, and then a where with a group_id.public function addGroupIdColumnQuery(Doctrine_Query $query, $field, $value) { $query->innerJoin(sprintf('%s.%s', $query->getRootAlias(), 'User u')) ->where('u.group_id = ?', $value); } - This method could certainly have gone in the PhonenumberTable class; in which case it would look like this
public function addGroupIdColumnQuery(Doctrine_Query $query, $field, $value) { Doctrine::getTable('Phonenumber')->applyGroupIdFilter($query, $value); }