Symfony4:预期参数类型为“Categorie”,“App\Entity\Categorie 的实例”在属性路径“categorie”处给出

问题描述 投票:0回答:2

我在

EntityType
中的
Symfony 4
类上遇到问题,我无法找到使表单行返回字符串而不是实体的方法 (
Categorie
)。这基本上是我作为表单生成器的一部分所拥有的:
ProduitType

$builder
        ->add('nom')
        ->add('prix')
        ->add('imgproduit')
        ->add('categorie', EntityType::class, [
            'class' => Categorie::class,
            'choice_label' => 'titre'
        ])
    ;

我的目标是让此字段返回所选

titre
的标题 (
Categorie
),而不是整个实体(因此,与“
choice_label
”的值完全相同)。我已经尝试将
__toString
添加到我的实体
Produit
但它是同样的问题

这是我的实体

Produit
,其中我使用
titre
中的
Categorie
作为名为
categorie

的属性
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Produit
 *
 * @ORM\Table(name="produit", uniqueConstraints={@ORM\UniqueConstraint(name="nom", columns={"nom"})}, indexes={@ORM\Index(name="fk_p_ct", columns={"categorie"})})
 * @ORM\Entity
 */
class Produit
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="nom", type="string", length=20, nullable=false)
     */
    private $nom;

    /**
     * @var float
     *
     * @ORM\Column(name="prix", type="float", precision=10, scale=0, nullable=false)
     */
    private $prix;

    /**
     * @var string
     *
     * @ORM\Column(name="imgproduit", type="string", length=200, nullable=false)
     */
    private $imgproduit;

    /**
     * @var \Categorie
     *
     * @ORM\ManyToOne(targetEntity=Categorie::class, inversedBy="categories")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="categorie", referencedColumnName="titre")
     * })
     */
    private $categorie;

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

    /**
     * @param int $id
     */
    public function setId(int $id): void
    {
        $this->id = $id;
    }

    /**
     * @return string
     */
    public function getNom(): ?string
    {
        return $this->nom;
    }

    /**
     * @param string $nom
     */
    public function setNom(string $nom): void
    {
        $this->nom = $nom;
    }

    /**
     * @return float
     */
    public function getPrix(): ?float
    {
        return $this->prix;
    }

    /**
     * @param float $prix
     */
    public function setPrix(float $prix): void
    {
        $this->prix = $prix;
    }

    /**
     * @return string
     */
    public function getImgproduit(): ?string
    {
        return $this->imgproduit;
    }

    /**
     * @param string $imgproduit
     */
    public function setImgproduit(string $imgproduit): void
    {
        $this->imgproduit = $imgproduit;
    }

    /**
     * @return \Categorie
     */
    public function getCategorie(): ?\Categorie
    {
        return $this->categorie;
    }

    /**
     * @param \Categorie $categorie
     */
    public function setCategorie(\Categorie $categorie): self
    {
        $this->categorie = $categorie;
        return $this;
    }

    public function __toString()
    {
        return $this->nom;
    }


}

我怎样才能实现我的目标?

php forms symfony4
2个回答
0
投票

仔细阅读错误,应该是“Category”,得到的是“App\Entity\Category”
它应该是“App\Entity\Category”,问题出在你的设置器上:

/**
 * @param \Categorie $categorie
 */
public function setCategorie(\Categorie $categorie): self
{
    $this->categorie = $categorie;
    return $this;
}

您将参数类型声明为来自

全局命名空间
\Categorie。您需要的是当前命名空间 (
Categorie
) 中的
App\Entity
\App\Entity\Categorie
。 解决方案是将文件中出现的 \Categorie
ALL
更改为
Categorie
\App\Entity\Categorie
,但是,尤其是在您的 setter 方法中,以解决此特定错误:

/**
 * @param Categorie $categorie
 */
public function setCategorie(Categorie $categorie): self
{
    $this->categorie = $categorie;
    return $this;
}

/**
 * @param \App\Entity\Categorie $categorie
 */
public function setCategorie(\App\Entity\Categorie $categorie): self
{
    $this->categorie = $categorie;
    return $this;
}

0
投票

SF 6.3

还有其他更简单的事情会导致此错误

如果您在实体中声明了 int 字段,但是通过函数传递字符串并尝试保存(刷新)实体。

如果在控制器中处理 $request 时出现错误 - 即 简单的错误

© www.soinside.com 2019 - 2024. All rights reserved.