Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicating extbase repository object

Tags:

extbase

In my extbase/fluid project,in addition to standard actions such as create,delete,list etc, I want to create a duplicate of a model class object which are stored in a repository. Using findall(), all objects are displayed in a list and corresponding actions such as delete,edit are displayed next to each. For duplicating an object, I have created a duplicate action in the corresponding controller and here is the code:

public function dupcliateAction(Tx_CcCompanylogin_Domain_Model_MyObject $testObject)
{
 $this->myObjectRepository->add($testObject);
 $this->redirect('list');//Lists the objects again from the repository
}

Seems straitforward enough but no new object is added to the repository and I am not getting an error.I have checked the documentation and there is no explicit method available for duplicating.

like image 615
user1107888 Avatar asked Sep 17 '12 16:09

user1107888


2 Answers

Note : When an object is cloned, PHP 5 will perform a shallow copy of all of the object's properties. Any properties that are references to other variables, will remain references.

Alternative you can use reflection to create a (deep) copy of your object.

    $productClone = $this->objectManager->create('Tx_Theext_Domain_Model_Product');

// $product = source object
    $productProperties = Tx_Extbase_Reflection_ObjectAccess::getAccessibleProperties($product);
    foreach ($productProperties as $propertyName => $propertyValue) {
        Tx_Extbase_Reflection_ObjectAccess::setProperty($productClone, $propertyName, $propertyValue);
    }

// $productAdditions = ObjectStorage property
    $productAdditions = $product->getProductAddition();
    $newStorage = $this->objectManager->get('Tx_Extbase_Persistence_ObjectStorage');
    foreach ($productAdditions as $productAddition) {
        $productAdditionClone = $this->objectManager->create('Tx_Theext_Domain_Model_ProductAddition');
        $productAdditionProperties = Tx_Extbase_Reflection_ObjectAccess::getAccessibleProperties($productAddition);
        foreach ($productAdditionProperties as $propertyName => $propertyValue) {
            Tx_Extbase_Reflection_ObjectAccess::setProperty($productAdditionClone, $propertyName, $propertyValue);
        }
        $newStorage->attach($productAdditionClone);
    }
    $productClone->setProductAddition($newStorage);
// This have to be repeat for every ObjectStorage property, or write a service. 
like image 172
CalleKhan Avatar answered Oct 26 '22 05:10

CalleKhan


For me, the Solution "Clone $Object and resetUid() in the Modell" does not work .. maybe this solution had worked in older TYPO3 Versions but in 7.6. LTS it throws an exception

#1222871239: The uid "61" has been modified, that is simply too much. 

so maybe someone can find my solution helpful as it is much less code than the other reflection Solution: (and you do not have to think about to set all single properties .. )

Given: an Object called $registrant with all Data. Wanted Result: a copy of that Object with same Data, but new Uid ..

/** @var \JVE\JvEvents\Domain\Model\Registrant $newregistrant */

$newregistrant = $this->objectManager->get( "JVE\\JvEvents\\Domain\\Model\\Registrant")  ;

$properties = $registrant->_getProperties() ;
unset($properties['uid']) ;

foreach ($properties as $key => $value ) {
    $newregistrant->_setProperty( $key , $value ) ;

}
like image 34
Jörg velletti Avatar answered Oct 26 '22 07:10

Jörg velletti