Skip to content

Commit 653df26

Browse files
committed
Multiple Entity Managers and Connections
1 parent 34417df commit 653df26

8 files changed

+259
-108
lines changed

src/Console/DqlCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php namespace Mitch\LaravelDoctrine\Console;
22

33
use Doctrine\Common\Util\Debug;
4-
use Doctrine\ORM\EntityManagerInterface;
4+
use Doctrine\Common\Persistence\ManagerRegistry;
55
use Symfony\Component\Console\Input\InputArgument;
66
use Symfony\Component\Console\Input\InputOption;
77

@@ -28,11 +28,11 @@ class DqlCommand extends Command
2828
*/
2929
private $entityManager;
3030

31-
public function __construct(EntityManagerInterface $entityManager)
31+
public function __construct(ManagerRegistry $registry)
3232
{
3333
parent::__construct();
3434

35-
$this->entityManager = $entityManager;
35+
$this->entityManager = $registry->getManager();
3636
}
3737

3838
public function fire()
@@ -53,4 +53,4 @@ protected function getOptions()
5353
['hydrate', null, InputOption::VALUE_OPTIONAL, 'Hydrate type. Available: object, array, scalar, single_scalar, simpleobject']
5454
];
5555
}
56-
}
56+
}
Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php namespace Mitch\LaravelDoctrine\Console;
22

33
use Illuminate\Console\Command;
4-
use Doctrine\ORM\EntityManagerInterface;
4+
use Doctrine\Common\Persistence\ManagerRegistry;
55

66
class GenerateProxiesCommand extends Command
77
{
@@ -20,37 +20,40 @@ class GenerateProxiesCommand extends Command
2020
protected $description = 'Generate proxies for entities.';
2121

2222
/**
23-
* The Entity Manager
23+
* The ManagerRegistry
2424
*
25-
* @var \Doctrine\ORM\EntityManagerInterface
25+
* @var \Doctrine\Common\Persistence\ManagerRegistry
2626
*/
27-
private $entityManager;
27+
private $registry;
2828

29-
public function __construct(EntityManagerInterface $entityManager)
29+
public function __construct(ManagerRegistry $registry)
3030
{
3131
parent::__construct();
3232

33-
$this->entityManager = $entityManager;
33+
$this->registry = $registry;
3434
}
3535

3636
public function fire()
3737
{
3838
$this->info('Starting proxy generation....');
39-
$metadata = $this->entityManager->getMetadataFactory()->getAllMetadata();
40-
if (empty($metadata)) {
41-
$this->error('No metadata found to generate any entities.');
42-
exit;
39+
foreach ($this->registry->getManagerNames() as $key => $value) {
40+
$manager = $this->registry->getManager($key);
41+
$metadata = $manager->getMetadataFactory()->getAllMetadata();
42+
if (empty($metadata)) {
43+
$this->error('No metadata found to generate any entities.');
44+
exit;
45+
}
46+
$directory = $this->laravel['config']['doctrine::doctrine.proxy.directory'];
47+
if ( ! $directory) {
48+
$this->error('The proxy directory has not been set.');
49+
exit;
50+
}
51+
$this->info('Processing entities:');
52+
foreach ($metadata as $item) {
53+
$this->line($item->name);
54+
}
55+
$manager->getProxyFactory()->generateProxyClasses($metadata, $directory);
56+
$this->info('Proxies have been created.');
4357
}
44-
$directory = $this->laravel['config']['doctrine::doctrine.proxy.directory'];
45-
if ( ! $directory) {
46-
$this->error('The proxy directory has not been set.');
47-
exit;
48-
}
49-
$this->info('Processing entities:');
50-
foreach ($metadata as $item) {
51-
$this->line($item->name);
52-
}
53-
$this->entityManager->getProxyFactory()->generateProxyClasses($metadata, $directory);
54-
$this->info('Proxies have been created.');
5558
}
56-
}
59+
}

src/Console/SchemaCreateCommand.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Illuminate\Console\Command;
44
use Doctrine\ORM\Tools\SchemaTool;
5-
use Doctrine\ORM\Mapping\ClassMetadataFactory;
5+
use Doctrine\Common\Persistence\ManagerRegistry;
66
use Symfony\Component\Console\Input\InputOption;
77

88
class SchemaCreateCommand extends Command
@@ -29,18 +29,18 @@ class SchemaCreateCommand extends Command
2929
private $tool;
3030

3131
/**
32-
* The class metadata factory
32+
* The ManagerRegistry
3333
*
34-
* @var \Doctrine\ORM\Tools\SchemaTool
34+
* @var \Doctrine\Common\Persistence\ManagerRegistry
3535
*/
36-
private $metadata;
36+
private $registry;
3737

38-
public function __construct(SchemaTool $tool, ClassMetadataFactory $metadata)
38+
public function __construct(SchemaTool $tool, ManagerRegistry $registry)
3939
{
4040
parent::__construct();
4141

4242
$this->tool = $tool;
43-
$this->metadata = $metadata;
43+
$this->registry = $registry;
4444
}
4545

4646
/**
@@ -52,11 +52,17 @@ public function fire()
5252
{
5353
if ($this->option('sql')) {
5454
$this->info('Outputting create query:'.PHP_EOL);
55-
$sql = $this->tool->getCreateSchemaSql($this->metadata->getAllMetadata());
56-
$this->info(implode(';'.PHP_EOL, $sql));
55+
foreach ($this->registry->getManagerNames() as $key => $value) {
56+
$manager = $this->registry->getManager($key);
57+
$sql = $this->tool->getCreateSchemaSql($manager->getMetadataFactory()->getAllMetadata());
58+
$this->info(implode(';'.PHP_EOL, $sql));
59+
}
5760
} else {
5861
$this->info('Creating database schema...');
59-
$this->tool->createSchema($this->metadata->getAllMetadata());
62+
foreach ($this->registry->getManagerNames() as $key => $value) {
63+
$manager = $this->registry->getManager($key);
64+
$this->tool->createSchema($manager->getMetadataFactory()->getAllMetadata());
65+
}
6066
$this->info('Schema has been created!');
6167
}
6268
}
@@ -67,4 +73,4 @@ protected function getOptions()
6773
['sql', false, InputOption::VALUE_NONE, 'Dumps SQL query and does not execute creation.']
6874
];
6975
}
70-
}
76+
}

src/Console/SchemaDropCommand.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<?php namespace Mitch\LaravelDoctrine\Console;
1+
<?php namespace Mitch\LaravelDoctrine\Console;
22

33
use Illuminate\Console\Command;
44
use Doctrine\ORM\Tools\SchemaTool;
5-
use Doctrine\ORM\Mapping\ClassMetadataFactory;
5+
use Doctrine\Common\Persistence\ManagerRegistry;
66
use Symfony\Component\Console\Input\InputOption;
77

88
class SchemaDropCommand extends Command
@@ -29,18 +29,18 @@ class SchemaDropCommand extends Command
2929
private $tool;
3030

3131
/**
32-
* The class metadata factory
33-
*
34-
* @var \Doctrine\ORM\Tools\SchemaTool
35-
*/
36-
private $metadata;
32+
* The ManagerRegistry
33+
*
34+
* @var \Doctrine\Common\Persistence\ManagerRegistry
35+
*/
36+
private $registry;
3737

38-
public function __construct(SchemaTool $tool, ClassMetadataFactory $metadata)
38+
public function __construct(SchemaTool $tool, ManagerRegistry $registry)
3939
{
4040
parent::__construct();
4141

4242
$this->tool = $tool;
43-
$this->metadata = $metadata;
43+
$this->registry = $registry;
4444
}
4545

4646
/**
@@ -50,18 +50,21 @@ public function __construct(SchemaTool $tool, ClassMetadataFactory $metadata)
5050
*/
5151
public function fire()
5252
{
53-
$sql = $this->tool->getDropSchemaSQL($this->metadata->getAllMetadata());
54-
if (empty($sql)) {
55-
$this->info('Current models do not exist in schema.');
56-
return;
57-
}
58-
if ($this->option('sql')) {
59-
$this->info('Outputting drop query:');
60-
$this->info(implode(';' . PHP_EOL, $sql));
61-
} else {
62-
$this->info('Dropping database schema....');
63-
$this->tool->dropSchema($this->metadata->getAllMetadata());
64-
$this->info('Schema has been dropped!');
53+
foreach ($this->registry->getManagerNames() as $key => $value) {
54+
$manager = $this->registry->getManager($key);
55+
$sql = $this->tool->getDropSchemaSQL($manager->getMetadataFactory()->getAllMetadata());
56+
if (empty($sql)) {
57+
$this->info('Current models do not exist in schema.');
58+
continue;
59+
}
60+
if ($this->option('sql')) {
61+
$this->info('Outputting drop query:');
62+
$this->info(implode(';' . PHP_EOL, $sql));
63+
} else {
64+
$this->info('Dropping database schema....');
65+
$this->tool->dropSchema($manager->getMetadataFactory()->getAllMetadata());
66+
$this->info('Schema has been dropped!');
67+
}
6568
}
6669
}
6770

@@ -72,4 +75,3 @@ protected function getOptions()
7275
];
7376
}
7477
}
75-

src/Console/SchemaUpdateCommand.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<?php namespace Mitch\LaravelDoctrine\Console;
1+
<?php namespace Mitch\LaravelDoctrine\Console;
22

33
use Illuminate\Console\Command;
44
use Doctrine\ORM\Tools\SchemaTool;
5-
use Doctrine\ORM\Mapping\ClassMetadataFactory;
5+
use Doctrine\Common\Persistence\ManagerRegistry;
66
use Symfony\Component\Console\Input\InputOption;
77

88
class SchemaUpdateCommand extends Command
@@ -29,18 +29,18 @@ class SchemaUpdateCommand extends Command
2929
private $tool;
3030

3131
/**
32-
* The class metadata factory
33-
*
34-
* @var \Doctrine\ORM\Tools\SchemaTool
35-
*/
36-
private $metadata;
32+
* The ManagerRegistry
33+
*
34+
* @var \Doctrine\Common\Persistence\ManagerRegistry
35+
*/
36+
private $registry;
3737

38-
public function __construct(SchemaTool $tool, ClassMetadataFactory $metadata)
38+
public function __construct(SchemaTool $tool, ManagerRegistry $registry)
3939
{
4040
parent::__construct();
4141

4242
$this->tool = $tool;
43-
$this->metadata = $metadata;
43+
$this->registry = $registry;
4444
}
4545

4646
/**
@@ -52,18 +52,21 @@ public function fire()
5252
{
5353
$this->info('Checking if database needs updating....');
5454
$clean = $this->option('clean');
55-
$sql = $this->tool->getUpdateSchemaSql($this->metadata->getAllMetadata(), $clean);
56-
if (empty($sql)) {
57-
$this->info('No updates found.');
58-
return;
59-
}
60-
if ($this->option('sql')) {
61-
$this->info('Outputting update query:');
62-
$this->info(implode(';' . PHP_EOL, $sql));
63-
} else {
64-
$this->info('Updating database schema....');
65-
$this->tool->updateSchema($this->metadata->getAllMetadata());
66-
$this->info('Schema has been updated!');
55+
foreach ($this->registry->getManagerNames() as $key => $value) {
56+
$manager = $this->registry->getManager($key);
57+
$sql = $this->tool->getUpdateSchemaSql($manager->getMetadataFactory()->getAllMetadata(), $clean);
58+
if (empty($sql)) {
59+
$this->info('No updates found.');
60+
continue;
61+
}
62+
if ($this->option('sql')) {
63+
$this->info('Outputting update query:');
64+
$this->info(implode(';' . PHP_EOL, $sql));
65+
} else {
66+
$this->info('Updating database schema....');
67+
$this->tool->updateSchema($this->metadata->getAllMetadata());
68+
$this->info('Schema has been updated!');
69+
}
6770
}
6871
}
6972

@@ -75,4 +78,3 @@ protected function getOptions()
7578
];
7679
}
7780
}
78-

src/DoctrineUserProvider.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace Mitch\LaravelDoctrine;
22

3-
use Doctrine\ORM\EntityManager;
3+
use Doctrine\Common\Persistence\ManagerRegistry;
44
use Doctrine\ORM\EntityRepository;
55
use Illuminate\Auth\UserInterface;
66
use Illuminate\Auth\UserProviderInterface;
@@ -13,7 +13,7 @@ class DoctrineUserProvider implements UserProviderInterface
1313
*/
1414
private $hasher;
1515
/**
16-
* @var EntityManager
16+
* @var \Doctrine\ORM\EntityManager
1717
*/
1818
private $entityManager;
1919
/**
@@ -23,13 +23,13 @@ class DoctrineUserProvider implements UserProviderInterface
2323

2424
/**
2525
* @param HasherInterface $hasher
26-
* @param EntityManager $entityManager
26+
* @param ManagerRegistry $registry
2727
* @param $entity
2828
*/
29-
public function __construct(HasherInterface $hasher, EntityManager $entityManager, $entity)
29+
public function __construct(HasherInterface $hasher, ManagerRegistry $registry, $entity)
3030
{
3131
$this->hasher = $hasher;
32-
$this->entityManager = $entityManager;
32+
$this->entityManager = $registry->getManager();
3333
$this->entity = $entity;
3434
}
3535
/**

0 commit comments

Comments
 (0)