[TYPO3-mvc] Model object tree building with Tx_Extbase_Persistence_ObjectStorage property

Franz Koch typo3.RemoveForMessage at elements-net.de
Mon Sep 27 23:32:27 CEST 2010


Hey Claus,

>> Give the following a try in your controllers "form" action (so the
>> action rendering the initial form, not the one responsible to process
>> submitted form data):
>>
>> if (!$entry->getCompany()->getContact()->count()) {
>>      $dummyPerson = $this->objectManager->getObject('Person');
>>      $entry->getCompany()->addContact($dummyPerson);
>> }
>
> The objectManager don't contain any object. So I just try this.
>
> if (!$entry->getCompany()->getContact()->count()) {
>   	$contact = t3lib_div::makeInstance('Tx_Myext_Domain_Model_Person');
>    $entry->getCompany()->addContact($contact);
> }

The line above should actually be:
$dummyPerson = 
$this->objectManager->getObject('Tx_YourExt_Domain_Model_Person');

I thought that might have been clear - sorry for that. But using 
t3lib_div::makeInstance is also fine for now, because the objectManager 
currenty only does the same thing. But to be better compatible to FLOW3 
and probably make use of upcoming features like "mixins" I personally 
tend to use the objectManager instead where available.

> Looks from debugging view that company now contain a person object.

correct - that was intended.

>> In your template you iterate over the person(s) then:
>>
>> <f:for each="{company.contact}" as="contact">
>>      <f:form.textbox property="company.contact.{contact.uid}.forename"
>> id="forename"/>
>>      <f:form.textbox property="company.contact.{contact.uid}.lastname"
>> id="lastname"/>
>> </f:for>
>
> But in the view company.contact is empty, no iteration

yes sorry - I got a bit confused by the template and thought the company 
object might be available directly. But in this case you have to use 
{entry.company.contact} in the for loop of course.

>> Something like that should/could work - not tested though. If it's not
>> working by assigning this properties, you have to take care of the
>> correct form names yourself like this:
>>
>> <f:form.textbox name="entry[company][contact][{contact.uid}][forname]"
>> value="{contact.forname} id="forename"/>
>
> and no contact.uid available oO

Ihat might indeed become a problem, because the empty/newly created 
contact doesn't have a uid set. I wasn't sure if it's returning NULL or 
0 - but I think it's NULL - so we might need to make this a bit more 
complex using a condition - so give this a try:

<f:for each="{entry.company.contact}" as="contact"><f:alias 
map="{contactId:'{f:if(condition:contact.uid, then:contact.uid, 
else:0)}'}">	
     <f:form.textbox property="company.contact.{contactId}.forename" 
id="forename"/>
     <f:form.textbox property="company.contact.{contactId}.lastname" 
id="lastname"/>
</f:alias>
</f:for>

Btw. - you only need the iteration stuff if you plan to allow adding 
multiple persons at once. In my filter form I created a little JS 
snippet that's duplicating a dummy fieldset and is replacing a #UID# 
marker in the name attributes of the fields with a according number. 
This is working in my special case, because I don't store the filter 
objects in the DB - but in case of the persons you will store them - so 
it might be a more tricky for you because you must not reuse existing UIDs.

> Is my way of creating dummy person object wrong ? But what else I can do
> if $this->objectManager->getObject won't work ?

No, your way is fine. As mentioned above the objectManager is currently 
doing the same as t3lib_div::makeInstance, it only might in future also 
take care of more things.

> There is another strange thing. If I instance [1] the entry object, the
> company object is missing also (watching from debug). I have to instance
> the company object explicit in the entry constructor
> $this->setCompany(new Tx_Myext_Domain_Model_Company());
> No lazy loading annotations given.

right - extbase is not creating/adding a dummy company for you in that 
case - so you also have to create and add it on your own.

-- 
kind regards,
Franz Koch


More information about the TYPO3-project-typo3v4mvc mailing list