1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Subscriber; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Event\LoadClassMetadataEventArgs; |
6
|
|
|
|
7
|
|
|
class TableSchemaSubscriber implements \Doctrine\Common\EventSubscriber |
8
|
|
|
{ |
9
|
|
|
private $schemaprefix; |
10
|
|
|
|
11
|
51 |
|
public function __construct($schemaprefix) |
12
|
|
|
{ |
13
|
51 |
|
$this->schemaprefix = $schemaprefix; |
14
|
51 |
|
} |
15
|
|
|
|
16
|
51 |
|
public function getSubscribedEvents() |
17
|
|
|
{ |
18
|
51 |
|
return array('loadClassMetadata'); |
19
|
|
|
} |
20
|
|
|
|
21
|
37 |
|
public function loadClassMetadata(LoadClassMetadataEventArgs $args) |
22
|
|
|
{ |
23
|
37 |
|
$classMetadata = $args->getClassMetadata(); |
24
|
37 |
|
if (!$this->schemaprefix || ($classMetadata->isInheritanceTypeSingleTable() && !$classMetadata->isRootEntity())) { |
25
|
37 |
|
return; |
26
|
|
|
} |
27
|
|
|
$classMetadata->setPrimaryTable(array('name' => $this->schemaprefix.'.'.$classMetadata->getTableName())); |
28
|
|
|
foreach ($classMetadata->getAssociationMappings() as $fieldName => $mapping) { |
29
|
|
|
if (\Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY == $mapping['type'] && |
30
|
|
|
isset($classMetadata->associationMappings[$fieldName]['joinTable']['name']) |
31
|
|
|
) { |
32
|
|
|
$mappedTableName = $classMetadata->associationMappings[$fieldName]['joinTable']['name']; |
|
|
|
|
33
|
|
|
$classMetadata->associationMappings[$fieldName]['joinTable']['name'] = $this->schemaprefix.'.'.$mappedTableName; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ($classMetadata->isIdGeneratorSequence()) { |
38
|
|
|
$newDefinition = $classMetadata->sequenceGeneratorDefinition; |
|
|
|
|
39
|
|
|
$newDefinition['sequenceName'] = $this->schemaprefix.'.'.$newDefinition['sequenceName']; |
40
|
|
|
|
41
|
|
|
$classMetadata->setSequenceGeneratorDefinition($newDefinition); |
42
|
|
|
$em = $args->getEntityManager(); |
43
|
|
|
if (isset($classMetadata->idGenerator)) { |
44
|
|
|
$sequenceGenerator = new \Doctrine\ORM\Id\SequenceGenerator( |
45
|
|
|
$em->getConfiguration()->getQuoteStrategy()->getSequenceName( |
46
|
|
|
$newDefinition, |
47
|
|
|
$classMetadata, |
48
|
|
|
$em->getConnection()->getDatabasePlatform() |
49
|
|
|
), |
50
|
|
|
$newDefinition['allocationSize'] |
51
|
|
|
); |
52
|
|
|
$classMetadata->setIdGenerator($sequenceGenerator); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.