通过在 Fluid 中取消设置复选框从模型中的子对象列表中删除对象
Remove an object from list of child objects in Model by checkbox unset in Fluid
我有两个具有 1:n 关系的 Extbase 模型。 Parent 通过 ObjectStorage
.
与 Children 相关
我想要实现的目标:editAction($parent)
将所有 Children 的列表显示为带有复选框的条目(默认选中)。允许用户取消设置任何复选框,提交表单,相应的 Children 应该从 Parent 关系中删除。
到目前为止我做了什么。
在流体中,我遍历对象并像这样输出复选框:
<f:for each="{parent.children}" as="child" iteration="iteration">
<f:form.checkbox property="children.{iteration.index}" value="{child}" />
<label>{child.title}</label>
</f:for>
这会生成以下 HTML,这对我来说似乎没问题:
<input type="hidden" name="tx_myext_plugin[parent][children][0][__identity]" value="">
<input type="checkbox" name="tx_myext_plugin[parent][children][0][__identity]" value="135" checked="checked">
<label>child0-title</label>
<input type="hidden" name="tx_myext_plugin[parent][children][1][__identity]" value="">
<input type="checkbox" name="tx_myext_plugin[parent][children][1][__identity]" value="136" checked="checked">
<label>child1-title</label>
...
但是当我取消设置第二个复选框 (uid=136) 并提交表单时,出现以下异常
#1297759968: Exception while property mapping at property path "children.1": The identity property "" is no UID.
这似乎也是合乎逻辑的,因为有一个隐藏的输入,它提交了一个空值。
我想,我可以在 MVC 进程的某个地方挂钩,然后过滤掉空条目 __identity
,但是有没有更优雅(例如最佳实践)的方法?
TYPO3 7.6.11
在您的控制器中,您可以创建一个 initialize*Action()
函数。您可以在那里过滤掉您的空值,以便只存在具有标识的值。
public function initializeSaveAction()
{
if ($this->request->hasArgument('parent')) {
$parent = $this->request->getArgument('parent');
foreach ($parent['children'] as $key => $child) {
if (!$child['__identity']) unset($parent['children'][$key]);
}
$this->request->setArgument('parent', $parent);
}
}
现在您的 saveAction
在 initializeSaveAction
之后被调用,并且只有选定的子 keept。
我有两个具有 1:n 关系的 Extbase 模型。 Parent 通过 ObjectStorage
.
我想要实现的目标:editAction($parent)
将所有 Children 的列表显示为带有复选框的条目(默认选中)。允许用户取消设置任何复选框,提交表单,相应的 Children 应该从 Parent 关系中删除。
到目前为止我做了什么。 在流体中,我遍历对象并像这样输出复选框:
<f:for each="{parent.children}" as="child" iteration="iteration">
<f:form.checkbox property="children.{iteration.index}" value="{child}" />
<label>{child.title}</label>
</f:for>
这会生成以下 HTML,这对我来说似乎没问题:
<input type="hidden" name="tx_myext_plugin[parent][children][0][__identity]" value="">
<input type="checkbox" name="tx_myext_plugin[parent][children][0][__identity]" value="135" checked="checked">
<label>child0-title</label>
<input type="hidden" name="tx_myext_plugin[parent][children][1][__identity]" value="">
<input type="checkbox" name="tx_myext_plugin[parent][children][1][__identity]" value="136" checked="checked">
<label>child1-title</label>
...
但是当我取消设置第二个复选框 (uid=136) 并提交表单时,出现以下异常
#1297759968: Exception while property mapping at property path "children.1": The identity property "" is no UID.
这似乎也是合乎逻辑的,因为有一个隐藏的输入,它提交了一个空值。
我想,我可以在 MVC 进程的某个地方挂钩,然后过滤掉空条目 __identity
,但是有没有更优雅(例如最佳实践)的方法?
TYPO3 7.6.11
在您的控制器中,您可以创建一个 initialize*Action()
函数。您可以在那里过滤掉您的空值,以便只存在具有标识的值。
public function initializeSaveAction()
{
if ($this->request->hasArgument('parent')) {
$parent = $this->request->getArgument('parent');
foreach ($parent['children'] as $key => $child) {
if (!$child['__identity']) unset($parent['children'][$key]);
}
$this->request->setArgument('parent', $parent);
}
}
现在您的 saveAction
在 initializeSaveAction
之后被调用,并且只有选定的子 keept。