A comma in the targetEntity can not list several entities - do not forget that you most likely refer to different tables in the database.
As I understand it, you want to list in the targetEntity all the entities under which you want to post comments. And as far as I can restore the context from a code snippet, this field and the annotation are in the class Comment .
Let's fantasize ...
Theoretically, you can refer to several different entities, using a single field using inheritance. Doctrine ( ORM , which Symfony uses) has various strategies for storing a class hierarchy, including one in which the entire class tree is stored in one table, which contains the union of the fields (that is, the set of all fields of all classes) and when elements of one class, only the fields declared in this class and inherited from the parents (but not the fields of the neighboring classes) are selected.
Those. there is a similar class hierarchy:
Record |-- Page |-- Post |-- News |-- Review |-- etc...
And you establish a link between the Comment class and the parent Record class. Then your annotated field will look like this:
/** * @ORM\ManyToOne(targetEntity="Record", inversedBy="comments") * @ORM\JoinColumn(name="record_id", referencedColumnName="id") */ private $record;
It should be noted that it is not necessary to fence inheritance, in the case where it should not be and store all the entities in one table - this is insane . And even if at the time of implementation, several classes will be permissible to fit under one ancestor, this does not mean that in the future there will be no need to tie comments to another entity that has nothing to do with the previous ones. Therefore, this option, clearly, is swept aside.
The reverse option with inheritance is also possible:
Comment |-- CommentToPage |-- CommentToPost |-- CommentToNews |-- CommentToReview |-- etc...
In this embodiment, the classes Page , Post , News , Review , etc. are not inherited from a single ancestor and are stored in different tables, but comment classes are separated, each of which contains comment content and publication time fields inherited from the parent Comment class and one field with reference to a specific entity from each of the child classes ( CommentToPage , etc.).
In this case, each class will have a field with a link:
class CommentToPage extends Comment{ /** * @ORM\ManyToOne(targetEntity="Page", inversedBy="comments") * @ORM\JoinColumn(name="page_id", referencedColumnName="id") */ private $page; } ... class CommentToPost extends Comment{ /** * @ORM\ManyToOne(targetEntity="Post", inversedBy="comments") * @ORM\JoinColumn(name="post_id", referencedColumnName="id") */ private $post; }
Harsh reality
In fact, looking at a huge number of projects, I am convinced that almost everyone thinks that “the game is not worth the trouble ”, and they do not bother with the inheritance and storage of several entities in one table. There are two options:
1. Storage of several links in the class of comments (the most frequent option):
class Comment{ /** * @ORM\ManyToOne(targetEntity="Page", inversedBy="comments") * @ORM\JoinColumn(name="page_id", referencedColumnName="id") */ private $page; //... /** * @ORM\ManyToOne(targetEntity="Post", inversedBy="comments") * @ORM\JoinColumn(name="post_id", referencedColumnName="id") */ private $post; }
2. It is much less common to use several classes of comments stored in different tables and naturally duplicating the fields of the contents of a comment and time.
In this case, a lot of copy-paste and frankly, you have to knock a little more on the keyboard, so the previous version is the most popular.
The essence
The main thing is to understand the essence. And the bottom line is that behind all entities, classes and annotations are real tables in the database, whose fields refer to rows in other tables and have foreign keys. Keeping this in mind and presenting the structure of tables in a database, it will be easier to create Сущности and connections between them.
Finally a few links: