Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Sonata Admin中将select with choices添加到过滤器中是最好的方法?_Php_Symfony_Sonata Admin - Fatal编程技术网

Php 如何在Sonata Admin中将select with choices添加到过滤器中是最好的方法?

Php 如何在Sonata Admin中将select with choices添加到过滤器中是最好的方法?,php,symfony,sonata-admin,Php,Symfony,Sonata Admin,如何在Sonata Admin中将select with choices添加到过滤器中是最好的方法 我可以: $builder->add('gender', 'choice', array( 'choices' => array('m' => 'Male', 'f' => 'Female'), 'required' => false, )); 但这在过滤器中不起作用 对于你的管理类,你应该使用函数来添加你的过滤器,如果你想为你的gender字

如何在Sonata Admin中将select with choices添加到过滤器中是最好的方法

我可以:

$builder->add('gender', 'choice', array(
    'choices'   => array('m' => 'Male', 'f' => 'Female'),
    'required'  => false,
));

但这在过滤器中不起作用

对于你的管理类,你应该使用函数来添加你的过滤器,如果你想为你的
gender
字段添加自定义选项,你可以使用
doctor\orm\u string
并以数组形式提供你的选项列表

$datagridMapper
       ->add('gender',
        'doctrine_orm_string',
        array(), 
       'choice',
        array('choices' => array('m' => 'Male', 'f' => 'Female')
        )
    );
试试这个:

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{

    $datagridMapper

       ->add('gender',null,  array(), ChoiceType::class, array(
           'choices' => array('m' => 'Male', 'f' => 'Female')
       ))
    ;
}

在我的版本-symfony 3.4和“sonata项目/管理包”:“^3.0”

工作方式如下:

->add('preferredLanguage', 'doctrine_orm_choice', [
                    'global_search' => true,
                    'field_type' => ChoiceType::class,
                    'field_options' => [
                        'choices' => [
                            'English' => PotentialCustomerInterface::PREFERRED_LANGUAGE_ENGLISH,
                            'Spanish' => PotentialCustomerInterface::PREFERRED_LANGUAGE_SPANISH
                        ]
                    ]
                ]
            )
这些选项是数据库中的字符串值

如果希望数据库中的选项通过某些逻辑过滤:

    ->add('csr', 'doctrine_orm_choice', [
            'field_type' => EntityType::class,

            'field_options' => [
                'class' => User::class,
                'query_builder' => function (UserRepository $userRepository) {
                    return $userRepository->qbFindAdmins();
                },
            ]

        ]
    )

在UserRepository中,只需创建返回查询生成器的方法。

我使用的是symfony 4.3sonata admin bundle 3.0,我就是这样做的:

use Sonata\DoctrineORMAdminBundle\Filter\StringFilter;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

/**
 * @param DatagridMapper $datagridMapper
 */
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper
        ->add('gender', StringFilter::class, ['label' => 'Gender'], ChoiceType::class, [
            'choices' => ['m' => 'Male', 'f' => 'Female']
        ])
    ;
}

您能否强调哪些部分起到了作用,以及它们为什么有助于解决问题,以便其他人可以从该片段中学习?