2

我目前正在检索我的实体的类名以将更改保存到日志中。这发生在侦听器中:

在我的服务层:

$product = $line->getProduct();

$product->setAvailability($product->getAvailability() - $line->getAmount());
$em->persist($product);

问题是通过在侦听器中执行以下操作:

$className = join('', array_slice(explode('\\', get_class($entity)), -1));
$modification->setEntidad($className);

设置到修改中的$classNamemiomioBundleEntityProductoProxy

如何获取实体的真实类名,而不是代理类名?

4

2 回答 2

1

在调用代理时收到代理名称这一事实get_class是很正常的,因为代理是让 ORM 和延迟加载关联工作的必要概念。

您可以使用以下 API 获取原始类名:

$realClassName = $entityManager->getClassMetadata(get_class($object))->getName();

然后您可以应用自己的转换:

$normalizedClassName = join('', array_slice(explode('\\', $realClassName), -1));

$modificacion->setEntidad($normalizedClassName);
于 2013-03-18T17:28:54.537 回答
1

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.

于 2017-08-22T23:11:44.323 回答