如何在SonataAdmin中创建自定义DataGrid筛选器

时间:2012-04-19 08:25:57

标签: symfony symfony-sonata

我有一个包含大量状态代码的实体交易。 我希望用户能够在SonataAdmin中将这些状态代码看作字符串。用户还应该能够根据这些状态代码进行过滤。

Entity Transaction 
{
    const TRANSACTION_STATUS_WAITING = 1;
    const TRANSACTION_STATUS_PENDING = 2;
    const TRANSACTION_STATUS_CONFIRMED = 3;

   /**
     * Set status
     *
     * @param smallint $status
     */
    public function setStatus($status)
    {
        $this->status = $status;
    }

    /**
     * Get status
     *
     * @return smallint 
     */
    public function getStatus()
    {
        return $this->status;
    }

    public function getStatusAsString()
    {
        switch($this->status){
            case(self::TRANSACTION_STATUS_WAITING): return "Waiting for Merchant";
            case(self::TRANSACTION_STATUS_PENDING): return "Pending Confirmation";
            case(self::TRANSACTION_STATUS_CONFIRMED): return "Confirmed";
        }
    }
}

我已经配置了我的Sonata List Mapper:

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('id')
        ->add('statusAsString', null, array('sortable' => true, 'label' => 'Status'))
}

完美无缺:

但是我无法使用与过滤器相同的功能。

如果我试试这个:

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
        ->add('user')
        ->add('status') // Works well 
        ->add('statusAsString', null, array('label' => 'Status')) // Doesn't work: 
    ;
}

这不起作用。它给出了以下错误 - >

Notice: Undefined index: statusAsString in ..../Sonata\DoctrineORMAdminBundle\Guesser\FilterTypeGuesser.php 

我怎样才能让它发挥作用?

2 个答案:

答案 0 :(得分:26)

这对我来说是一个临时解决方案。如果有人有更好的解决方案,请分享。

$datagridMapper
       ->add('status', 'doctrine_orm_string', array(),
             'choice', array('choices' => Transaction::getStatusList())
       );

在实体

public static function getStatusList()
    {
        return array(
            self::TRANSACTION_STATUS_WAITING => "Waiting",
            self::TRANSACTION_STATUS_PENDING_CONFIRMATION => "Pending Confirmation",
            self::TRANSACTION_STATUS_CONFIRMED => "Confirmed",
            self::TRANSACTION_STATUS_PAYMENT_REQUESTED => "Payment Requested",);
    }

答案 1 :(得分:3)

像这样的东西

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
        ->add('codes', 'doctrine_orm_callback', array(
            'label'         => 'Код',
            'callback'      => array($this, 'getCodesFilter'),
            'field_type'    => 'genemu_jquerychosen',
            'field_options' => array(
                'class'     => 'Mtools\ClientBundle\Entity\Client',
                'widget'    => 'entity',
                'multiple'  => false,
            )
        );
}

public function getCodesFilter($queryBuilder, $alias, $field, $value)
{
    if (!$value) {
        return;
    }
    $queryBuilder->leftJoin(sprintf('%s.codes', $alias), 'c');
    $queryBuilder->andWhere('c.code = :code');
    $queryBuilder->setParameter('code', $value['value']);
}