Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine 2 get real class name of proxy class

The 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).

like image 436
Rene Terstegen Avatar asked Oct 06 '10 08:10

Rene Terstegen


2 Answers

use the Doctrine class 'ClassUtils'

\Doctrine\Common\Util\ClassUtils::getRealClass(get_class($entity));
like image 99
Paul-Emile MINY Avatar answered Sep 28 '22 00:09

Paul-Emile MINY


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);
    }
}
like image 40
beberlei Avatar answered Sep 27 '22 23:09

beberlei