sonata admin过滤器doctrine_orm_callback

时间:2017-06-06 02:52:43

标签: php sonata-admin

我有一个过滤器,可以过滤该项目的标签数量。但我不能让它工作它会给我一个错误:

  

查询返回了多行。更改查询或使用不同的结果函数,如getScalarResult()。

在我的物品实体中:

/**
 * @var integer
 *
 * @ORM\ManyToOne(targetEntity="Item", inversedBy="tags")
 */
private $clip;

在我的代码实体

->add('tag', 'doctrine_orm_callback', array(
            'label' => 'Keywords',
            'callback' => function($queryBuilder, $alias, $field, $value) {
                if (!isset($value['value'])) {
                    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->leftJoin(sprintf('%s.%s', $alias, $field), 't');
                $queryBuilder->having('COUNT(ct) '.$operator.' :count');
                $queryBuilder->groupBy('t.item');
                $queryBuilder->setParameter('count', $value['value']);
            },
            'field_type' => 'number',
        ))

在ItemAdmin的configureDatagridFilters

SELECT * FROM [Sheet1$] WHERE ([Field Name in Brackets] = SomeNumericValue)
SELECT * FROM [Sheet1$] WHERE ([Field Name in Brackets] = 'SomeTextValueInSingleQuote')
SELECT * FROM [Sheet1$] WHERE ([Field Name in Brackets] = #SomeDateInHashTags#)

0 个答案:

没有答案