我在使用对象作为其他一些对象的原型时遇到问题。
下面的代码预计会保留对象容器的所有实例(在下面的代码中可见的是 $module1 和 $module2),但是只有最后一个被保留,我认为这是由于我复制原型对象的方式。
我应该以其他方式复制原型吗?
//Create module prototype
$module = new Container();
$module->setCompany($currentCompany);
$module->setContainerType($typeModule);
$module->setParent($entity);
//Set the modules in use by this template (structure a bit ugly here, but makes it easier when dealing with the layout on other areas of the app)
if ($size = $template->getModule1()) {
$module1 = $module; //copy the prototype
$module1->setName('Module1'); //Give a unique name
$module1->setContainerSize($size); //Copy the size from the layoutTemplate
$em->persist($module1); //Persist this module
$layout->setModule1($module1); //Connect this container to become a module in the layout
}
if ($size = $template->getModule2()) {
$module2 = $module; //copy the prototype
$module2->setName('Module2'); //Give a unique name
$module2->setContainerSize($size); //Copy the size from the layoutTemplate
$em->persist($module2); //Persist this module
$layout->setModule2($module2); //Connect this container to become a module in the layout
}