Doctrine 2 get real class name of proxy class
T开发者_JAVA技巧he following:
I have approximantely 20 models. These classes extend a base class. This base class contains a method which should be able to determine the classname of the child element. Normally you could this with:
get_called_class();
But in many cases doctrine 2 uses ProxyClasses and in that case the get_called_class() function returns something like:
Proxies\BasePageElementProxy
While the original name is \Base\PageElement. Can anybody tell me how I can find out what the name of this class is (without generating the name out of the string Proxies\BaseSectionProxy cause that is dirty and in many cases unreliable).
use the Doctrine class 'ClassUtils'
\Doctrine\Common\Util\ClassUtils::getRealClass(get_class($entity));
You get the real name by calling:
$em->getClassMetadata(get_called_class())->name;
This however requires a reference to the EntityManager. If you are doing static finder methods through your entity classes you will have access to that statically/globally anyways though, for example:
abstract class Record
{
private static $em = null;
static public function setEntityManager($em)
{
self::$em = $em;
}
static public function __callStatic($method, $args)
{
$className = self::$em->getClassMetadata(get_called_class())->name;
return call_user_func_array(array(self::$em->getRepository($className), $method), $args);
}
}
精彩评论