[TYPO3-mvc] Weird behaviour of persiting sub/child objects

Franz Koch typo3.RemoveForMessage at elements-net.de
Fri Jan 29 00:34:56 CET 2010


Hi Thomas,

> this code behaves very weird:
...
> The result is, that the cartItem object gets persisted
> to the database table, but the customer property
> is not set. It's just 0.
>
> If I comment in '$cartItem->setCustomer($this->customer);'
> the customer property is set, also in the database correctly.
>
> Is this an intended behaviour or am I
> maybe missing some kind of relation definition somewhere?

on having a look at the blog example, there the blog object is also 
assigned to the post "by hand".

$blog->add($post);
$post->setBlog($blog);

So it seems to be intended that way. What you could do to ease things up 
for you is to have the customer model is add itself to the cartItem in 
the addCartItem method:

public function addCartItem(Tx_... $cartItem) {
   $this->cartItems->add($cartItem);
   $cartItem->setCustomer($this);
}

The only question is if this logic should belong to the model or the 
controller - or maybe a service 
(Tx_..._CartService::addCartItemToCustomer($cartItem, $customer)?

-- 
kind regards,
Franz Koch


More information about the TYPO3-project-typo3v4mvc mailing list