【问题标题】:Symfony2 Doctrine ORM Manager named "customer" does not exist名为“客户”的 Symfony2 Doctrine ORM 管理器不存在
【发布时间】:2015-10-19 23:34:49
【问题描述】:

您好,我从 Symfony2 文档中复制了使用两个不同数据库连接的示例:Symfony2 multiple connections documentation 因此,Symfony 没有找到客户实体管理器。 (参数定义正确)

doctrine:
dbal:
    default_connection: default
    connections:
        default:
            driver:   "%database_driver%"
            host:     "%database_host%"
            port:     "%database_port%"
            dbname:   "%database_name%"
            user:     "%database_user%"
            password: "%database_password%"
            charset:  UTF8
        customer:
            driver:   "%database_driver%"
            host:     "%database_host2%"
            port:     "%database_port2%"
            dbname:   "%database_name2%"
            user:     "%database_user2%"
            password: "%database_password2%"
            charset:  UTF8

orm:
    default_entity_manager: default
    entity_managers:
        default:
            connection: default
            mappings:
                AppBundle:  ~
        customer:
            connection: customer
            mappings:
                AppBundle: ~

控制器看起来像这样:

class DefaultController extends Controller
{
/**
 * @Route("/", name="homepage")
 */
public function indexAction(Request $request)
{
    $em = $this->get('doctrine')->getManager();
    $em = $this->get('doctrine')->getManager('default');
    $em = $this->get('doctrine.orm.default_entity_manager');

    // Both of these return the "customer" entity manager
    $customerEm = $this->get('doctrine')->getManager('customer');
    $customerEm = $this->get('doctrine.orm.customer_entity_manager');
    return $this->render('default/index.html.twig', array(
        'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
    ));
}

我应该得到客户实体管理器,但是 Symfony 会在消息中抛出一个无效参数异常。

[2015-10-19 23:19:18] request.CRITICAL: Uncaught PHP Exception 
InvalidArgumentException: "Doctrine ORM Manager named "customer" does 
not exist." at /srv/www/htdocs/symfony/my_project_name/app/cache
/prod/classes.php line 7344 {"exception":"[object] 
(InvalidArgumentException(code: 0): Doctrine ORM Manager named 
\"customer\" does not exist. at /srv/www/htdocs/symfony/my_project_name
/app/cache/prod/classes.php:7344)"} []

我用 php app/console cache:clear 清理了缓存,但这没有帮助。

【问题讨论】:

  • cache:clear 默认清除dev 环境,但查看错误日志,您会在prod 环境中收到错误。要清除生产环境中的缓存,请添加-e prod 标志,例如cache:clear -e prod 并刷新您的页面。

标签: php symfony doctrine-orm doctrine


【解决方案1】:

Artamiel 在评论中给出了正确答案:

Symfony2 Doctrine ORM Manager named "customer" does not exist

cache:clear 默认清除开发环境,但查看错误日志,您会在生产环境中收到错误。要清除生产环境中的缓存,请添加 -e prod 标志,例如缓存:clear -e prod 并刷新页面。

【讨论】:

    【解决方案2】:

    首先,我认为,在你的控制器中声明一个实体管理器就足够了:

    / *** /   
    public function indexAction(Request $request)
    {
        $em = $this->get('doctrine')->getManager('default');
    
        $customerEm = $this->get('doctrine')->getManager('customer');
    
        / *** /
    }
    

    其次,您确定可以在映射配置中声明相同的捆绑包(在这种情况下为AppBundle: ~)吗?

    【讨论】:

      猜你喜欢
      • 2016-10-25
      • 1970-01-01
      • 2011-12-10
      • 2012-01-18
      • 2023-03-10
      • 2015-04-19
      • 2015-01-19
      • 2019-11-10
      • 2017-05-23
      相关资源
      最近更新 更多