【问题标题】:Expected argument of type "string", "App\Entity\Mets" given at property path "nomPlat"属性路径“nomPlat”中给出的“字符串”、“App\Entity\Mets”类型的预期参数
【发布时间】:2019-06-16 23:19:56
【问题描述】:

我需要在我的表单中获取一个值(EntityType,因为我需要在我的值 nomPlat 上选择字段)但我收到此错误 属性路径“nomPlat”中给出的“字符串”类型、“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(),


        ]);
    }
}

【问题讨论】:

    标签: symfony4


    【解决方案1】:

    我认为您需要在您的 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

    【讨论】:

    • 感谢您的快速回答,我添加了choiceValue,但我仍然遇到同样的问题
    猜你喜欢
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 2020-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多