Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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 使用easyadmin的Symfony,尝试在数据库的下拉列表中显示值而不是索引_Php_Symfony_Dropdown_Easyadmin - Fatal编程技术网

Php 使用easyadmin的Symfony,尝试在数据库的下拉列表中显示值而不是索引

Php 使用easyadmin的Symfony,尝试在数据库的下拉列表中显示值而不是索引,php,symfony,dropdown,easyadmin,Php,Symfony,Dropdown,Easyadmin,我正在使用easyadmin 3在symfony 5下工作,我正在尝试创建一个包含来自数据库的类别下拉列表的表单,直到这里它工作正常,但当我打开表单时,类别显示为其索引,而不是其名称。。。事实上,我有一个choicefield,它也使用数组,它实际上显示内容的索引,而不是它们的名称,我将向您展示 #src\Repository\CategorieRepository.php /** * @return Categorie[] */

我正在使用easyadmin 3在symfony 5下工作,我正在尝试创建一个包含来自数据库的类别下拉列表的表单,直到这里它工作正常,但当我打开表单时,类别显示为其索引,而不是其名称。。。事实上,我有一个choicefield,它也使用数组,它实际上显示内容的索引,而不是它们的名称,我将向您展示

#src\Repository\CategorieRepository.php
        /**
         * @return Categorie[]
         */
        public function findAll(){
            return $this->createQueryBuilder('c')
                ->orderBy('c.titre', 'ASC')
                ->getQuery()
                ->getResult();
        }
    
#src\Controller\Admin\ExerciceCrudController.php
        public function getCategBdd()
        {
            $categ = $this->getDoctrine()
            ->getRepository(Categorie::class);
            return $tab = array($categ->findAll());
        }
    
    public function configureFields(string $pageName): iterable
        {
            $tab = $this->getCategBdd();
      
            $fields = [
                    ChoiceField::new('main')
                    ->setLabel('Main')
                    ->setChoices(['Gaucher','Droitier'])
                    ->renderExpanded(true)
                    ->allowMultipleChoices(true),
                ChoiceField::new('categorie')
                     ->setLabel('Categorie')
                     ->renderAsNativeWidget(true)
                     ->setChoices($tab),
     ];
       
            return $fields;
        
        }

我听说过entity type可以这样做,但我真的不知道我可以用什么以及如何做,所以如果有人能帮我的话,谢谢。

在您使用的entity类中,您想添加一个
\u toString()
方法并返回name属性

比如说

public function __toString()
{
    return $this->getName();
}

不幸的是,我已经创建了一个tostring函数,这就是为什么我不明白为什么它在选项卡中按类别的索引显示类别