Monday, January 31, 2022

[FIXED] How can I connect my entity field to the same entity?

Issue

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.


Solution

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



Answered By - Ihor Kostrov

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.