Understanding the @ManyToOne and @OneToMany annotations with the comment system
Let's start with the comments. Visitors to our blog should be able to react to our posts. We have to create a new Comment
Doctrine entity type storing the reader's comments. Comment
entities will be linked to one Post
entity. One post can have many comments, and one comment is associated with a sole post.
The following E-R diagram represents the MySQL schema that will be generated using mapping information:
Creating the Comment entity class (owning side)
The Comment
entity has the following four properties:
id
: This is a unique identifier of the commentbody
: This represents the comment's textpublicationDate
: This is the date of publication of the commentpost_id
: This represents the post related to the comment
Here is the first code snippet of the Comment
entity, containing annotated properties. It must be placed in the Comment.php
file at the src/Blog/Entity/
location.
<?php namespace Blog\Entity; use Doctrine...