Symfony2学说关系

时间:2016-09-14 13:44:56

标签: forms symfony doctrine-orm entity-relationship

我有3张桌子。

Product
+-----+-----------+
| id  |  name     |
+-----+-----------+
| 1   | Product 1 |
+-----+-----------+
| 2   | Product 2 |
+-----+-----------+

Categories
+-----+-----------+
| id  | name      |
+-----+-----------+
| 1   | cat 1     |
+-----+-----------+
| 2   | cat 2     |
+-----+-----------+

ProductCategories
+-----+------+------+
| id  | p_id | c_id |
+-----+------+------+
| 1   | 1    | 1    |
+-----+------+------+
| 2   | 1    | 2    |
+-----+------+------+
| 3   | 2    | 1    |
+-----+------+------+

问题:

  1. 我是否需要创建3个实体?产品,类别和产品类别
  2. 如何按类别ID获取产品,它们应包含有关其指定类别的信息?
  3. 创建新产品并从表中记录的category,category_id和product_id中分配类别:product_categories?

1 个答案:

答案 0 :(得分:3)

您只需要与ManyToMany映射关联的2个实体ProductCategory

阅读the documentation,您将在问题上找到答案。