Mittwoch, 31. März 2010

Filter Admin-List in Symfony 1.4 + Doctrine

Some time ago I wrote an article about filtering the admin-list in Symfony 1.0 and Propel. Now in Symfony 1.4 and Doctrine the steps to do this are completely different:

First we have to change the auto-generated file in apps/[APP]/modules/[MODULE]/actions/actions.class.php and to add a function "buildQuery()" to the [module]Actions-class:


class hotelActions extends autoHotelActions
{
protected function buildQuery()
{
$query = parent::buildQuery();
//Here you can extend your $query to filter the result.
return $query;
}
}


So if we have a "creator_id"-field in our schema and we want to filter the result to show only the entries created by the current user, we have to add the following code:


class hotelActions extends autoHotelActions
{
protected function buildQuery()
{
$query = parent::buildQuery();
$rootAlias = $query->getRootAlias();
$query->andWhereIn($rootAlias.'.creator_id', Array($this->getUser()->getGuardUser()->getId()));
return $query;
}
}