1

How can i create a relationship between entities with Symfony 2 and Doctrine? I'm only able to create standalone entities. Maybe someone can help me figure this out using the entity generator? I want to:

  • Create two entities: Post and Category. A Post is part of a Category.
  • Create a Tag entity: A Post can have many Tags.

2 Answers 2

3

A practical example is covered in Symfony2 docs here:

http://symfony.com/doc/current/book/doctrine.html#entity-relationships-associations

To elaborate, taking the first example, you need to create a OneToMany relationship between your Category object and your Post object:

Category.php:

<?php namespace Your\CustomBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; /** * @ORM\Table(name="category") * @ORM\Entity() */ class Category { /** * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\OneToMany(targetEntity="Post", mappedBy="category") */ public $posts; /** * Constructor */ public function __construct() { $this->posts = new ArrayCollection(); } /** * @return integer */ public function getId() { return $this->id; } } 

Post.php

<?php namespace Your\CustomBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table(name="post") * @ORM\Entity() */ class Post { /** * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @ORM\ManyToOne(targetEntity="Category", inversedBy="posts") */ public $category; /** * @return integer */ public function getId() { return $this->id; } } 

This should get you started. I've just written this so there might be errors :s

I'm making properties $posts and $category public here for brevity; however you'd probably be advised to make these private and add setters/getters to your classes.

Also note that $posts is an array-like Doctrine ArrayObject class especially for arrgregating entities, with methods like $category->posts->add($post) etc.

For more detail look into association mapping in the Doctrine documentation. You'll probably need to set up a ManyToMany relationship between Posts and Tags.

Hope this helps :)

Sign up to request clarification or add additional context in comments.

2 Comments

Why do you relate back from Categories to Posts? One relation wouldnt be enough?
It's just a bi-directional relationship; you don't have to define the OneToMany annotation if you didn't need it. But it would be useful if, for example, you wanted to show all posts related to a Category etc.
0

You don't create the relationships with the entity generator itself.

Once the entity classes themselves exist (created either with the entity generator or written by hand), you then edit them to add the relationships.

For example, with your Post having many Tags example

namespace Your\Bundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * Your\Bundle\Entity\Post * * @ORM\Table(name="post") * @ORM\Entity */ class Post { /** * @var \Doctrine\ORM\PersistentCollection * * @ORM\OneToMany(targetEntity="Tag", mappedBy="post", cascade={"persist"}) */ private $tags; } 

See Doctrine's Documentation for more information about specifying relationships.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.