I'm working on a Symfony 4 Form with embedded types for a REST API. The form:
class OffertaType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('pulsanti_social_attivi', CheckboxType::class, [ ])
->add('immagini', CollectionType::class, [
'entry_type' => OffertaImmagineType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => 'immagini'
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
[
'data_class' => Offerta::class,
'allow_extra_fields' => true,
'csrf_protection' => false,
]
);
}
}
The collection contains 2 objects
[
{
"id": 1,
"url": "https://via.placeholder.com/150?text=1"
},
{
"id": 2,
"url": "https://via.placeholder.com/150?text=2"
}
]
All "works", if I post some data a new object is created and if I update an object the DB is updated. The strange thing is the update/delete of row seem based on the object position in the collection and not on its ID. If I delete the first object the result should be
[
{
"id": 2,
"url": "https://via.placeholder.com/150?text=2"
}
]
but its
[
{
"id": 1,
"url": "https://via.placeholder.com/150?text=2"
}
]
Symfony see only 1 object, so delete the second one and update the first one "url" field.
How can I "index" the collection by the objects' ID and not by their position?