[TYPO3-mvc] creating two related objects in one form

Franz Koch typo3.RemoveForMessage at elements-net.de
Sun Nov 7 13:39:46 CET 2010


Hey guys,

>> I try to get my head around creating two related objects in one form in
>> the FE.
>>
>> For example:
>> I want to add a blog post from FE and attach a (or multiple!) tags to
>> it. Tags will be value objects, and preferrably reusable, so a suggest
>> feature should be possible too, but first things first.
>>
>> Does someone have a working solution on how to achieve this?
>>
>> Thanks in advance for sharing!
>>
>> Henjo
>>
>
> Hi Henjo,
>
> I'm not quite sure if I understand correctly, but maybe my post:
> "Editing 1:n connections - Fluid forms" explains what you are looking
> for? I wrote it on 23.10. and just updated today

yes, the solution from this posting should also help you. It shows you 
how to make child objects editable within the same form. Christian 
mentioned in his solution the hidden "_identity" field, which is telling 
extbase the objects identity(DB-uid) so that it can update it. If you 
don't provide a identity, extbase is creating a new object.

Actually I'm not sure how valueObjects behave here as they don't have a 
identity from a OO point of view, but of course they have a database 
identifier. IIRC you don't need the identity field containing the UID in 
that case, but I'm not entirely sure - never had that situation.

So in your case it should be sufficient to create form fields that look 
like this in order to have extbase map them automatically to your post:

<input type="hidden" 
name="tx_yourext_pi1[form/objectName][propertyName][]" value="Name of 
the tag" />



If you prefer using a simple comma separated input field only, you have 
to use a different approach though. In that case you have to split the 
tags and create and attach according tagObjects by hand in your 
controller action. In that case don't try to bind the input field for 
the tags to your object, but give it a custom name attribute (like 
'tags') and add a $tags argument to your create method - extbase will 
automatically map it for you and you can process it something like this 
(not tested):

public function create (Tx_YourExt_Domain_Model_Post $post, $tags = NULL) {
   if ($tags !== NULL && strlen($tags)) {
	// clear old tags
	$post->getTags()->removeAll($posts->getTags());

	// add new tags		
	$tagsArray = t3lib_div::trim_explode(',',$tags);
	foreach ($tagsArray as $tagName) {
		$tag = $this->objectManager->getObject('Tx_YourExt_Domain_Model_Tag');
		$tag->setName($tagName);
		$post->addTag($tag);
	}
   }
   $this->postRepository->attach($post);
}

As the tags are value objects, extbase should automatically check if the 
same tag already exists in the DB and bind this one to the post 
automatically.

HTH.
-- 
kind regards,
Franz Koch


More information about the TYPO3-project-typo3v4mvc mailing list