在属性路径“ nomPlat”中给出的类型为“字符串”,“ App \ Entity \ Mets”的预期参数

时间:2019-06-16 23:19:56

标签: symfony4

我需要获取表单中的值(EntityType,因为我需要在值nomPlat上选择字段),但出现此错误 在属性路径“ nomPlat”处给出类型为“ string”,“ App \ Entity \ Mets”的预期参数。

这是我的项目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;
}


}

EntityType我的表单

<?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(),


        ]);
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您需要在choice_value方法中添加一个MetsEtVinsType::buildForm()选项。您的错误来自以下事实:您正在将字符串字段的值设置为实体。看来您想从实体的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