在我的extbase/fluid项目中,除了标准操作,如创建,删除,列表等,我想创建一个存储在存储库中的模型类对象的副本.使用findall(),所有对象都显示在列表中,并且每个对象旁边都会显示相应的操作,如删除,编辑.为了复制一个对象,我在相应的控制器中创建了一个重复的动作,这里是代码:
public function dupcliateAction(Tx_CcCompanylogin_Domain_Model_MyObject $testObject)
{
$this->myObjectRepository->add($testObject);
$this->redirect('list');//Lists the objects again from the repository
}
Run Code Online (Sandbox Code Playgroud)
看起来很不稳定,但没有新的对象添加到存储库,我没有收到错误.我检查了文档,没有明确的方法可用于复制.
小智 3
注意:克隆对象时,PHP 5 将执行该对象所有属性的浅表复制。任何引用其他变量的属性都将保留引用。
或者,您可以使用反射来创建对象的(深层)副本。
$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.
Run Code Online (Sandbox Code Playgroud)