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