2016-10-10 37 views
2

我有兩個具有1:n關係的Extbase模型。 涉及兒童通過ObjectStorage從模型中取消選中模型中子對象列表中的對象流體

我想要達到的效果:editAction($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

回答

1

在你的控制器,你可以讓一個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); 
    } 
} 

現在你saveActioninitializeSaveAction後打來電話,只有選定的孩子keept。

+0

是的,這是我在想什麼,只是想知道是否有其他推薦的方法。 –

+1

據我所知這是簡單的方法。也許你可以編寫一個propertyMapper,但我認爲這是一個簡單的想法,它「很多」。 –