Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a class is a Doctrine entity?

Tags:

Given a class name, say Domain\Model\User, is there a programmatic way to know whether this class is marked as a Doctrine entity?

I could check for the presence of the @Entity annotation, but I'm looking for a generic way that would work with any metadata driver (annotations, YAML, XML, etc.)

like image 895
BenMorel Avatar asked Jan 03 '14 00:01

BenMorel


2 Answers

Courtesy Stof on the doctrine-dev mailing list:

return ! $em->getMetadataFactory()->isTransient($className); 

I'll add that this method considers proxies (returned by the EntityManager as part of the lazy loading strategy) as transient; so if you're testing objects, don't blindly use get_class(), be sure to test first:

$object instanceof \Doctrine\Common\Persistence\Proxy 

Working implementation:

use Doctrine\Common\Persistence\Proxy; use Doctrine\ORM\EntityManager;  function isEntity(EntityManager $em, string|object $class): bool {     if (is_object($class)) {         $class = ($class instanceof Proxy)             ? get_parent_class($class)             : get_class($class);     }      return ! $em->getMetadataFactory()->isTransient($class); } 
like image 78
BenMorel Avatar answered Oct 07 '22 05:10

BenMorel


As an addition to Benjamin his answer...
If you know for sure that you are handling doctrine entities, but you are not sure whether you have a proxy or a instance of the real class you can easily retrieve the real class by using the Doctrine Common ClassUtils:

use Doctrine\Common\Util\ClassUtils; 

and then you can get the real class through the static getClass method like this:

$proxyOrEntity;  $className = ClassUtils::getClass($proxyOrEntity); 

So this means @Benjamin his isEntity function can be written like this:

/**  * @param EntityManager $em  * @param string|object $class  *  * @return boolean  */ function isEntity(EntityManager $em, $class) {     if(is_object($class)){         $class = ClassUtils::getClass($class);     }     return ! $em->getMetadataFactory()->isTransient($class); } 

Which will give you true/false depending on whether the class is a doctrine entity or not.

like image 34
Wilt Avatar answered Oct 07 '22 03:10

Wilt