Create two entities: Item
and Category
:
<?php
namespace YourApplication\Bundle\DatabaseBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="Categories")
*/
class Category
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="Item", inversedBy="categories")
* @ORM\JoinTable(name="ItemsToCategories")
*/
protected $items;
public function __construct() {
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
}
}
<?php
namespace YourApplication\Bundle\DatabaseBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="Items")
*/
class Item
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="Category", mappedBy="items")
*/
protected $items;
public function __construct() {
$this->items = new \Doctrine\Common\Collections\ArrayCollection();
}
}
Since a category owns the items and the items are owned by the categories, Category
will be the owning side and Item
will be the inverse side of the relationship.