我当前正在检索实体的类名,以将更改保存到日志中。这发生在监听器中:
在我的服务层:
$product = $line->getProduct();
$product->setAvailability($product->getAvailability() - $line->getAmount());
$em->persist($product);
问题是通过在监听器中执行以下操作:
$className = join('', array_slice(explode('\\', get_class($entity)), -1));
$modification->setEntidad($className);
设置到修改中的$className
是momioBundleEntityProductoProxy
。
如何获取实体的真实类名,而不是代理类名?
最佳答案
由于代理类总是从真实实体类扩展而来:
class <proxyShortClassName> extends \<className> implements \<baseProxyInterface>
那么,你可以通过 class_parents()
获取它功能:
if ($entity instanceof \Doctrine\Common\Proxy\Proxy) {
$class = current(class_parents($entity)); // get real class
}
当您无权访问 EntityManager
实例时特别有用。
关于doctrine-orm - Doctrine ORM 实体类名返回代理类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15476210/