Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Persistence in PHP with Doctrine ORM

You're reading from  Persistence in PHP with Doctrine ORM

Product type Book
Published in Dec 2013
Publisher Packt
ISBN-13 9781782164104
Pages 114 pages
Edition 1st Edition
Languages
Author (1):
Kevin Dunglas Kevin Dunglas
Author Profile Icon Kevin Dunglas
Kevin Dunglas
Toc

Understanding the @ManyToMany annotation with tags


Tags group posts by topics. A tag contains several posts, and a post has several tags. This is a Many-To-Many bidirectional association. Doctrine manages transparently the association table needed to store Many-To-Many relations at the SQL level. The MySQL schema that will be generated is shown in the following screenshot:

Creating the Tag entity class (inverse side)

The Tag entity class has only two properties:

  • name: This is the name of the tag, it is unique, and is the identifier of the entity

  • posts: This is the collection of posts associated with this tag

The following are the steps to create the Tag entity class:

  1. Create a Tag.php file in the src/Blog/Entity/ location that contains the entity class using the following code snippet:

    <?php
    
    namespace Blog\Entity;
    
    use Doctrine\Common\Collections\ArrayCollection;
    use Doctrine\ORM\Mapping\Entity;
    use Doctrine\ORM\Mapping\Column;
    use Doctrine\ORM\Mapping\Id;
    use Doctrine\ORM\Mapping\ManyToMany...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime