2014-03-06 81 views
0

我使用此代碼獲取實體名稱;Symfony2 - 獲取實體名稱

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

     return $type; 
    } 

但是,這個功能不給我我想要的。這返回;

阿克米/ DemoBundle /實體/用戶

,但是,我想只是實體名稱;所以「用戶」。

我該怎麼辦?我不想使用strstrip等功能。

回答

2

編輯:

嘗試ReflectionClass:

$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; 
} 
+0

因爲它不僅方式。我找到了; $ type = new \ ReflectionClass($ data); $ type = $ type-> getShortName(); http://www.php.net/manual/en/reflectionclass.getshortname.php thnx求助! – user3132616

+0

是的,在同一時間! –