1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
use Doctrine\DBAL\Sharding\PoolingShardConnection; |
8
|
|
|
use Doctrine\ORM\EntityManager; |
9
|
|
|
use Doctrine\ORM\Tools\EntityGenerator; |
10
|
|
|
use LogicException; |
11
|
|
|
use Symfony\Component\Console\Command\Command; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Base class for Doctrine console commands to extend from. |
15
|
|
|
* |
16
|
|
|
* @internal |
17
|
|
|
*/ |
18
|
|
|
abstract class DoctrineCommand extends Command |
19
|
|
|
{ |
20
|
|
|
protected $doctrine; |
21
|
|
|
|
22
|
|
|
public function __construct(ManagerRegistry $doctrine) |
23
|
|
|
{ |
24
|
|
|
parent::__construct(); |
25
|
|
|
|
26
|
|
|
$this->doctrine = $doctrine; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* get a doctrine entity generator |
31
|
|
|
* |
32
|
|
|
* @return EntityGenerator |
33
|
|
|
*/ |
34
|
|
|
protected function getEntityGenerator() |
35
|
|
|
{ |
36
|
|
|
$entityGenerator = new EntityGenerator(); |
37
|
|
|
$entityGenerator->setGenerateAnnotations(false); |
38
|
|
|
$entityGenerator->setGenerateStubMethods(true); |
39
|
|
|
$entityGenerator->setRegenerateEntityIfExists(false); |
40
|
|
|
$entityGenerator->setUpdateEntityIfExists(true); |
41
|
|
|
$entityGenerator->setNumSpaces(4); |
42
|
|
|
$entityGenerator->setAnnotationPrefix('ORM\\'); |
43
|
|
|
|
44
|
|
|
return $entityGenerator; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get a doctrine entity manager by symfony name. |
49
|
|
|
* |
50
|
|
|
* @param string $name |
51
|
|
|
* @param int|null $shardId |
52
|
|
|
* |
53
|
|
|
* @return EntityManager |
54
|
|
|
*/ |
55
|
|
|
protected function getEntityManager($name, $shardId = null) |
56
|
|
|
{ |
57
|
|
|
$manager = $this->doctrine->getManager($name); |
58
|
|
|
|
59
|
|
|
if ($shardId) { |
|
|
|
|
60
|
|
|
if (! $manager->getConnection() instanceof PoolingShardConnection) { |
61
|
|
|
throw new LogicException(sprintf("Connection of EntityManager '%s' must implement shards configuration.", $name)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$manager->getConnection()->connect($shardId); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $manager; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get a doctrine dbal connection by symfony name. |
72
|
|
|
* |
73
|
|
|
* @param string $name |
74
|
|
|
* |
75
|
|
|
* @return Connection |
76
|
|
|
*/ |
77
|
|
|
protected function getDoctrineConnection($name) |
78
|
|
|
{ |
79
|
|
|
return $this->doctrine->getConnection($name); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: