类型为“整数或空”的Symfony预期参数。 [英] Symfony Expected argument of type "integer or null"
问题描述
我遇到了一个问题,但不知道如何解决。我正在为网站上的类别进行CRUD。
I'm having an issue, and I don't know how to fix it. I'm doing a CRUD for categories on a webiste.
我们可以有2种类别, categorieParent
和每个类别<都有一个
categorieParent
。
We can Have 2 types of Categories, categorieParent
and each Categorie
having one categorieParent
.
可以使用 make:form
进行CRUD,但是当我提交表单时,会出现以下错误:
I've mae the CRUD with the make:form
But when I submit the form the following error appear :
类型为整数或null的预期参数,在属性路径
categorie_parent_id处给出的
App\Entity\CategorieParent。
Expected argument of type "integer or null", "App\Entity\CategorieParent" given at property path "categorie_parent_id".
这是我的实体:
类别
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\CategorieRepository")
*/
class Categorie
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $categorie_intitule;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\CategorieParent", inversedBy="categorie_id")
*/
private $categorie_parent_id;
public function __construct()
{
$this->categorie_id = new ArrayCollection();
$this->categorie_id_1 = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getCategorieIntitule(): ?string
{
return $this->categorie_intitule;
}
public function setCategorieIntitule(string $categorie_intitule): self
{
$this->categorie_intitule = $categorie_intitule;
return $this;
}
/**
* @return Collection|Produit[]
*/
public function getCategorieId1(): Collection
{
return $this->categorie_id_1;
}
public function addCategorieId1(Produit $categorieId1): self
{
if (!$this->categorie_id_1->contains($categorieId1)) {
$this->categorie_id_1[] = $categorieId1;
$categorieId1->setCategorieId1($this);
}
return $this;
}
public function removeCategorieId1(Produit $categorieId1): self
{
if ($this->categorie_id_1->contains($categorieId1)) {
$this->categorie_id_1->removeElement($categorieId1);
// set the owning side to null (unless already changed)
if ($categorieId1->getCategorieId1() === $this) {
$categorieId1->setCategorieId1(null);
}
}
return $this;
}
public function getCategorieParentId(): ?int
{
return $this->categorie_parent_id;
}
public function setCategorieParentId(?int $categorie_parent_id): self
{
$this->categorie_parent_id = $categorie_parent_id;
return $this;
}
public function addCategorieParentId(self $categorieParentId): self
{
if (!$this->categorie_parent_id->contains($categorieParentId)) {
$this->categorie_parent_id[] = $categorieParentId;
}
return $this;
}
public function removeCategorieParentId(self $categorieParentId): self
{
if ($this->categorie_parent_id->contains($categorieParentId)) {
$this->categorie_parent_id->removeElement($categorieParentId);
}
return $this;
}
}
** categorieParent **
**categorieParent **
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\CategorieParentRepository")
*/
class CategorieParent
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $categorie_intitule;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Categorie", mappedBy="categorie_parent_id")
*/
private $categorie_id;
public function __construct()
{
$this->categorie_id = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getCategorieIntitule(): ?string
{
return $this->categorie_intitule;
}
public function setCategorieIntitule(string $categorie_intitule): self
{
$this->categorie_intitule = $categorie_intitule;
return $this;
}
/**
* @return Collection|Categorie[]
*/
public function getCategorieId(): Collection
{
return $this->categorie_id;
}
public function addCategorieId(Categorie $categorieId): self
{
if (!$this->categorie_id->contains($categorieId)) {
$this->categorie_id[] = $categorieId;
$categorieId->setCategorieParentId($this);
}
return $this;
}
public function removeCategorieId(Categorie $categorieId): self
{
if ($this->categorie_id->contains($categorieId)) {
$this->categorie_id->removeElement($categorieId);
// set the owning side to null (unless already changed)
if ($categorieId->getCategorieParentId() === $this) {
$categorieId->setCategorieParentId(null);
}
}
return $this;
}
public function __toString()
{
return $this->categorie_intitule;
}
}
您能解释一下我错了吗?非常感谢。
Can you explain me what i get wrong ? Thanks a lot.
推荐答案
查看此部分:
/**
* @ORM\ManyToOne(targetEntity="App\Entity\CategorieParent", inversedBy="categorie_id")
*/
private $categorie_parent_id;
虽然属性名称为 categorie_parent_id
,不会返回ID。主义将这一领域水化为物体。它将返回一个 CategorieParent
对象(或 null
)。考虑删除此属性名称的 _id
部分,因为它不包含整数,而是一个对象。
While your attribute name is categorie_parent_id
, it won't return an ID. Doctrine hydrates this field into an object. It will return a CategorieParent
object (or null
) instead. Consider removing the _id
part of this attribute name, because it doesn't hold an integer but an object.
更新您的方法:
public function getCategorieParentId(): ?CategorieParent
{
return $this->categorie_parent_id;
}
public function setCategorieParentId(?CategorieParent $categorie_parent_id): self
{
$this->categorie_parent_id = $categorie_parent_id;
return $this;
}
这篇关于类型为“整数或空”的Symfony预期参数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!