【问题标题】:Use Doctrine in symfony front controller在 symfony 前端控制器中使用 Doctrine
【发布时间】:2015-10-12 20:06:13
【问题描述】:

是否可以在前端控制器中使用自定义的学说实体管理器?

orm:
    default_entity_manager: default
    auto_generate_proxy_classes: "%kernel.debug%"
    entity_managers:
        default:
            connection: client_connection
            mappings:
               AppBundle: ~
        master:
            connection: master_connection
            mappings:
                Master:
                    type: annotation
                    dir: %kernel.root_dir%/../src/Clients/TestBundle/Entity
                    is_bundle: false
                    alias: Master
                    prefix: Clients\TestBundle\Entity

在服务中,我可以像这样使用我的自定义实体管理器

$result = $this->entityManagerMaster->getRepository("Master:Client")->findOneBy(array("domain" => $host));

但是如何在 app.php 前端控制器中将这个 entityManager 与我的自定义连接一起使用?

【问题讨论】:

    标签: php symfony doctrine-orm


    【解决方案1】:

    如果你真的想那么你可以在 app.php 中这样做:

    $kernel = new AppKernel('prod', false);
    $kernel->loadClassCache();
    
    $kernel->boot();
    $em = $kernel->getContainer()->get('doctrine.orm.default_entity_manager');
    

    但是,正如@xabbuh 指出的那样,您可能并不想这样做。内核监听器 (http://symfony.com/doc/current/reference/events.html#kernel-request) 几乎可以肯定是要走的路。

    【讨论】:

    • 谢谢!我真的需要这个,因为我为每个租户都有自己的环境(因为缓存、数据库等)。所有租户都保存在数据库中,因此我需要教义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多