在 Typo3 中,通过 Extension Builder 设置模型结构,我如何编辑通过 1:n 连接到父模型的子模型的值?

In Typo3, with a model structure set up through the Extension Builder, how can I edit the values of child-models connected via 1:n to parent-model?

我正在开发一个使用 Typo3 9.5.14 和 Extension Builder 9.10.2 的项目。我有一个“提供者”模型,与“标签”模型有 1:n 关系。它看起来像这样: Extension Builder provider-label relation

我的目标:通过 HTML 表单,我想编辑连接到特定提供商的标签的值。目前我的实现如下所示。

我有一个控制器,它有方法“companyProfileSaveAction”。该方法有效并允许我设置提供者模型的属性。它看起来像这样:

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]);
}

这里提到的providerRepository extends \TYPO3\CMS\Extbase\Persistence\Repository。 "update" 和 "add" 函数就是从这个继承而来的。

现在我尝试使用此功能来添加或编辑提供商的标签,这些标签通过 1:n-关系连接,如上所述。我通过 html 表单发送数据。数据基本上是这样的:

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

这也有效,标签已创建并连接到提供商。但是现在,举个例子,我想像这样编辑我的标签:

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

当我这样发送时,旧的三个标签与我的提供商完全断开连接,并创建了三个新标签。随着时间的推移,这会在我的数据库中填满许多断开连接的标签,这些标签没有任何作用。与其每次保存表单时都创建新标签,不如如何编辑已经存在的标签?

我已经有一段时间没弄乱 TYPO3 的这些部分了,但是:

您是否尝试过添加 __identity 虚拟 post 字段,例如tx_my_extension[provider][labels][0][__identity]: 123 其中值是相关对象的 UID?

这应该使 属性 映射器在将值映射到属性之前获取此对象,这反过来应该意味着您的根实体仍然与子实体相关,并且子实体被标记为“脏”,然后得到坚持。