Skip to content
Advertisement

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
{ 

 /**
  * @ORMId()
  * @ORMGeneratedValue()
  * @ORMColumn(type="integer")
  */
  private $id;


  /**
  * @ORMManyToOne(targetEntity="Category")
  * @ORMJoinColumn(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 “AppEntityCategory” is invalid.

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement