创建两个实体听到:Item
和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();
}
}
由于类别拥有项目和按类别拥有的项目,Category
将拥有方和Item
将是关系的另一方。