[TYPO3-mvc] Temporary solution: Persisting/updating nested objects (or sub objects) coming from a frontend form
Thomas "Thasmo" Deinhamer
thasmo at gmail.com
Thu Jan 14 21:15:18 CET 2010
Good evening folks!
Recently I coded a shopping cart for a new shop extension
I'm working on at the moment, using Extbase and Fluid.
Trying to persist nested objects (or sub objects) which are
related to a parent object using an intermediate table (and
listed in the backend via IRRE), I had some troubles persisting
those objects after submitting them to a controller, previously
rendered into a frontend form.
The thing is simple. The shopping cart displays cart items, which
are the sub objects of the user object. Unfortunately at present,
Fluid - in combination with Extbase - is not able to generate a
working form, for updating nested objects.
Searching for a solution I ended up in the #typo IRC channel,
where cdngfrog helped me out a lot and found out how to manually
create the html for a working form for persisting the cart items.
The trick is to send the identifier index with the actual cart items,
so the property mapper can map the sub objects accordingly. Otherwise,
as it happened in my case, the nested objects are deleted.
Here is a short example of thr form:
<f:form name="object" object="{object}" action="update">
<f:for each="{object.subObjects}" as="subObject">
<f:form.textbox name="subObjects[{subObject.uid}][title]"
value="{subObject.title}"/>
<f:form.hidden name="subObjects[{subObject.uid}][__identity]"
value="{subObject.uid}"/>
</f:for>
</f:form>
Using the hidden field, submitting the __identity value,
it is possible to manually map the edited title property onto
the edited sub objects.
To map the value, this controller action will help out:
/**
* Update
*
* @dontverifyrequesthash
* @param Tx_EXT_Domain_Model_Object $object
* @param array $subObjects
* @return void
*/
public function updateAction(Tx_EXT_Domain_Model_Object $object, array
$subObjects) {
$this->propertyMapper->map(array('subObjects'), array('subObjects' =>
$subObjects), $object);
}
I stripped down the examples above, hopefully they represent a snapshot
for a working example, as of Extbase 1.0, TYPO3 4.3. If something's not
correct, please leave note.
Have fun and thank you,
Thomas
More information about the TYPO3-project-typo3v4mvc
mailing list