简体   繁体   中英

Insert Many to many relation in symfony2

I am trying to insert a many to many relation in my symfony2 project. I have followed all steps from Symfony2-Doctrine: ManyToMany relation is not saved to database

ALL records are entered in items table except category_id...

Any idea ???

"i have already category_id in items_categories... Should i remove the column category_id from items table ??"

Actually your Item entity should not have category_id property (as the table), only "categories", smth like:

/**
  * @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)

You specify $categories field in your Item entity (Doctrine doesn't create any additional column like category_id). category_id is database concept. On ORM level you don't use columns - you just use entity properties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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