21

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?

1
  • What is "the entity name" in your example?
    – Nico Haase
    Mar 17, 2023 at 9:46

5 Answers 5

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();
4
  • Does the job. Any advantages compared to Wojciech's approach?
    – dialogik
    Aug 16, 2013 at 10:20
  • 1
    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.
    – Alan Q.
    Sep 10, 2013 at 10:02
  • @benske 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. Oct 16, 2014 at 22:09
  • In Doctrine 2.4.8 this method returns the fully qualified class name. How do you get the short name, e.g. AcmeStoreBundle:User?? Aug 29, 2017 at 9:38
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;
}
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? Jan 12, 2016 at 13:54
8

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

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 value
    – Matt
    Feb 3, 2012 at 11:06
  • 5
    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"
    – Chopchop
    Aug 28, 2012 at 14:09
  • 1
    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.
    – benske
    Nov 15, 2013 at 6:38
  • 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?
    – Nico Haase
    Mar 17, 2023 at 9:47
0

You can use php's instanceOf operator:

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

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

1
  • That won't return any "entity name"
    – Nico Haase
    Mar 17, 2023 at 9:46
0

If you only need the Class name:

private function getEntityClassName(object $entity): string
{
    return $this->entityManager->getMetadataFactory()>getMetadataFor(
        $entity::class
    )->getReflectionClass()->getShortName();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.