【问题标题】:Symfony2 - Get Entity NameSymfony2 - 获取实体名称
【发布时间】:2014-03-06 12:34:41
【问题描述】:

我使用此代码获取实体名称;

function setTypeFunction($data)
    {
        $em = $this->container->get('doctrine')->getManager();
        $type = $em->getClassMetadata(get_class($data))->getName();

        return $type;
    }

但是,这个功能没有给我我想要的。这个返回;

Acme/DemoBundle/Entity/User

但是,我只想要实体名称;所以“用户”。

我该怎么办?我不想使用 strstrip 等功能。

【问题讨论】:

    标签: php symfony namespaces entity


    【解决方案1】:

    编辑:

    试试反射类:

    $ref = new ReflectionClass($data);
    $ref->getShortName()
    

    为什么不想使用函数?

    function setTypeFunction($data)
    {
        $em = $this->container->get('doctrine')->getManager();
        $type = $em->getClassMetadata(get_class($data))->getName();
        $type = end(explode("/",$type));
        return $type;
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多