【问题标题】:Symfony with easyadmin, trying to display value instead of index in a dropdown from a database带有easyadmin的Symfony,试图在数据库的下拉列表中显示值而不是索引
【发布时间】:2021-08-02 19:56:46
【问题描述】:

我在 symfony 5 和 easyadmin 3 下工作,我正在尝试创建一个表单,其中包含来自数据库的类别下拉列表,直到这里它运行良好,但是当我打开表单时,类别与他们一起显示索引而不是他们的名字......事实上我有一个使用数组的选择字段,它实际上显示内容的索引而不是他们的名字,我会告诉你

#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;
        
        }

我听说过用于执行此操作的实体类型,但我真的不明白我该怎么做,如果有人可以帮助我,非常感谢。

【问题讨论】:

    标签: php symfony dropdown easyadmin


    【解决方案1】:

    在您正在使用的 Entity 类中,您想添加一个 __toString() 方法并返回 name 属性。

    例如

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

    【讨论】:

    • 不幸的是,我已经创建了一个 tostring 函数,这就是为什么我不明白为什么它在选项卡中按索引显示类别
    猜你喜欢
    • 2011-09-02
    • 2016-07-06
    • 2018-11-04
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    相关资源
    最近更新 更多