Get entity name from class object
Asked Answered
C

6

22

I have the following code:

namespace Acme\StoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Acme\StoreBundle\Entity\User
 *
 * @ORM\Table(name="users")
 * @ORM\Entity()
 */
class User {
...
}

$user = new User();

Does anybody know how I can now get the entity name (AcmeStoreBundle:User) from the User object?

Conall answered 3/2, 2012 at 10:42 Comment(1)
What is "the entity name" in your example?Jud
E
50

This should always work (no return of Proxy class):

$em = $this->container->get('doctrine')->getEntityManager(); 
$className = $em->getClassMetadata(get_class($object))->getName();

As getClassMetadata is deprecated, you can now use getMetadataFor

$entityName = $this->em->getMetadataFactory()->getMetadataFor(get_class($object))->getName();
Enright answered 9/1, 2013 at 7:7 Comment(4)
Does the job. Any advantages compared to Wojciech's approach?Depolymerize
As @chopchop pointed out, proxies like Proxies\__CG__\MyBundle\Entity\MyEntity are correctly handled. This is especially important if you fetch entites from a database.Ambrotype
@Enright If I do a dump on the entity, I see a CLASS property. Is that the same property that get_class() unreliably references? Or is that a reliable a value? For simplicity sake I would ideally like to reference this from a dynamic trait that is added to a versionable entity, where we don't have access to container.Closefisted
In Doctrine 2.4.8 this method returns the fully qualified class name. How do you get the short name, e.g. AcmeStoreBundle:User??Lode
W
9

getClassMetadata() is deprecated and will be removed in the future. Use getMetadataFor() instead:

$entityName = $this->em->getMetadataFactory()->getMetadataFor(get_class($entity))->getName();

Or a complete function:

/**
 * Returns Doctrine entity name
 *
 * @param mixed $entity
 *
 * @return string
 * @throws \Exception
 */
private function getEntityName($entity)
{
    try {
        $entityName = $this->em->getMetadataFactory()->getMetadataFor(get_class($entity))->getName();
    } catch (MappingException $e) {
        throw new \Exception('Given object ' . get_class($entity) . ' is not a Doctrine Entity. ');
    }

    return $entityName;
}
Waler answered 8/1, 2015 at 10:47 Comment(1)
Sorry to jump in on this question but I am facing an issue closest to this. I have a custom entity repository that extends Doctrine's. In my custom repository I have a count function that is suppose to give me a total number of records for whichever entity this method is called on. When I do a print_r I can see what I am looking for but do not knw how to get to it. The "from" method of my QueryBuilder needs to be passed the entity name being queried dynamically. Any help?Silent
K
8

PHP get_class() function will return User and namespace (see comments in php docs).

Kurtzman answered 3/2, 2012 at 10:47 Comment(4)
Although you are right, this will also return me the entity directory in the namespaced path, so yes i could strip this out but didnt know if there was already a function available in doctrine to get this intended valueConall
Be carefull using the method sine it won't work everytime. Doctrine use some cache and will generate proxy class. If you use thins method you will have something like this "Proxy\AcmeStoreBundleEntity\Proxy"Cheer
See the other solution, get_class() can fail because it sometimes returns a Proxy class. "$em->getClassMetadata(get_class($object))->getName()" does not. This handles the proxy classes correctly.Enright
Please add some explanation to your answer such that others can learn from it. Are you sure the "entity name" from Doctrine relates to this?Jud
R
1

You can use php's instanceOf operator:

if($a instanceof MyClass) { /*code*/ }

https://www.php.net/manual/pt_BR/language.operators.type.php

Raddy answered 16/4, 2020 at 19:38 Comment(1)
That won't return any "entity name"Jud
T
0

If you only need the Class name:

private function getEntityClassName(object $entity): string
{
    return $this->entityManager->getMetadataFactory()>getMetadataFor(
        $entity::class
    )->getReflectionClass()->getShortName();
}
Titrate answered 17/3, 2023 at 9:43 Comment(0)
G
0

To get the class name of an object in PHP, you typically use get_class($entity) or $entity::class. However, when working with Doctrine entities, these methods might not always return the expected class name because they can sometimes return the proxy class name instead.

To ensure you get the actual class name of a Doctrine entity, you should use the Doctrine\Common\Util\ClassUtils class. Here's how you can do it:

use Doctrine\Common\Util\ClassUtils;

$className = ClassUtils::getClass($entity);
Gally answered 19/5 at 23:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.