简体   繁体   English

在symfony2中插入多对多关系

[英]Insert Many to many relation in symfony2

I am trying to insert a many to many relation in my symfony2 project. 我试图在我的symfony2项目中插入多对多关系。 I have followed all steps from Symfony2-Doctrine: ManyToMany relation is not saved to database 我已经完成了Symfony2-Doctrine的所有步骤:ManyToMany关系未保存到数据库

ALL records are entered in items table except category_id... 除类别ID以外,所有记录都输入到项目表中。

Any idea ??? 任何想法 ???

"i have already category_id in items_categories... Should i remove the column category_id from items table ??" “我已经在items_categories中具有category_id ...我应该从items表中删除category_id列吗?”

Actually your Item entity should not have category_id property (as the table), only "categories", smth like: 实际上,您的Item实体不应具有category_id属性(如表),而应仅具有“ categories”,例如:

/**
  * @var array
  *
  * @ORM\ManyToMany(targetEntity="Category", inversedBy="items")
  */
protected $categories = array();

It's because category_id shouldn't be in items table but in items_categories table (it's many to many relation so Doctrine will create third table to achieve this) 这是因为category_id不应位于items表中,而应位于items_categories表中(这是多对多关系,因此Doctrine将创建第三个表来实现此目的)

You specify $categories field in your Item entity (Doctrine doesn't create any additional column like category_id). 您可以在Item实体中指定$ categories字段(Doctrine不会创建任何其他列,例如category_id)。 category_id is database concept. category_id是数据库概念。 On ORM level you don't use columns - you just use entity properties 在ORM级别上,您不使用列-您只使用实体属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM