6

我有一个带有许多状态代码的实体事务。我希望用户能够在 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";
        }
    }
}

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

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 

我怎样才能让它工作?

4

2 回答 2

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",);
    }
于 2012-05-23T06:08:35.930 回答
4

类似的东西

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']);
}
于 2012-06-22T08:16:31.627 回答