1 / 17

Understanding Symfony2 Relationships: OneToOne, ManyToOne, OneToMany, and ManyToMany

Dive into Symfony2's Doctrine ORM relationships through this comprehensive guide. Explore the intricacies of OneToOne, ManyToOne, OneToMany, and ManyToMany associations, using practical examples such as Blog and BlogSetting, and the relationship between Post and Category. Learn how to define self-referencing entities like categories and efficiently manage their hierarchy. This step-by-step approach helps you grasp the underlying concepts and build robust data structures for your web applications.

Télécharger la présentation

Understanding Symfony2 Relationships: OneToOne, ManyToOne, OneToMany, and ManyToMany

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Symfony2 - Step-by-step • Doctrine ORM Relationship • OneToOne • ManyToOne • OneToMany • ManyToMany • Self-referencing

  2. Entities

  3. OneToOne • “Blog” vs “Blog Setting” • 1 blog has only 1 setting • 1 setting only belong to 1 blog

  4. /** @Entity **/ class Blog { // ... /** * @OneToOne(targetEntity=“BlogSetting") * @JoinColumn(name=“setting_id", referencedColumnName="id") **/ private $setting; // ... } /** @Entity **/ class BlogSetting { // ... }

  5. Tables • Blog • BlogSetting

  6. ManyToOne and OneToMany • “Blog” vs “Post” • 1 blog has many posts • 1 post only belong to 1 blog

  7. /** @Entity **/ class Blog { // ... /** * @ORM\OneToMany(targetEntity="Post", mappedBy="blog", cascade={"remove"}) **/ private $posts; // ... } /** @Entity **/ class Post { /** * @ORM\ManyToOne(targetEntity=“Blog", inversedBy="posts“) * @ORM\JoinColumn(name="blog_id", referencedColumnName="id") **/ private $blog; }

  8. Tables • Blog • Post

  9. ManyToMany • “Post” vs “Category” • 1 post can belong to one or many categories • 1 category can have one or many posts 1. Post “Singapore tour” 2. Post category “tour”, “relax”, “picture” 3. Post “Nhatrang tour” 4. Post category “tour”

  10. /** @Entity **/ class Post { // ... /** * @ORM\ManyToMany(targetEntity="Category", inversedBy="posts") * @ORM\JoinTable(name="category_post") **/ private $categories; // ... } /** @Entity **/ class Category { /** * @ORM\ManyToMany(targetEntity="Post", mappedBy="categories") **/ private $posts; }

  11. Tables • Category • Post • Category_Post

  12. OneToMany – Self-referencing • “Category” vs “Parent Category” • 1 category has 1 parent category • 1 parent category has 1 or many children category

  13. /** @Entity **/ class Category { // ... /** * @OneToMany(targetEntity="Category", mappedBy="parent") **/ private $children; /** * @ManyToOne(targetEntity="Category", inversedBy="children") * @JoinColumn(name="parent_id", referencedColumnName="id") **/ private $parent; // ... }

  14. Tables • Category

More Related