sonata admin list filter for a virtual field like "count"

What if directly do:
$listMapper->add('TotalNumberOfComments', 'doctrine_orm_callback', array(
    'callback' => function($queryBuilder, $alias, $field, $value) {
        if (!$value || !isset($value['value']) || !isset($value['value']['type']) || !$value['value']['type']) {
            return;
        }

        $operators = array(
            NumberType::TYPE_EQUAL            => '=',
            NumberType::TYPE_GREATER_EQUAL    => '>=',
            NumberType::TYPE_GREATER_THAN     => '>',
            NumberType::TYPE_LESS_EQUAL       => '<=',
            NumberType::TYPE_LESS_THAN        => '<',
        );

        $operator = $operators[$value['value']['type']];

        $queryBuilder->andWhere('SIZE('$alias.'.comments) '.$operator.' :size');
        $queryBuilder->setParameter('size', $value['value']['value']);
    },
    'field_type' => 'sonata_type_filter_number',
))
Whereas the whole operator part should be optimized by the bundle, this case is marginal enough to deserve no more than a documentation entry. Moreover, your desired way of solving the issue introduces a serious performance issue, which is not the case using my proposal.

评论

热门帖子