I am working on a project that uses Typo3 9.5.14 with Extension Builder 9.10.2. I have a "provider"-model with a 1:n-relationship to a "label"-model. It looks like this: Extension Builder provider-label relation
My goal: Through a HTML Form I want to edit the values of the labels that are connected to a specific provider. At the moment my implemention looks as follows.
I have a controller in place that has the method "companyProfileSaveAction". The method works and allows me to set properties of the provider-Model. It looks like this:
public function companyProfileSaveAction(Provider $provider)
{
$persistenceManager = $this->objectManager->get(\TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager::class);
if ($provider->getUid()) {
$this->providerRepository->update($provider);
} else {
$this->providerRepository->add($provider);
}
$persistenceManager->persistAll();
$this->redirect('companyProfilePage', null, null, ['provider' => $provider, 'saved' => true]);
}
The here mentioned providerRepository extends \TYPO3\CMS\Extbase\Persistence\Repository. The functions "update" and "add" come from this inheritance.
Now I try to use this function to add or edit labels of the provider, which are connected via 1:n-relationship as mentioned above. I send data via html form. The data basically looks like this:
tx_my_extension[provider][labels][0][name]: label 1
tx_my_extension[[provider][labels][0][text]: description 1
tx_my_extension[[provider][labels][1][name]: label 2
tx_my_extension[[provider][labels][1][text]: description 2
tx_my_extension[[provider][labels][2][name]: label 3
tx__my_extension[[provider][labels][2][text]: description 3
This also works and the labels are created and connected to the provider. But now, as an example let's say I want to edit my labels like so:
tx_my_extension[provider][labels][0][name]: label 1 edited
tx_my_extension[[provider][labels][0][text]: description 1
tx_my_extension[[provider][labels][1][name]: label 2 edited
tx_my_extension[[provider][labels][1][text]: description 2
tx_my_extension[[provider][labels][2][name]: label 3 edited
tx__my_extension[[provider][labels][2][text]: description 3
When I send it like this, the old three labels are completely disconnected from my provider and three new labels are created. Over time, this fills my database with many disconnected labels, that don't serve any purpose. Instead of creating new labels every time I save my form, how can I edit the already existing labels?
It's been a while since I messed with these parts of TYPO3, but:
Have you tried adding the __identity
virtual post field, e.g. tx_my_extension[provider][labels][0][__identity]: 123
where the value is the UID of the related object?
This should make the property mapper fetch this object before mapping values onto the properties, which in turn should mean that your root entity is still related to the children and children are marked as "dirty" which then gets persisted.