[TYPO3-mvc] Copy object and add to repository

Fabian Fisahn fisahn at its-immaterial.com
Wed Apr 6 22:09:34 CEST 2011


Hi Franz,

On 06.04.11 17:57, Franz Koch wrote:
> 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);
>     }
> }

thanks, cool solution. I tried a SQL level solution, but thats nicer.

But: it doesn't work with lazy properties. I got an exception, thats one
argument should be of for example type "Tx...Brand", instance of
Tx_Extbase_Persistence_LazyLoadingProxy given

If I remove the @lazy addition it works, but makes my application slower.

Is that a known bug?

Fabian








More information about the TYPO3-project-typo3v4mvc mailing list