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 :)