Symfony 3从另一个bundle`s存储库访问实体管理器

时间:2017-11-21 15:40:05

标签: php symfony

在我的项目中,我有2个捆绑包(AppBundle和CommonBundle)和2个不同的数据库,每个捆绑包1个。在我的配置中,它显示如下:

orm:
    default_entity_manager:   default
    auto_generate_proxy_classes: "%kernel.debug%"
    entity_managers:
        default:
            connection:       default
            auto_mapping:     true
            naming_strategy:  doctrine.orm.naming_strategy.underscore
        common:
            connection:       common_data
            auto_mapping:     false
            mappings:
                CommonBundle:     ~
                #FOSUserBundle: ~
            naming_strategy:  doctrine.orm.naming_strategy.underscore

现在我在CommonBundle的存储库中,我想访问AppBundle的连接以从其数据库中获取一些数据,如下所示:

<?php

namespace CommonBundle\Repository;

use Doctrine\ORM\EntityRepository;
use CommonBundle\Entity\Audit;

class AuditRepository extends EntityRepository
{
  public function MyFunc()
    {
        $em =$this->getEntityManager();
        $em_def =$this->getEntityManager('default');
        $em_def->getRepository('AppBundle:Info')->getInfo('myInfo');//ERROR here
    }
}

但我得到以下错误:

Uncaught PHP Exception Doctrine\ORM\ORMException: "Unknown Entity namespace alias 'AppBundle'." at ...

所以任何想法如何解决它都会受到欢迎。谢谢。

1 个答案:

答案 0 :(得分:-1)

您需要使用完整路径,因为您在另一个Bundle中 尝试这个或类似的完整路径:

$em_def->getRepository('AppBundle\Entity\Info')->getInfo('myInfo');
相关问题