Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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
Symfony sonata_type_模型字段的自定义选项列表,带有sonata Admin_Symfony_Sonata Admin - Fatal编程技术网

Symfony sonata_type_模型字段的自定义选项列表,带有sonata Admin

Symfony sonata_type_模型字段的自定义选项列表,带有sonata Admin,symfony,sonata-admin,Symfony,Sonata Admin,我正在使用Sonata Admin,我有一个类别字段,我需要按顺序显示它们,就像选择中的树一样: <select> <option>Category father-1</option> <option>--Category child-1-1</option> <option>--Category child-1-2</option> <option>--Categor

我正在使用Sonata Admin,我有一个类别字段,我需要按顺序显示它们,就像选择中的树一样:

<select>
    <option>Category father-1</option>
    <option>--Category child-1-1</option>
    <option>--Category child-1-2</option>
    <option>--Category child-1-3</option>
    <option>----Category child-1-3-1</option>
    <option>----Category child-1-3-2</option>
    <option>--Category child-1-4</option>
    <option>--...</option>
    <option>Category father-2</option>
</select>
这显示了错误:

The option "choice_list" with value "Array" is expected to be of type "null", "Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface"
我不确定是否必须使用字段类型“sonata\u type\u model”或“choice”

试试:

->add('category', 'entity', array(
        'class'    => 'Acme\Entity\Category',
)
仅当您有实体
类别
时,此选项才有效


请参阅关于为SonataAdminBundle的
类别
实体创建树编辑器。是俄文的同一篇文章,但在第一个变体中包含缺失的代码。

好的,我已经在树中排序了类别列表,将其包含在相关实体中,如下所示:

protected function configureFormFields(FormMapper $formMapper)
{
    $em = $this->modelManager->getEntityManager('MyBundle\Entity\Category');

    $query = $em->createQueryBuilder('c')
                ->select('c')
                ->from('MyBundle:Category', 'c')
                ->where('c.parent IS NOT NULL')
                ->orderBy('c.root, c.lft', 'ASC');

    $formMapper
        ...
        ->add('categoria', 'sonata_type_model', array(
            'required' => true, 
            'query' => $query
        ))
        ...
    ; 
}

我希望它能帮助某人

在阅读了上述答案后,我必须执行以下操作才能获得OP所需的功能:

protected function configureFormFields(FormMapper $formMapper)
{

$em = $this->modelManager->getEntityManager('YourBundleFile\YourBundleFileBundle\Entity\YourEntity');

$qb = $em->createQueryBuilder();

$qb = $qb->add('select', 'u')
        ->add('from', 'YourBundleFile\YourBundleFileBundle\Entity\YourEntity u');

$query = $qb->getQuery();
$arrayType = $query->getArrayResult();

$formMapper
->add('yourProperty', 'choice', array('choices'=>$arrayType))
-end();
}

我不得不使用
companybundle:Category
。您好,您是否尝试过使用required false?我得到了并且一直得到不能设置空值,除了Categoria和得到了空值,谢谢找到我的解决方案。如果您希望它是“必需的”=>false。setCategory(Category$Category=null),因此提交表单时不会将值设置为null并抛出错误。
protected function configureFormFields(FormMapper $formMapper)
{

$em = $this->modelManager->getEntityManager('YourBundleFile\YourBundleFileBundle\Entity\YourEntity');

$qb = $em->createQueryBuilder();

$qb = $qb->add('select', 'u')
        ->add('from', 'YourBundleFile\YourBundleFileBundle\Entity\YourEntity u');

$query = $qb->getQuery();
$arrayType = $query->getArrayResult();

$formMapper
->add('yourProperty', 'choice', array('choices'=>$arrayType))
-end();
}