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
|
|
|
/** |
21
|
|
|
* @var ManagerRegistry |
22
|
|
|
*/ |
23
|
|
|
protected $doctrine; |
24
|
|
|
|
25
|
|
|
public function __construct(ManagerRegistry $doctrine) |
26
|
|
|
{ |
27
|
|
|
parent::__construct(); |
28
|
|
|
|
29
|
|
|
$this->doctrine = $doctrine; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* get a doctrine entity generator |
34
|
|
|
* |
35
|
|
|
* @return EntityGenerator |
36
|
|
|
*/ |
37
|
|
|
protected function getEntityGenerator() |
38
|
|
|
{ |
39
|
|
|
$entityGenerator = new EntityGenerator(); |
40
|
|
|
$entityGenerator->setGenerateAnnotations(false); |
41
|
|
|
$entityGenerator->setGenerateStubMethods(true); |
42
|
|
|
$entityGenerator->setRegenerateEntityIfExists(false); |
43
|
|
|
$entityGenerator->setUpdateEntityIfExists(true); |
44
|
|
|
$entityGenerator->setNumSpaces(4); |
45
|
|
|
$entityGenerator->setAnnotationPrefix('ORM\\'); |
46
|
|
|
|
47
|
|
|
return $entityGenerator; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get a doctrine entity manager by symfony name. |
52
|
|
|
* |
53
|
|
|
* @param string $name |
54
|
|
|
* @param int|null $shardId |
55
|
|
|
* |
56
|
|
|
* @return EntityManager |
57
|
|
|
*/ |
58
|
|
|
protected function getEntityManager($name, $shardId = null) |
59
|
|
|
{ |
60
|
|
|
$manager = $this->doctrine->getManager($name); |
61
|
|
|
|
62
|
|
|
if ($shardId) { |
|
|
|
|
63
|
|
|
if (! $manager->getConnection() instanceof PoolingShardConnection) { |
64
|
|
|
throw new LogicException(sprintf("Connection of EntityManager '%s' must implement shards configuration.", $name)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$manager->getConnection()->connect($shardId); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $manager; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get a doctrine dbal connection by symfony name. |
75
|
|
|
* |
76
|
|
|
* @param string $name |
77
|
|
|
* |
78
|
|
|
* @return Connection |
79
|
|
|
*/ |
80
|
|
|
protected function getDoctrineConnection($name) |
81
|
|
|
{ |
82
|
|
|
return $this->doctrine->getConnection($name); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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: