【问题标题】:How can I use `orm.ems` when declaring multiple Doctrine EntityManagers in Silex?在 Silex 中声明多个 Doctrine EntityManager 时如何使用“orm.ems”?
【发布时间】:2017-10-05 13:46:21
【问题描述】:

我正在编写一个 PHP Silex 应用程序,并且我想使用多个 OMS 实体管理器。我已按照Silex\Provider\DoctrineServiceProvider 的说明添加多个命名数据库

$dbs_options = [
    "base" => [
        "driver" => "pdo_sqlite",
        "path" => "C:\users\russells\workspaces\turlesystems\database\dev.sqlite"
    ]
];

$app -> register(new DoctrineServiceProvider, ['dbs.options' => $dbs_options]);

我已经配置了 EntityManagers:

$mappings =  [
    [
        'type' => 'annotation',
        'namespace' => 'Turtle\Model\Entity',
        'path' => "c:\users\russells\workspaces\fisheagle\Turtle\Model\Entity",
        'use_simple_annotation_reader' => false
    ]
];

$app -> register(new DoctrineOrmServiceProvider, [
        'orm.proxies_dir' => Utils::joinPaths($app -> config -> appRoot, 'running', 'proxies'),
        'orm.em.options' => [
            'mappings' => $mappings
        ]
    ]
);

$app['orm.ems.default'] = 'basedb';
$app['orm.ems.options'] = [
    'basedb' => [
        'connection' => 'base',
        'mappings' => $app['orm.em.options']['mappings']
    ]
];

然后我尝试在存储库上执行 findAll() 操作,但我的应用程序崩溃了,或者更确切地说,显示状态存在异常,但我在日志中没有看到异常,try ... catch 也没有显示任何内容。

dump("Retrieve Entity Manager");
$em = $this -> app['orm.ems']['basedb'];
dump("Get Repo");
$repo = $em -> getRepository("\Turtle\Model\Entity\WebsiteDomain");
dump("Get all items");
$items = $repo -> findAll();
dump("Display items");
dump($items);

我正在使用PHP内置的Web服务器运行该应用程序,日志如下:

PHP 7.1.8 Development Server started at Thu Oct  5 14:42:32 2017
Listening on http://0.0.0.0:10000
Document root is C:\Users\russells\workspaces\turtlesystems\fisheagle\web\local
Press Ctrl-C to quit.
[Thu Oct  5 14:43:43 2017] 127.0.0.1:65116 [200]: /
[Thu Oct  5 14:43:45 2017] 127.0.0.1:65117 Invalid request (Unexpected EOF)
[Thu Oct  5 14:43:45 2017] 127.0.0.1:65119 Invalid request (Unexpected EOF)

我被难住了。如果我在配置中只使用一个 EM,我的应用程序可以正常工作,只有在我尝试使用 orm.ems 选项时才会发生这种情况。有没有我遗漏的非常简单的东西。

我使用的是 PHP 7.1 和 Silex 2。这段代码在 Windows 上运行,我还没有在 Linux 上尝试过,但是鉴于一个 EM 可以工作,我感觉这不是问题。

【问题讨论】:

    标签: php doctrine-orm silex


    【解决方案1】:

    我已经解决了我的问题。原来是数据库的路径,因为我使用的是“\正在转义字符,这意味着找不到数据库文件。

    我意识到我没有在调试模式下运行后发现了这个,当我收到我需要的错误消息时。我将此添加到我的代码中:

    $app['debug'] = true
    

    【讨论】:

      猜你喜欢
      • 2013-04-08
      • 2017-03-17
      • 1970-01-01
      • 2023-03-19
      • 2021-09-16
      • 1970-01-01
      • 2017-08-30
      • 2014-07-23
      • 1970-01-01
      相关资源
      最近更新 更多