2013-03-18 63 views
3

我目前正在检索我的实体的类名称以将更改保存到日志中。这发生在一个听众:学说ORM实体类名称返回代理类名称

在我服务层:

$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被设置成修改是miomioBundleEntityProductoProxy

我怎样才能得到真实的类名称为我的实体,而不是代理类名称?

+0

我既不知道你的问题是什么,也不知道你的问题是什么。请考虑改写你的问题,以便理解。 – Sgoettschkes 2013-03-18 16:11:35

+0

@Sgoettschkes我改写了这个问题 – Ocramius 2013-03-18 20:01:48

回答

0

在代理上调用get_class时,您收到代理名称的事实很正常,因为代理是让ORM和延迟加载协会起作用的必需概念。

您可以通过以下API得到原始的类名:

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

那么你可以申请自己的转换:

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

$modificacion->setEntidad($normalizedClassName); 
+1

使用ClassUtils :: getRealClass($ className)的速度更快 – kipelovets 2013-08-26 10:17:35

+0

这不会在所有情况下检索类名。你不应该手动这样做,因为它是一个实现细节。 – Ocramius 2013-08-26 10:31:40

1

由于代理类总是从现实的实体延伸等级:

class <proxyShortClassName> extends \<className> implements \<baseProxyInterface> 

然后,你可以用class_parents()功能得到它:

if ($entity instanceof \Doctrine\Common\Proxy\Proxy) { 
    $class = current(class_parents($entity)); // get real class 
} 

当你没有获得EntityManager情况下尤其有用。