简体   繁体   English

奏鸣曲管理员自定义列表字段(不是来自实体)

[英]sonata admin custom list field (not from entity)

Sonata admin bundle documentation seems scarce and I did not find a way implement this. Sonata管理包文档似乎很少,我没有找到实现这一点的方法。

Goal: display boolean value in field list. 目标:在字段列表中显示布尔值。 Value should calculated for each object from other properties. 应为其他属性中的每个对象计算值。

I managed to implement this for datagridFilter as doctrine_orm_callback but not for listFields. 我设法将datagridFilter实现为doctrine_orm_callback但不是listFields。

Working code for configureDatagridFilters() : configureDatagridFilters()工作代码:

// 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)
    ;
}

Questions 问题

  1. How would I this implement this for configureListFields() ? 我如何为configureListFields()实现这个? Tried several ways using same logic from working configureDatagridFilters() with no success. 尝试使用相同的逻辑工作configureDatagridFilters()几种方法没有成功。
  2. Is this possible without queryBuilder and DQL? 没有queryBuilder和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
}

In your admin form listing then 然后在您的管理表单列表中

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM