2

I am currently using Sonata Admin to generate a datagrid with an entity having a read boolean field. I would like to filter on this property, setting it by default to false.

So, I added the following to my Admin class:

protected $datagridValues = array(
    'read' => array('value' => false),
);

Yet, it does not seem to work. The generated select list is the following:

<select id="filter_read_value" name="filter[read][value]" class="span8">
    <option value=""></option>
    <option value="1">oui</option>
    <option value="2">non</option>
</select>

I suppose this is normal, as value for false would be 0, which is the empty option.

So, I used some constants such as:

const STATUS_READ = 1;
const STATUS_UNREAD = 2;

It works, but I am wondering if there is any proper solution to avoid these two unnecessary constants?

4

3 回答 3

4

您可以使用 getFilterParameters 可能:

<?php
public function getFilterParameters()
{
    $this->datagridValues = array_merge(array(
            'booleanField' => array(
                'value' => '0',
            )
        ),
        $this->datagridValues

    );
    return parent::getFilterParameters();
}
于 2013-08-20T08:32:17.470 回答
0

关于如何将枚举持久化到数据库中的信息有点太少了,但是将值常量与类型类中的可能选项一起存储是非常标准的。只要您从不将您的选项引用为整数,它就没有错。

于 2013-08-17T12:28:02.967 回答
0

最好的解决方案是使用 sonata-admin 的类型:

<?php
protected $datagridValues = [
    'read' => [
        'type' => Sonata\CoreBundle\Form\Type\EqualType::TYPE_IS_EQUAL,
        'value' => Sonata\CoreBundle\Form\Type\BooleanType::TYPE_NO,
    ]
];
于 2016-11-15T15:22:31.690 回答