[TYPO3-mvc] Copy object and add to repository

Franz Koch typo3.RemoveForMessage at elements-net.de
Wed Apr 6 17:57:54 CEST 2011


Hey,

>> $newProduct = clone($product);
>> $newProduct->setProductName('[Copy of] ' . $product->getProductName());
>>
>> $this->productsRepository->add($newProduct);
>>
>>
>> That should do the trick. The magic lies in "clone" which is creating a
>> new "identity".
>
> i got the exception
> "The uid "1037" has been modified, that is simply too much."
>
> The entry with the uid 1037 ist in the database but its empty.
>
> Did I forget something or need to set defaults or something like this?

FYI: We're currently discussing this use case in the Extbase team and 
probably consider the current behavior as a bug/missing feature. We 
didn't come to a final decision yet on what to do and how to implement 
this, but I'll keep you updated. In the meantime I'd suggest to create a 
new Object and fill it using a loop like this:

$availableProperties = 
Tx_Extbase_Reflection_ObjectAccess::getGettablePropertyNames($product);
$newProduct = $this->objectManager->create('Tx_..._Product');

foreach ($availableProperties as $propertyName) {
	if (Tx_Extbase_Reflection_ObjectAccess::isPropertySettable($newProduct, 
$propertyName) && !in_array($propertyName, array('uid','pid'))) {
		$propertyValue = 
Tx_Extbase_Reflection_ObjectAccess::getProperty($product, $propertyName);
		Tx_Extbase_Reflection_ObjectAccess::setProperty($newProduct, 
$propertyName, $propertyValue);
	}
}


This is a code snipped I use to map properties from one object to 
another one, not necessarily of the same type. That's why there is some 
overhead with "isPropertySettable" etc.

-- 
kind regards,
Franz Koch


More information about the TYPO3-project-typo3v4mvc mailing list