如何在 SonataAdmin 中创建自定义 DataGrid 过滤器 [英] How can I create a custom DataGrid filter in SonataAdmin

查看:31
本文介绍了如何在 SonataAdmin 中创建自定义 DataGrid 过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an entity Transaction with numerous status codes. I want the user to be able to see these status codes as strings in SonataAdmin. The user should also be able to filter on the basis of these status codes.

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";
        }
    }
}

我已经这样配置了我的奏鸣曲列表映射器:

I have configured my Sonata List Mapper like this:

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

效果很好:

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

However I am unable to use the same as a Filter.

如果我试试这个:

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

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

This doesn't work. It gives the following error ->

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

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

How can I make it work?

推荐答案

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

This worked as a temporary solution for me. If anyone has a better solution, please share.

$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",);
    }

这篇关于如何在 SonataAdmin 中创建自定义 DataGrid 过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆