drkp/doctrine-metadata-configuration-factory

用于创建 Doctrine 2 ORM 配置实例的工厂类。

v1.0.1 2016-09-01 22:03 UTC

This package is auto-updated.

Last update: 2024-09-18 09:25:28 UTC


README

Build Status Code Coverage Scrutinizer Code Quality SensioLabsInsight

用于创建 Doctrine 2 ORM 配置实例的工厂类。

提供了一种简单的方式来决定在你的项目中更喜欢哪种类型的映射。

安装

composer require drkp/doctrine-metadata-configuration-factory

简单用法

<?php

$mapping = [
   "dbal" => [
       'driver' => 'pdo_sqlite',
       'path' =>  __DIR__ . '/../../../data/db.sqlite'
   ],
   "mappings" => [
       [
           "type" => "annotation",
           "namespace" => "Some\\Namespace",
           "path" => "/path/to/mapping/dir",
       ],
   ],
];

$config = new \DRKP\ZF3Doctrine\MetadataConfigurationFactory(
    $mapping['mappings'],
    $mapping['mappings'][0]['type']
);

$entityManager = \Doctrine\ORM\EntityManager::create(
    $mapping['dbal'],
    $config->make()
);

$repository = $entityManager->getRepository(\Some\Namespace::class);

高级用法

多个实体管理器和多种映射类型

<?php

use DRKP\ZF3Doctrine\MetadataConfigurationFactory;
use Doctrine\ORM\EntityManager;

$mapping = [
   "dbal" => [
       'driver' => 'pdo_sqlite',
       'path' =>  __DIR__ . '/../../../data/db.sqlite'
   ],
   "dbal1" => [
        'host' => '127.0.0.1',
        'port' => '3306',
        'dbname' => '{db}',
        'user' => '{user}',
        'password' => '{pass}',
        'charset' => 'utf8'
   ],
   "mappings" => [
       [
           "type" => "annotation",
           "namespace" => "Some\\Namespace",
           "path" => "/path/to/mapping/dir",
       ],
       [
           "type" => "yaml",
           "namespace" => "Some\\Other\\Namespace",
           "path" => "/path/to/other/mapping/dir",
       ],
   ],
];

$config = new MetadataConfigurationFactory(
    $mapping['mappings'],
    $mapping['mappings'][0]['type']
);

$defaultEntityManager = EntityManager::create(
    $mapping['dbal'],
    $config->make()
);

$config1 = new MetadataConfigurationFactory(
    $mapping['mappings'],
    'yaml'
);

$secondaryEntityManager = EntityManager::create(
    $mapping['dbal1'],
    $config1->make()
);

$repository = $defaultEntityManager->getRepository(\Some\Namespace::class);
$secondaryRepository = $secondaryEntityManager->getRepository(\Some\Other\Namespace::class);