collection-crud/src/Repository/LensesRepository.php

42 lines
1003 B
PHP

<?php
namespace CameraBundle\Repository;
use CameraBundle\Entity\{Lenses, PreviouslyOwnedLenses};
use Doctrine\ORM\EntityRepository;
class LensesRepository extends EntityRepository
{
/**
* @param Lenses $currentRecord
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function deacquire(Lenses $currentRecord)
{
$em = $this->getEntityManager();
$currentRecord->setFormerlyOwned(true);
$newRecord = new PreviouslyOwnedLenses();
$old = new \ReflectionObject($currentRecord);
$new = new \ReflectionObject($newRecord);
foreach ($old->getProperties() as $property) {
$propertyName = $property->getName();
if ($new->hasProperty($propertyName)) {
$newProperty = $new->getProperty($propertyName);
$newProperty->setAccessible(true);
$property->setAccessible(true);
$newProperty->setValue($newRecord, $property->getValue($currentRecord));
}
}
// dump($newRecord);
$em->persist($newRecord);
//$em->remove($currentRecord);
$em->flush();
}
}