"string"、"App\Entity\Mets" 类型的预期参数在 属性 路径 "nomPlat" 处给出

Expected argument of type "string", "App\Entity\Mets" given at property path "nomPlat"

我需要在我的表单中获取一个值(EntityType,因为我需要在我的值 nomPlat 上 select 字段)但我收到此错误 "string"、"App\Entity\Mets" 类型的预期参数在 属性 路径 "nomPlat".

处给出

这是我的项目symfony的代码

实体

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\MetsRepository")
 */
class Mets
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\Column(type="string", length=255)
 */
private $nomPlat;

/**
 * @ORM\Column(type="array", nullable=true)
 */
private $nomVin = [];

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 */
private $img;

/**
 * @ORM\Column(type="string", length=255)
 */


public function getId(): ?int
{
    return $this->id;
}

public function getNomPlat(): ?string
{
    return $this->nomPlat;
}

public function setNomPlat(string $nomPlat): self
{
    $this->nomPlat = $nomPlat;

    return $this;
}

public function getNomVin(): ?array
{
    return $this->nomVin;
}

public function setNomVin(?array $nomVin): self
{
    $this->nomVin = $nomVin;

    return $this;
}

public function getImg(): ?string
{
    return $this->img;
}

public function setImg(?string $img): self
{
    $this->img = $img;

    return $this;
}


}

实体类型我的表单

<?php

namespace App\Form;

use App\Entity\Mets;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;

use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class MetsEtVinsType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array 
     $options)
{
    $builder
        ->add('nomPlat', EntityType::class, [
            'class' => Mets::class,
            'query_builder' => function (EntityRepository $er) {

                return $er->createQueryBuilder('p')
                    ->orderBy('p.nomPlat', 'Asc');

            },
            'choice_label' => 'nomPlat',
            'placeholder' => 'Selectionnez un plat',
            'label' => 'Recherche',





        ])->add('save', SubmitType::class);
}

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

我的控制器

<?php

namespace App\Controller;

use App\Entity\Mets;
use App\Form\MetsEtVinsType;
use App\Repository\MetsRepository;
use App\Service\Pagination;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class MetEtVinsController extends AbstractController
{
 /**
 * @Route("/metsetvins/{page}", name="mets_et_vins", requirements= 
   {"page": "\d+"})
 * @param MetsRepository $repo
 * @param Pagination $pagination
 * @param Request $request
 * @param int $page
 * @return Response
 */
public function index(Pagination $pagination, Request $request, 
$page = 1)
{



    $pagination->setEntityClass(Mets::class)
        ->setPage($page)
        ->setLimit(24)
         ->setNameToOrder('nomPlat');







    $form = $this->createForm(MetsEtVinsType::class);
    $form->handleRequest($request);

当我使用 handleRequest 时出现了我的问题

    return $this->render('met_et_vins/index.html.twig', [
        'pagination' => $pagination,
        'form' => $form->createView(),


        ]);
    }
}

我认为您需要在 MetsEtVinsType::buildForm() 方法中添加一个 choice_value 选项。您的错误来自于您将字符串字段的值设置为实体。看起来您想从实体的 nomPlat 属性 中获取一个字符串值,下面的代码应该就是这样做的。

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('nomPlat', EntityType::class, [
            'class' => Mets::class,
            'query_builder' => function (EntityRepository $er) {

                return $er->createQueryBuilder('p')
                    ->orderBy('p.nomPlat', 'Asc');

            },
            'choice_label' => 'nomPlat',
            'placeholder' => 'Selectionnez un plat',
            'label' => 'Recherche',

            'choice_value' => function (Mets $entity = null) {
                return $entity ? $entity->getNomPlat() : '';
            },



        ])->add('save', SubmitType::class);
}

https://symfony.com/doc/current/reference/forms/types/entity.html#choice-value