获取实体类的表名

Mat*_*att 44 symfony doctrine-orm

您知道如何从我的控制器类中的实体声明中获取表名

实体类

<?php

namespace Acme\StoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Acme\StoreBundle\Entity\User
 *
 * @ORM\Table(name="users")
 * @ORM\Entity
 */
class User
Run Code Online (Sandbox Code Playgroud)

我现在想获取User实体的表名,我将如何在Symfony2控制器中执行此操作?

Ste*_*nte 103

在控制器中,您将使用:

$em = $this->getDoctrine()->getManager();
$tableName = $em->getClassMetadata('StoreBundle:User')->getTableName();
Run Code Online (Sandbox Code Playgroud)

请注意,该getClassMetadata方法返回一组有关实体的有趣信息.

  • 使用php 5.5+,您可以使用内置类的constant :: class.``$ tableName = $ em-> getClassMetadata(User :: class) - > getTableName();``` (12认同)
  • 加入表名怎么样?你知道怎么做吗? (2认同)

con*_*con 5

我需要找出一个多对多关系的映射表的名称(使用FOSUserBundle)。也许这可以帮助某人:

    $groupAssociation = $this->getEntityManager()
                             ->getClassMetadata('UOACLBundle:User')
                             ->getAssociationsByTargetClass(Group::class);

    // 'groups' is the name of the property in my User Class
    $mappingTable = $groupAssociation['groups']['joinTable']['name'];
Run Code Online (Sandbox Code Playgroud)