SonataAdminBundle: Unable to delete from sonata_type_collection (worked in 2.0 but not after upgrade)
I know that this used to be a common problem and the answer was always to put 'by_reference' => false. Here’s my code:
protected function configureFormFields(FormMapper $form)
{
$form
->add('title')
->add('collectionHasStories', 'sonata_type_collection', array('by_reference' => false, 'label' => 'Stories'), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position'
))
;
}
I don’t think that this is something I’m doing wrong as I’ve noticed that since upgrading I’m not even able to delete media items from a gallery’s galleryHasMedia collection. This is a very big problem for me!
Has anything changed since that would cause this to break? Or is it something to do with the particular versions I’m using? Here is everything related to sonata in my composer.json:
"sonata-project/user-bundle": "*",
"sonata-project/cache-bundle": "dev-master",
"sonata-project/doctrine-orm-admin-bundle": "*",
"sonata-project/media-bundle": "*",
(I found I didn’t need to add the admin-bundle as the user-bundle depends on the dev-master version of it)
About this issue
- Original URL
- State: closed
- Created 12 years ago
- Reactions: 4
- Comments: 19 (7 by maintainers)
Commits related to this issue
- remove nelmio >= 3.0 conflict rule (#998) — committed to intexsys/SonataAdminBundle by ibogdan94 6 years ago
Hello, I got the same problem on symfony 2.1 + sonata + Admin with many-to-many field; I set the doctrine’s orphanRemoval property on my one-to-many field and then makes deletion working on type Collection, something likes :
My Entity (simplified)
Note that
orphanRemoval=trueis added to theOneToManyannotation. Also I leftby_referenceto befalsein the form mapper.The problem
Similar to above, I am seeing a problem that all items got deleted upon save.
The cause
This is due to the code in
setAttachments, which replaces the existingPersistentCollectionof the entity by a new array/ArrayCollection. This is WRONG. (See https://github.com/doctrine/doctrine2/issues/6344, quote: “Replacing collection instances is not really fully supported”)The solution
So I modified the
setAttachmentsmethod like this:The local
$attachmentscontains the new and existing items but not the deleted ones (they are removed by the ResizeFormListener). So thesetAttachmentsmethod compares the entity’s$attachmentsagainst the local one, adds new items to it and removes deleted items from it.Now delete function should work correctly.
Alternative method
If I add the
removeAttachmentmethod to theNewsclass it will get called by the PropertyAccessor beforesetAttachments():In this case, it’s no longer neccessary to track the removed items in
setAttachments(). Also, it turns out to be fine adding the same object to thePersistentCollection. This means that it’s also not neccessary to track whether an item has already been added to the collection, we can just add everything to it and let Doctrine handles the rest. ThesetAttachments()method can then be simplified to:This works, but this would mean that the
attachmentsarray could be in an invalid state (Entity’s$attachmentsfield may contain duplicated item). AlsosetAttachments()now assumes that items not found in the$attachmentsargument be removed viaremoveFilein advance.Also fixed this issue by adding orphanRemoval=true to my OneToMany associations in my OneToMany <-> ManyToMany <-> OneToMany relationship.
It is an issue with reference and the Form component. Please review: https://github.com/sonata-project/SonataMediaBundle/blob/master/Admin/GalleryAdmin.php#L125