簡體   English   中英

提取Symfony實體的命名空間或捆綁快捷方式

[英]Extract the Namespace or Bundle Shortcut for a Symfony Entity

我有一個函數需要知道傳入實體的捆綁軟件快捷方式名稱。

public function doSomething($entity) {
     $bundleShortcut = SOMEFUNCTION($entity);
     // ... do other stuff and return a value ...
}

我希望它返回我實體的捆綁軟件快捷方式名稱:

GutensiteCmsBundle:ViewVersion

這可能嗎? 實體管理員是否可以某種方式訪問​​此元數據?

我知道我可以在所有實體中預先注冊該名稱:

class ViewVersion {
    protected $bundleName = 'GutensiteCmsBundle:ViewVersion';
    public function getBundleName() {
        return $this->bundleName;
    }
}

然后我可以做:

$entity->getBundleName();

但這很la腳。

基於@Chamlee的答案,這是我使用的功能:

public function getEntityBundleShortcut($entity) {
    // wrap in EntityManager's Class Metadata function avoid problems with cached proxy classes
    $path = explode('\Entity\\', $this->em->getClassMetadata(get_class($entity))->getName());
    return str_replace('\\', '', $path[0]).':'.$path[1];
}

因此,對於以下實體,這是返回值:

// Entity
Gutensite\ArticleBundle\Entity\Article
// Returns
GutensiteArticleBundle:Article


// Entity
Gutensite\CmsBundle\Entity\View\ViewVersion
// Returns
GutensiteArticleBundle:View\ViewVersion

嘗試看這個:

$className = explode("\\", get_class($document));

它返回一個數組,其中包含您所需要的全部;)如果您對$ className進行了構造和回顯,則將看到該結構,我認為它可能適合您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM