1

I'm having an issue, and I don't know how to fix it. I'm doing a CRUD for categories on a webiste.

We can Have 2 types of Categories, categorieParent and each Categoriehaving one categorieParent.

I've mae the CRUD with the make:form But when I submit the form the following error appear :

Expected argument of type "integer or null", "App\Entity\CategorieParent" given at property path "categorie_parent_id".

Here are my ENTITY :

Categorie

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

}


Can you explain me what i get wrong ? Thanks a lot.

1 Answer 1

3

Look at this part:

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\CategorieParent", inversedBy="categorie_id")
 */
private $categorie_parent_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.

Update your methods:

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I've tried to update my method as you said but the error is still here.
I've updated my answer, I forgot a method. This is just an example of two methods; please check your other methods as well for this mistake (integer versus objects).
Thanks I haven't thnking about updates the set method too. Thank you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.