peace_love
peace_love

Reputation: 6471

How can I make parameter in method valid?

In a method in my entity I am using a parameter "addlinkedDocuments".

class Documents {
  /**
  * Many Documents link to many Documents.
  * @ORM\ManyToMany(targetEntity="App\Entity\Documents", fetch="EAGER")
  * @ORM\JoinTable(name="documents_documents",
  *      joinColumns={@JoinColumn(name="link_origin", referencedColumnName="id")},
  *      inverseJoinColumns={@JoinColumn(name="link_destination", referencedColumnName="id")}
  *      )
  * @Groups("documents")
  */
  private $linkedDocuments;


  public function __construct() {
    $this->linkedDocuments = new ArrayCollection();
  }



  /**
  * @return Collection|linkedDocuments[]
  */
  public function getlinkedDocuments(): Collection
  {
    return $this->linkedDocuments;
  }

  public function addlinkedDocuments(linkedDocuments $linkedDocuments): self
  {
    if (!$this->linkedDocuments->contains($linkedDocuments)) {
      $this->linkedDocuments[] = $linkedDocuments;
    }

    return $this;
  }

  public function removelinkedDocuments(linkedDocuments $linkedDocuments): self
  {
    if ($this->linkedDocuments->contains($linkedDocuments)) {
      $this->linkedDocuments->removeElement($linkedDocuments);
    }

    return $this;
  }

But I get the error message:

The type hint of parameter "linkedDocuments" in method "addlinkedDocuments" in class "App\Entity\Documents" is invalid.

Upvotes: 1

Views: 65

Answers (1)

Bananaapple
Bananaapple

Reputation: 3114

As far as I can tell you got a self referential many to many relationship on Documents.

Any given Document can therefore be related to many other Documents.

linkedDocuments is merely the name of the variable that holds the collection of Documents.

My point is that the linked documents are not of type linkedDocuments but Documents, so your type-hint should be changed accordingly:

/**
 * @return Collection|Document[]
 */
 public function getlinkedDocuments(): Collection
 {
     return $this->linkedDocuments;
 }

 public function addlinkedDocument(Document $linkedDocument): self
 {
     if (!$this->linkedDocuments->contains($linkedDocument)) {
         $this->linkedDocuments[] = $linkedDocument;
     } 

     return $this;
 }

 public function removelinkedDocument(Document $linkedDocument): self
 {
     if ($this->linkedDocuments->contains($linkedDocument)) {
         $this->linkedDocuments->removeElement($linkedDocument);
     }

     return $this;
 }

EDIT: As per Cerad's suggestion I have gone and renamed the methods to better reflect plurality. So your class should be called Document so that any one given document can be linked to many documents.

Upvotes: 2

Related Questions