문제

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

도움이 되었습니까?

해결책

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);
    }
}

다른 팁

use the Doctrine class 'ClassUtils'

\Doctrine\Common\Util\ClassUtils::getRealClass(get_class($entity));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top