/** * @param Product $product * @param Product $productCopy */ protected function cloneChildObjects(Product $product, Product $productCopy) { foreach ($product->getUnitPrecisions() as $unitPrecision) { $productCopy->addUnitPrecision(clone $unitPrecision); } foreach ($product->getNames() as $name) { $productCopy->addName(clone $name); } foreach ($product->getDescriptions() as $description) { $productCopy->addDescription(clone $description); } if ($imageFile = $product->getImage()) { $imageFileCopy = $this->attachmentManager->copyAttachmentFile($imageFile); $productCopy->setImage($imageFileCopy); } $attachments = $this->attachmentProvider->getEntityAttachments($product); foreach ($attachments as $attachment) { $attachmentCopy = clone $attachment; $attachmentFileCopy = $this->attachmentManager->copyAttachmentFile($attachment->getFile()); $attachmentCopy->setFile($attachmentFileCopy); $attachmentCopy->setTarget($productCopy); $this->doctrineHelper->getEntityManager($attachmentCopy)->persist($attachmentCopy); } }
public function testClone() { $id = 123; $product = new Product(); $product->getUnitPrecisions()->add(new ProductUnitPrecision()); $product->getNames()->add(new LocalizedFallbackValue()); $product->getDescriptions()->add(new LocalizedFallbackValue()); $refProduct = new \ReflectionObject($product); $refId = $refProduct->getProperty('id'); $refId->setAccessible(true); $refId->setValue($product, $id); $this->assertEquals($id, $product->getId()); $this->assertCount(1, $product->getUnitPrecisions()); $this->assertCount(1, $product->getNames()); $this->assertCount(1, $product->getDescriptions()); $productCopy = clone $product; $this->assertNull($productCopy->getId()); $this->assertCount(0, $productCopy->getUnitPrecisions()); $this->assertCount(0, $productCopy->getNames()); $this->assertCount(0, $productCopy->getDescriptions()); }