Symfony UX LiveComponent with collection
I'm using a LiveComponent to access a form with a OneToMany collection. Adding an item works perfectly, but when I delete an item, the entire collection is deleted.
The code is below.
->add('experiences', LiveCollectionType::class, [
'entry_type' => ExperienceType::class,
'label' => false,
'entry_options' => [
'writable' => true,
],
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
])
And the LiveComponent
#[LiveAction]
public function save(): Response
{
$this->submitForm();
/** @var Cv $model */
$model = $this->getForm()->getData();
try {
$this->upsertCvHandler->__invoke($model); // persist and save the data
$this->addFlash(
'success',
$this->translator->trans(id: 'action.edit-success', domain: 'cv')
);
} catch (\Exception $e) {
$this->addFlash('error', $e->getMessage());
$this->logger->error($e->getMessage(), [
'method' => __METHOD__,
'line' => __LINE__,
]);
}
return $this->redirectToRoute('admin_candidate_cv_index', ['id' => $this->candidate->getId()],
Response::HTTP_SEE_OTHER);
}
Previously I had 2 experiments; after deleting one experiment, here is the data sent:
{"props":{"initialFormData":338,"candidate":338,"formName":"cv","cv":{"id":"338","experiences":[{"id":"54","establishment":"test","experienceType":"professional","timeType":"full","hoursPerWeek":"","description":"test","beginAt":"2023-07-01","inProgress":null,"duration":"2","__dynamic_error":"","endAt":"2023-08-31"}],"_token":"csrf-token"},"isValidated":false,"validatedFields":[],"@attributes":{"id":"live-3883195067-0"},"@checksum":"0ElWsgFNZKilv/Y+nlz8rW8pUBtCLyqIWiEuWIPBrzA="},"updated":{},"args":{}}
Can you tell me where my error is coming from?
Thanks.