views:

23

answers:

1

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

Thanx in advance!

+2  A: 

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);
    }
}
beberlei
Thank you very much! Works like a charm. It looks like you're becoming my personal teacher when it comes to Doctrine! +1
Stegeman