Symfony预期类型为“整数或空”的参数

时间:2019-05-06 13:44:15

标签: php symfony doctrine-orm

我遇到了一个问题,我不知道如何解决。我正在对网站上的类别进行CRUD。

我们可以有2种类别,categorieParent,每种Categorie都有一种categorieParent

我已经使用make:form进行了CRUD,但是当我提交表单时,会出现以下错误:

  

类型为“整数或null”的预期参数,   属性路径中给出的“ App \ Entity \ CategorieParent”   “ categorie_parent_id”。

这是我的ENTITY:

类别

<?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 **

<?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;
    }

}


你能解释一下我错了吗?非常感谢。

1 个答案:

答案 0 :(得分:2)

看这部分:

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\CategorieParent", inversedBy="categorie_id")
 */
private $categorie_parent_id;

您的属性名称为categorie_parent_id时,它不会返回ID。主义将这一领域水化为物体。它将返回一个CategorieParent对象(或null)。考虑删除此属性名称的_id部分,因为它不包含整数,而是一个对象。

更新您的方法:

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;
}