I have a function that needs to know the bundle shortcut name for an entity that is passed in.
public function doSomething($entity) {
$bundleShortcut = SOMEFUNCTION($entity);
// ... do other stuff and return a value ...
}
I would love to have it return the bundle shortcut name of my entity:
GutensiteCmsBundle:ViewVersion
Is this possible? Does the entity manager have access to this metadata somehow?
I'm aware I could pre-register the name in all my entities:
class ViewVersion {
protected $bundleName = 'GutensiteCmsBundle:ViewVersion';
public function getBundleName() {
return $this->bundleName;
}
}
Then I can do:
$entity->getBundleName();
But that's lame.
Based on @Chamlee's answer, this is the function I use:
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];
}
So for the following entities here are the return values:
// Entity
Gutensite\ArticleBundle\Entity\Article
// Returns
GutensiteArticleBundle:Article
// Entity
Gutensite\CmsBundle\Entity\View\ViewVersion
// Returns
GutensiteArticleBundle:View\ViewVersion
Try looking at this :
$className = explode("\\", get_class($document));
It returns you an array with all you need ;) If you make and echo of the $className you'll see the structure and I think it could fit with your problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.