In my Symfony project I'm using SonataAdminBundle. And I got a question: For example I have some entity with a function:
class Entity {
protected $id;
public idBiggerThan($value) {
return $id > $value;
}
And I want to create a filter that filters list by the result of this function (for example to be able choose only entities that have id bigger that current user).
I understand that this filter can be written without using this function, but I have much more complex functions, for which I don't want to write queries.
Thanks for your help.
UPD: I have tried something like this, but it doesn't work
$datagridMapper
->add('id', 'doctrine_orm_callback', array(
'callback' =>
function($queryBuilder, $alias, $field, $value) {
if (!$value['value']) {
return;
}
$queryBuilder
->where($alias . '. idBiggerThan(:current_user)')
->setParameter('current_user', $this->getCurrentUser()->getId());
},
'field_type' => 'checkbox'
));
Have a look on doctrine_orm_callback in the documentation: https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/filter_field_definition.html
$datagridMapper->add('title') ->add('enabled') ->add('tags', null, array(), null, array('expanded' => true, 'multiple' => true)) ->add('author') ->add('with_open_comments', 'doctrine_orm_callback', array( // 'callback' => array($this, 'getWithOpenCommentFilter'), 'callback' => function($queryBuilder, $alias, $field, $value) { if (!$value['value']) { return; } $queryBuilder->leftJoin(sprintf('%s.comments', $alias), 'c'); $queryBuilder->andWhere('c.status = :status'); $queryBuilder->setParameter('status', Comment::STATUS_MODERATE); return true; }, 'field_type' => 'checkbox' ));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.