1 / 17

Symfony2 - Step-by-step

Symfony2 - Step-by-step. Doctrine ORM Relationship OneToOne ManyToOne OneToMany ManyToMany Self-referencing. Entities. OneToOne. “Blog” vs “Blog Setting” 1 blog has only 1 setting 1 setting only belong to 1 blog. /** @Entity **/ class Blog { // ... /**

Download Presentation

Symfony2 - Step-by-step

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