[Flow] Copy an object
David Sporer
david.sporer at gmail.com
Thu Apr 16 07:28:13 CEST 2015
Thank you Carsten.
I’ve tried your suggestion but it’s not yet working because if I create the connected objects like you suggested it gives me an error:
„It is not allowed to map property „0“. You need to use $propertyMappingConfiguration->allowProperties(‚0‘) to enable mapping of this property.“
This happens because the first entry of the array has index 0 which seems to be a problem.
Can you help?
Regards,
David
> Am 15.04.2015 um 09:40 schrieb Carsten Bleicker <carsten at bleicker.de>:
>
> i think you can not solve this in a generic way.
> nobody knows if your application needs to just create a new collection of related objects
> or do you need to copy each single collection item also.
>
> maybe you could try to get the properties you need for the copy project as a simple array.
> and use the converter features to convert this array into a new object.
>
> f.e.
>
> class CopyService {
>
> /**
> * @var \TYPO3\Flow\Property\PropertyMapper
> * @Flow\Inject
> */
> protected $propertyMapper;
>
> /**
> * @var \TYPO3\Flow\Property\PropertyMappingConfigurationBuilder
> * @Flow\Inject
> */
> protected $propertyMappingConfigurationBuilder;
>
> /**
> * @param FooObject $object
> * @return FooObject
> */
> public function copyFooWithSameBarReferences(FooObject $object){
> $properties = ObjectAccess::getGettableProperties($object);
>
> // maybe $object->bars contains collection of Baz
> // and you want to use the same relations for the new object, just add their identifiers to the new collection
> $properties['bars'] = [];
> while($bar = $object->getBars()->current()){
> $properties['bars'][] = $this->persistenceManager->getIdentifierByObject($bar);
> $object->getBars()->next();
> }
>
> // Configure what is allowed to map from $properties etc.
> // Note: its incomplete at this point
> $configuration = $this->propertyMappingConfigurationBuilder->build();
> $configuration->allowAllProperties();
>
> return $this->propertyMapper->convert($properties, FooObject::class, $configuration);
> }
>
> /**
> * @param FooObject $object
> * @return FooObject
> */
> public function copyFooWithNewBarReferences(FooObject $object){
> $properties = ObjectAccess::getGettableProperties($object);
>
> // maybe $object->bars contains collection of Baz
> // and you want to use the same relations for the new object, just add their identifiers to the new collection
> $properties['bars'] = [];
> while($bar = $object->getBars()->current()){
> $properties['bars'][] = ObjectAccess::getGettableProperties($bar);
> $object->getBars()->next();
> }
>
> // Configure what is allowed to map from $properties etc.
> // Note: its incomplete at this point
> $configuration = $this->propertyMappingConfigurationBuilder->build();
> $configuration->allowAllProperties();
>
> return $this->propertyMapper->convert($properties, FooObject::class, $configuration);
> }
>
> }
>
>
>
>
>> Am 15.04.2015 um 08:45 schrieb David Sporer <david.sporer at gmail.com>:
>>
>> Hi,
>>
>> I have a fairly complex model and I want to create a copyAction in my controller that basically duplicates the object.
>> My first approach was to simply clone the original object with clone().
>> The problem with this however is that the references to resources and other collections remain the same.
>> If I clear collections of the copied resource the collection of the original resource is also cleared which I don’t want.
>> Same thing for resources. Deleting the original object fails because the resource is referenced by the new object also.
>>
>> Do you have any best practices for doing such a thing?
>> One approach that I guess would work is creating the copied object from scratch and also manually duplicating all connected objects but that wouldn’t be that easy and I guess there must be an easier way.
>>
>> Thanks,
>> David
>> _______________________________________________
>> Flow mailing list
>> Flow at lists.typo3.org
>> http://lists.typo3.org/cgi-bin/mailman/listinfo/flow
>
> _______________________________________________
> Flow mailing list
> Flow at lists.typo3.org
> http://lists.typo3.org/cgi-bin/mailman/listinfo/flow
More information about the Flow
mailing list