2014-02-19 59 views
0

我需要在Doctrine2下表型号:这些表格之间建议的学说关系是什么?

  • 表1.用品(产品)
  • 表2.分类
  • 表3 ItemsToCategories(ID,ITEM_ID,CATEGORY_ID)

一个项目可以有很多种类。一个类别可以有很多项目。重组数据(当前)不是一种选择,我没有设计模式。

建议使用连接表建立多对多关联的建议方式是什么?该documentation似乎并没有覆盖这个确切的情况下,我渴望从别人谁做了类似的东西与Doctrine2和/或Symfony的(2.4)

回答

1

创建两个实体听到:ItemCategory

<?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将是关系的另一方。

相关问题