fala
fala

Reputation: 271

How can I connect my entity field to the same entity?

I added to my entity "Category" a field "parentcategory" to be able to connect a category to another category:

class Category
{ 

 /**
  * @ORM\Id()
  * @ORM\GeneratedValue()
  * @ORM\Column(type="integer")
  */
  private $id;


  /**
  * @ORM\ManyToOne(targetEntity="Category")
  * @ORM\JoinColumn(name="parentcategory", referencedColumnName="id")
  *
  */
  private $parentcategory;



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

  public function getParentcategory(): ?Parentcategory {
    return $this->parentcategory;
  }

  public function setParentcategory(?Parentcategory $parentcategory): self {
    $this->parentcategory = $parentcategory;

    return $this;
  }

I get the error message:

The return type of method "getParentcategory" in class "App\Entity\Category" is invalid.

Upvotes: 0

Views: 47

Answers (2)

Youssef Saoubou
Youssef Saoubou

Reputation: 591

It's actually your setter that is causing issueyou need to set the exact class

public function setParentcategory(?Category $category): self {
$this->parentcategory = $category;

return $this;

Upvotes: 1

Ihor Kostrov
Ihor Kostrov

Reputation: 2561

Change

  public function getParentcategory(): ?Parentcategory {
    return $this->parentcategory;
  }

  public function setParentcategory(?Parentcategory $parentcategory): self  {
    $this->parentcategory = $parentcategory;

    return $this;
  }

to

  public function getParentcategory(): ?Category {
    return $this->parentcategory;
  }

  public function setParentcategory(?Category $parentcategory): self  {
    $this->parentcategory = $parentcategory;

    return $this;
  }

Because in your case return type is invalid class

Upvotes: 1

Related Questions