1

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 ???

Community
  • 1
  • 1
Shaun
  • 2,313
  • 7
  • 36
  • 43
  • Please post your code from your entity, and also your code from the controller where you save persist the entity – Sam Janssens Sep 12 '12 at 07:03
  • 1
    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) – Cyprian Sep 12 '12 at 07:11
  • i have already category_id in items_categories... Should i remove the column category_id from items table ?? – Shaun Sep 12 '12 at 07:16
  • you specify $categories field in your Item entity (Doctrine doesn't create any additional column like category_id) – Cyprian Sep 12 '12 at 08:05
  • i have $categories field in Item entity.... and it persists the data handsomely in category_items table with category and item id ,but i want know should i maintain category_id in items entity?? – Shaun Sep 12 '12 at 08:14
  • definitly no :) category_id is database concept. On ORM level you don't use columns - you just use entity properties – Cyprian Sep 12 '12 at 08:22
  • @Cyprian please post your comment as answer so that i can accept it ... – Shaun Sep 12 '12 at 09:48

2 Answers2

0

"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();
ZloyPotroh
  • 377
  • 7
  • 20
0

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

Cyprian
  • 11,174
  • 1
  • 48
  • 45