sebrogala/xsv-doctrine

Doctrine 2 集成到 Zend Expressive 的中间件

3.1.0 2018-07-19 13:43 UTC

This package is not auto-updated.

Last update: 2024-09-15 06:00:01 UTC


README

中间件模块,用于将 Doctrine 2 集成到 Zend Expressive 3。

基本用法

data 目录复制 doctrine.local.php.distconfig/autoload 文件夹,并在保存数据库凭证时删除 .dist

config 目录内需要文件 cli-config.php.dist,因为它是负责 Doctrine 控制台使用的。

config/config.php 中启用模块

$configManager = new ConfigManager([
    //...
    Xsv\Doctrine\ConfigProvider::class,
    //...
    new PhpFileProvider('config/autoload/{{,*.}global,{,*.}local}.php'),
]);

要获取实体管理器,只需使用

$em = $container->get(\Doctrine\ORM\EntityManager::class);

或作为别名

$em = $container->get('entity-manager');

创建额外的实体管理器

要创建一个,需要创建一个新的类或接口,并为其名称提供新的配置。例如:在 src/App/Service 目录内创建 SomeEntityManager 文件

<?php

namespace App\Service;

interface SomeEntityManager {}

现在在配置文件中,例如 config/autoload/doctrine.local.php

<?php

return [
    'doctrine' => [
        \Doctrine\ORM\EntityManager::class => [
            /* standard config here */
        ],
        \App\Service\SomeEntityManager::class => [
            /* Config for another EntityManager */
        ],
    ],
    'dependencies' => [
        'factories' => [
            \App\Service\SomeEntityManager::class => EntityManagerFactory::class,
        ],
    ],
];

要使用该实体管理器,只需通过新创建的接口从 ServiceManager 中获取

$someEntityManager = $container->get(\App\Service\SomeEntityManager::class);