Symfony 4中CollectionType表单的问题

问题描述

我在Symfony 4中遇到Collectiontype问题。 我尝试将一些信息保存在数据库的许多表中。 我保存的级联是:1个releve包含1个carriere包含很多infoCarriere包含很多点 为了保存此信息,我将特定形式与collectionType一起使用: 我的CarriereRelaiType:

public function buildForm(FormBuilderInterface $builder,array $options)
    {
        $builder
            ->add("caisses",CollectionType::class,[
                'entry_type' => CarriereType::class,'allow_add' => true,'allow_delete' => true,'attr' => ['class' => 'col-sm-2']
            ]);

    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => null,'empty_data' => null
        ]);
    }

我的InfoCarriereRelaiType:

public function buildForm(FormBuilderInterface $builder,array $options)
    {
        $builder
            ->add("infoCarrieres",[
                'entry_type' => InfoCarriere::class,'empty_data' => null
        ]);

    }

我的观点也一样。 显示我的控制器的一部分:

// creation carriere
$recupCaisse = [];
        foreach ($dispatch as $keyDispatch => $valueDispatch){
            if ($keyDispatch != "dateDoc"){
                $recupCaisse[][$keyDispatch] = $valueDispatch;
            }
        }
        
        $compteurCaisse = count($recupCaisse);
        for ($i=0; $i<$compteurCaisse; $i++){
            // creation de Carriere
            $dataCarriere["caisses"][$i] = new Carriere();
            foreach ($recupCaisse[$i] as $keyCaisse => $valueCaisse){
                // recuperation des noms de caisse
                $caisse = $caisseRepository->findByAcronyme(strtoupper($keyCaisse));
            }
            $dataCarriere["caisses"][$i]->setCaisse($caisse[0]);
            // recuperation des numeros 
            foreach ($valueCaisse as $keyDetail => $valueDetail) {
                if ($keyDetail == "numero") {
                    $dataCarriere["caisses"][$i]->setNumero($valueDetail);
                }
            }
        }
       dump($dataCarriere);
        $formCarriere = $this->createForm(CarriereRelaiType::class,$dataCarriere);
        $formCarriere→handleRequest($request);


for ($i=0; $i<$compteurCaisse; $i++){
            foreach ($recupCaisse[$i] as $keyDetail => $valueDetail){
                // ajout infocarriere
                foreach ($valueDetail as $keyTempInfoCarriere => $valueTempoInfoCarriere){
                    $dataInfoCarriere[$keyDetail][$keyTempInfoCarriere] = new InfoCarriere();
                    if (is_array($valueTempoInfoCarriere)){
                        foreach ($valueTempoInfoCarriere as $key => $value){
                            if ($key != "numeroDossier"){
                                if ($key == 'annee'){
                                    $dataInfoCarriere[$keyDetail][$keyTempInfoCarriere]->setAnnee($value);
                                }elseif ($key == 'debut'){
                                    $dataInfoCarriere[$keyDetail][$keyTempInfoCarriere]->setDebut($value);
                                }elseif ($key == 'fin'){
                                    $dataInfoCarriere[$keyDetail][$keyTempInfoCarriere]->setFin($value);
                                }elseif ($key == 'naturePeriode'){
                                    $dataInfoCarriere[$keyDetail][$keyTempInfoCarriere]->setStatus($value);
                                }

                            }
                        }
                    }
                    }
            }
        }
dump($dataInfoCarriere);
$formInfoCarriere = $this->createForm(InfoCarriereRelaiType::class,$dataInfoCarriere);
$formInfoCarriere->handleRequest($request);
        

与树枝一起显示在表单上并保存Carriere是可以的。 我想显示所有信息的表格,但是却显示以下错误消息:

Could not load type "App\Entity\InfoCarriere": class does not implement "Symfony\Component\Form\FormTypeInterface".

在我的转储($ dataCarriere和$ dataInfoCarriere)上,我具有相同的结构,但是在$ dataCarriere中,我只有1个条目:

array:1 [▼
  "caisses" => array:4 [▼
    0 => App\Entity\Carriere {#1154 ▶}
    1 => App\Entity\Carriere {#1801 ▶}
    2 => App\Entity\Carriere {#1860 ▶}
    3 => App\Entity\Carriere {#1891 ▶}
  ]
]

,在$ dataInfoCarriere中,我有4个条目:

array:4 [▼
  "truc" => array:35 [▶]
  "bidule" => array:14 [▶]
  "machin" => array:8 [▶]
  "chose" => array:23 [▶]
]

(1个条目的一部分)

array:4 [▼
  "truc" => array:35 [▼
    11 => App\Entity\InfoCarriere {#1943 ▶}
    12 => App\Entity\InfoCarriere {#2090 ▶}
    13 => App\Entity\InfoCarriere {#2105 ▶}
    14 => App\Entity\InfoCarriere {#2107 ▶}
    15 => App\Entity\InfoCarriere {#2326 ▶}
    16 => App\Entity\InfoCarriere {#2398 ▶}
    17 => App\Entity\InfoCarriere {#2401 ▶}
    18 => App\Entity\InfoCarriere {#2403 ▶}

我试图只用一个,但相同的错误消息来更改$ dataInfoCarriere条目。 我想我忘记了一些东西,但我不知道是什么。 预先感谢您的帮助

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...