"App\Entity\Personnes"、"array" 类型的预期参数在 属性 路径 "professeurs" 处给出

Expected argument of type "App\Entity\Personnes", "array" given at property path "professeurs"

我的“handlerequest”出现异常,它告诉我“App\Entity\Personnes 类型的预期参数”,属性 路径“professeurs”处给出的“数组”。 我已经坚持了好几天了。

我的实体用户:

#[ORM\ManyToMany(targetEntity: Reunion::class, mappedBy: 'professeurs')]
private $reunions;

public function __construct()
{
    $this->evenements = new ArrayCollection();
    $this->participations = new ArrayCollection();
    $this->reunions = new ArrayCollection();
}

我的会议实体:

#[ORM\ManyToMany(targetEntity: Personnes::class, inversedBy: 'reunions')]
private $professeurs;

public function __construct()
{
    $this->professeurs = new ArrayCollection();
    $this->sujets = new ArrayCollection();
}

我的控制器:

#[Route('/professeur/reunion/create', name: 'create_reunion')]
public function create_reunion(Request $request, EntityManagerInterface $em): Response
{
    $reunion = new Reunion;
    $form = $this->createForm(ReunionType::class, $reunion);
    $form->handleRequest($request);

    if ($form->isSubmitted()) {
        $reunion->addProfesseur($this->getUser());
        $em->persist($reunion);
        $em->flush();
    }

    return $this->render('reunion/create.html.twig', [
        'formView' => $form->createView()
    ]);
}

我的表格:

class ReunionType extends AbstractType{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
    $builder
        ->add('dateReunion', DateTimeType::class)
        ->add('objet', TextType::class, [
            'attr' => [
                'placeholder' => 'Objet de la réunion'
            ]
        ])
        ->add('professeurs', CollectionType::class, [
            'label' => "Ajouter des professeurs",
            'entry_type' => ProfType::class,
            'by_reference' => false,
            "allow_add" => true,
            "allow_delete" => true
        ]);
}

public function configureOptions(OptionsResolver $resolver): void
{
    // $resolver->setDefaults([
    //     'data_class' => Reunion::class,
    // ]);
}
}

对于 'entry_type' :

class ProfType extends AbstractType{

public function __construct(protected PersonnesRepository $pr)
{
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
    $builder
        ->add('personnes', EntityType::class, [
            'label' => false,
            'class' => Personnes::class,
            'placeholder' => "-- Choisir un professeur --",
            'attr' => [
                'class' => "form-select"
            ],
            'choices' => $this->pr->findByRole('PROF'),
            'choice_label' =>  function (Personnes $personnes) {
                return strtoupper($personnes->getNom()) . " " . $personnes->getPrenom();
            }
        ]);
}

public function configureOptions(OptionsResolver $resolver): void
{
    // $resolver->setDefaults([
    //     'data_class' => Personnes::class,
    // ]);
}
}

通常我的“教授”collection 可以接收 1 个或多个实体。

对于 MenyToMeny 关系,使用带有选项 multiple=true

EntityType

你的 ReunionType 应该是这样的

class ReunionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('dateReunion', DateTimeType::class)
            ->add('objet', TextType::class, [
                'attr' => [
                    'placeholder' => 'Objet de la réunion'
                ]
            ])
            ->add('professeurs', EntityType::class, [
                'label' => "Ajouter des professeurs",
                'class' => Personnes::class,
                'multiple' => true
            ]);

    }

    public function configureOptions(OptionsResolver $resolver): void
    {
         $resolver->setDefaults([
             'data_class' => Reunion::class,
         ]);
    }
}

添加实体Personnes

 public function __toString(): string
 {
    return  $this->getPrenom() .' '. $this->getNom();
 }

在文件 templates/reunion/create.html.twig 中从 form_end

中删除选项 render_rest