<?php
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="item_archive")
*/
class ItemArchive
{
}
<?php
use Doctrine\ORM\Mapping as ORM,
Doctrine\ORM\Event\LifecycleEventArgs;
/**
* @ORM\Table(name="item")
* @ORM\HasLifecycleCallbacks
*/
class Item
{
//
// Getters & setters here
//
/**
* Returns an instance of ItemArchive with all class properties copied.
* @param Item $item - the Item to copy properties from
* @return ItemArchive
*/
protected static function createArchive(Item $item)
{
$archive = new ItemArchive();
foreach (get_object_vars($item) as $key => $value) {
$setter = 'set' . ucfirst($key);
if (is_callable([$archive, $setter])) {
$archive->$setter($value);
}
}
return $archive;
}
/**
* @ORM\PreRemove
* Will be invoked when EntityManager::remove is called,
* to persist a copy of the Entity in the archive table.
*/
public function onPreRemove(LifecycleEventArgs $eventArgs)
{
$archive = self::createArchive($this);
$eventArgs->getEntityManager()->persist($archive);
}
}