Extbase: Make a (deep) copy of the model


/ Published in: PHP
Save to your folder(s)

You need a copy of your model and relations (ObjectStorage properties).
In this example there is a model Product with property ProductAddition (1:n relation).


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. $productClone = $this->objectManager->create('Tx_Theext_Domain_Model_Product');
  4.  
  5. // $product = source object
  6. $productProperties = Tx_Extbase_Reflection_ObjectAccess::getAccessibleProperties($product);
  7. foreach ($productProperties as $propertyName => $propertyValue) {
  8. Tx_Extbase_Reflection_ObjectAccess::setProperty($productClone, $propertyName, $propertyValue);
  9. }
  10.  
  11. // $productAdditions = ObjectStorage property
  12. $productAdditions = $product->getProductAddition();
  13. $newStorage = $this->objectManager->get('Tx_Extbase_Persistence_ObjectStorage');
  14. foreach ($productAdditions as $productAddition) {
  15. $productAdditionClone = $this->objectManager->create('Tx_Theext_Domain_Model_ProductAddition');
  16. $productAdditionProperties = Tx_Extbase_Reflection_ObjectAccess::getAccessibleProperties($productAddition);
  17. foreach ($productAdditionProperties as $propertyName => $propertyValue) {
  18. Tx_Extbase_Reflection_ObjectAccess::setProperty($productAdditionClone, $propertyName, $propertyValue);
  19. }
  20. $newStorage->attach($productAdditionClone);
  21. }
  22. $productClone->setProductAddition($newStorage);
  23. // This have to be repeat for every ObjectStorage property, or write a service.
  24. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.