Search code examples
phpdoctrine-ormsilex

How can I use `orm.ems` when declaring multiple Doctrine EntityManagers in Silex?


I am writing a PHP Silex application and I want to use multiple OMS Entity Managers. I have followed the instructions for the Silex\Provider\DoctrineServiceProvider to add multiple named databases

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

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

And I have configured the EntityManagers thus:

$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']
    ]
];

I then try to perform findAll() operation on a repository and my application crashes, or rather the display states there has been an Exception but I am not seeing one in the logs nor does try ... catch show anything.

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);

enter image description here

I am running the app using PHP built in web server, and the logs are as follows:

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)

I am stumped. If I use just one EM in the configuration my application works without issue, it only happens when I try to use the orm.ems options. Is there something very simple that I am missing.

I am using PHP 7.1 and Silex 2. This code is running on Windows, I have not yet tried it on Linux, but given that one EM works I feel that this is not the issue.


Solution

  • I have fixed my issue. It turned out to be the path to the database, because I was using " the \ was escaping the characters which meant that the DB file could not be found.

    I found this after realising that I was not running in debug mode, when I did I got the error message I needed. I added this to my code:

    $app['debug'] = true