I want to assign many items to one category. I'm using Symfony 2.6.7.
I'm not at the owning side here. If I open this URI:
/category/2/assign
[x] Item 1
[x] Item 2
(Save Button)
I have the possibility to chose many items with checkboxes.
This is my db table "Item", where I want to connect the both:
id | category_id
It is an OneToMany relation, where One = Category and Many = Items.
How can I assign the both here?
It is already working, when I edit one item and than select a category to this item. Now I'm on the category side and here I want select many items to this one category. Can someone help, please? :-)
See, In Symfony2 the entity with the property with the inversedBy doctrine comment is the one that is supposed to take action when you want to create a new link between the two tables. That is why you can assign a category to an item but not add items to a category.
The above code is a standard CRUD-generated Symfony2 updateAction function. The only tweak is the foreach, thus forcibly assigning a category to every item you selected in the form.
It's rudimentary but it works.
NOTE: I did not include a workaround for REMOVING items from a category, but a similar approach would do it. Hope it helps.
EDIT: FOR REMOVING ITEMS:
Same function, just include the new lines.
array_diff will return the items that were linked to the category entity before the submit and are not after the submit, then with the foreach again you can assign null as each of those items' category, i.e.: break the link between them.
The second foreach does the same as the original answer's. Just try it like this now and tell me if it worked.
Again, rudimentary, again, should work.