Reputation: 275
I am currently retrieving the class name of my entities to save changes into a log. This happens in a listener:
In my service layer:
$product = $line->getProduct();
$product->setAvailability($product->getAvailability() - $line->getAmount());
$em->persist($product);
the problem is that by doing following in a listener:
$className = join('', array_slice(explode('\\', get_class($entity)), -1));
$modification->setEntidad($className);
The $className
that is set into the modification is miomioBundleEntityProductoProxy
.
How can I get the real class name for my entity, and not the proxy class name?
Upvotes: 2
Views: 2127
Reputation: 9585
As the proxy class always extends from the real entity class:
class <proxyShortClassName> extends \<className> implements \<baseProxyInterface>
then, you can get it with class_parents()
function:
if ($entity instanceof \Doctrine\Common\Proxy\Proxy) {
$class = current(class_parents($entity)); // get real class
}
Especially useful when you don't have access to EntityManager
instance.
Upvotes: 2
Reputation: 25431
The fact that you receive a proxy name when calling get_class
on a proxy is quite normal, since proxies are a required concept to let the ORM and lazy loading of associations work.
You can get the original class name by using following API:
$realClassName = $entityManager->getClassMetadata(get_class($object))->getName();
Then you can apply your own transformations:
$normalizedClassName = join('', array_slice(explode('\\', $realClassName), -1));
$modificacion->setEntidad($normalizedClassName);
Upvotes: 1