本文介绍了从实体获取包名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从实体中获取捆绑包的名称?

是否有任何功能或服务可以获得此功能?

$artist = new Artist();$bundleName = 艺术家->getBundleName();回声 $bundleName

myCompanyArtistBundle

解决方案

我找到了一个解决方案,但不知道是否合适 :

get('kernel')->getBundles();$bundleName = '';foreach($bundles as $type=>$bundle){$className = get_class($bundle);$entityClass = substr($rootEntityName,0,strpos($rootEntityName,'\\Entity\\'));if(strpos($className,$entityClass)=== FALSE){echo get_class($bundle).'<br>';echo $type.'
';}别的{$bundleName = $type;}}回声 $bundleName;

How can you get the bundle's name from an entity?

is there any function or service to get this?

$artist = new Artist();

$bundleName = artist->getBundleName();

echo  $bundleName
解决方案

I found a solution but don't know if it's a good one :

<?php
    $rootEntityName = "company\myNamespace\Entity\user";

    $bundles = $context->get('kernel')->getBundles();
    $bundleName = '';

    foreach($bundles as $type=>$bundle){
        $className = get_class($bundle);

        $entityClass = substr($rootEntityName,0,strpos($rootEntityName,'\\Entity\\'));

        if(strpos($className,$entityClass)=== FALSE){
            echo get_class($bundle).'<br>';
            echo $type.'<br>';
        }else{
            $bundleName = $type;
        }
    }
    echo $bundleName;

这篇关于从实体获取包名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 02:11