Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get entity name from class object

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?

like image 763
Matt Avatar asked Feb 03 '12 10:02

Matt


1 Answers

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();
like image 97
benske Avatar answered Oct 05 '22 13:10

benske