奏鸣曲管理员自定义列表字段

奏鸣曲管理员自定义列表字段

本文介绍了奏鸣曲管理员自定义列表字段(不是来自实体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sonata 管理包文档似乎很少,我没有找到实现它的方法.

Sonata admin bundle documentation seems scarce and I did not find a way implement this.

我设法将 datagridFilter 实现为 doctrine_orm_callback 而不是 listFields.

I managed to implement this for datagridFilter as doctrine_orm_callback but not for listFields.

// LicenceAdmin.php
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper->add('isValid', 'doctrine_orm_callback', [
            'callback' => [$this, 'isValidFilterCallback'],
            'field_type' => 'checkbox',
        ]);
}
public function isValidFilterCallback($queryBuilder, $alias, $field, $value)
{
    // if no value or value == false means unchecked checkbox - show all instances
    if (!$value || empty($value['value'])) {
        return;
    }
    // if checked, display only by active logic
    $dateNow = new \DateTime();
    $queryBuilder
        ->andWhere("{$alias}.isActive = 1")
        ->andWhere("{$alias}.validFrom <= :date")
        ->andWhere("{$alias}.validTo > :date")
        ->setParameter('date', $dateNow)
    ;
}

问题

  1. 我将如何为 configureListFields() 实现这个?使用与工作 configureDatagridFilters() 相同的逻辑尝试了多种方法,但均未成功.
  2. 如果没有 queryBuilder 和 DQL,这可能吗?我宁愿将实体对象及其属性用于逻辑.类似的东西:

  1. How would I this implement this for configureListFields()? Tried several ways using same logic from working configureDatagridFilters() with no success.
  2. Is this possible without queryBuilder and DQL? I would rather use entity object and its properties for logic. Something like:

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper->add('isValid', 'callback', [
        'callback' => function($object) { <-- IMAGINARY FUNCTIONALITY
            if ($object->getIsValid()) return true;
            else return false;
        }
    ]);
}

推荐答案

我相信答案比您想要的更容易,除非我没有得到您想要的.

I believe the answer is easier than what you are looking for, unless I did not get what you are after.

在您的实体中,创建以下方法

In your entity, create the following method

public function isValid()
{
// do your business logic here
}

在您的管理表单列表中

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper->add('isValid', 'boolean');
}

希望这会有所帮助.

这篇关于奏鸣曲管理员自定义列表字段(不是来自实体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 19:27