drkp / doctrine-metadata-configuration-factory
Factory class to create Doctrine 2 ORM Configuration instances.
v1.0.1
2016-09-01 22:03 UTC
Requires
- php: ^5.5 || ^7.0
- doctrine/orm: ^2.5
Requires (Dev)
- henrikbjorn/phpspec-code-coverage: ^3.0
- memio/spec-gen: ^0.6
- phpspec/phpspec: ^3.0@beta
This package is auto-updated.
Last update: 2024-11-18 09:49:52 UTC
README
Factory class to create Doctrine 2 ORM Configuration instances.
Provides simple way to decide what kind of mapping you prefer in your project.
Instalation
composer require drkp/doctrine-metadata-configuration-factory
Simple Usage:
<?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);
Advanced usage
Multiple Entity Managers and multiple mapping types
<?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);