As to the Doctrine to do one-to-many collection?

Good day.
There is a table of tasks, which can be an unlimited number of meta records. It would seem that a typical one-to-many (one-to-many task meta-records), but when I straight it indicates, Symphony swears by the collection (says The return type of method "getMeta" is invalid). But I need to take it a collection - a lot of meta-records!
/**
 * @ORM\OneToMany(targetEntity="App\Entity\TaskMeta", mappedBy="task")
*/
private $meta;

public function getMeta() : ? \Collection
{
 return $this->meta;
}

Besides, my relationship implies uniqueness, i.e., one meta-record can have only one task, but the task can be a lot of meta-records.

How to do? ManyToOne that allows you to return a collection that are not suitable, because it involves a lot of tasks to a single meta, and this is not my case.
March 12th 20 at 08:41
1 answer
March 12th 20 at 08:43
Solution
You have potentially two errors in the above code:

1. As wasuremono type you specify \Collection, whereas the collection of Doctrine is a \Doctrine\Common\Collections\Collection
2. Your code is not creating this collection, respectively, if it is not itself a Doctrine entity created when loading data from the database - value $meta - null. The correct approach is either to initialize collections in the constructor:
public function __construct() {
 $this->meta = new ArrayCollection(); // where ArrayCollection is a \Doctrine\Common\Collections\ArrayCollection
}

either directly in the method:
public function getMeta() : Collection
{
 if (!$this->meta) {
 $this->meta = new ArrayCollection();
}
 return $this->meta;
}

Find more questions by tags Doctrine ORMSymfony