1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OrbitaleCmsBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Rock Ancelet <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Orbitale\Bundle\CmsBundle\EventListener; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\EventSubscriber; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
17
|
|
|
use Doctrine\ORM\Event\LoadClassMetadataEventArgs; |
18
|
|
|
use Doctrine\ORM\Events; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This class adds automatically the ManyToOne and OneToMany relations in Page and Category entities, |
22
|
|
|
* because it's normally impossible to do so in a mapped superclass. |
23
|
|
|
*/ |
24
|
|
|
class DoctrineMappingListener implements EventSubscriber |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $pageClass; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $categoryClass; |
35
|
|
|
|
36
|
|
|
public function __construct($pageClass, $categoryClass) |
37
|
|
|
{ |
38
|
|
|
$this->pageClass = $pageClass; |
39
|
|
|
$this->categoryClass = $categoryClass; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns an array of events this subscriber wants to listen to. |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function getSubscribedEvents() |
48
|
|
|
{ |
49
|
|
|
return [Events::loadClassMetadata]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) |
53
|
|
|
{ |
54
|
|
|
/** @var ClassMetadata $classMetadata */ |
55
|
|
|
$classMetadata = $eventArgs->getClassMetadata(); |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
if (is_a($classMetadata->getName(), $this->pageClass, true) && !$classMetadata->hasAssociation('parent')) { |
59
|
|
|
// Declare mapping for category |
60
|
|
|
$classMetadata->mapManyToOne([ |
61
|
|
|
'fieldName' => 'category', |
62
|
|
|
'targetEntity' => $this->categoryClass, |
63
|
|
|
'inversedBy' => 'pages', |
64
|
|
|
]); |
65
|
|
|
|
66
|
|
|
// Declare self-bidirectionnal mapping for parent/children |
67
|
|
|
$classMetadata->mapManyToOne([ |
68
|
|
|
'fieldName' => 'parent', |
69
|
|
|
'targetEntity' => $this->pageClass, |
70
|
|
|
'inversedBy' => 'children', |
71
|
|
|
]); |
72
|
|
|
$classMetadata->mapOneToMany([ |
73
|
|
|
'fieldName' => 'children', |
74
|
|
|
'targetEntity' => $this->pageClass, |
75
|
|
|
'mappedBy' => 'parent', |
76
|
|
|
]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (is_a($classMetadata->getName(), $this->categoryClass, true) && !$classMetadata->hasAssociation('parent')) { |
80
|
|
|
// Declare self-bidirectionnal mapping for parent/children |
81
|
|
|
$classMetadata->mapManyToOne([ |
82
|
|
|
'fieldName' => 'parent', |
83
|
|
|
'targetEntity' => $this->categoryClass, |
84
|
|
|
'inversedBy' => 'children', |
85
|
|
|
]); |
86
|
|
|
$classMetadata->mapOneToMany([ |
87
|
|
|
'fieldName' => 'children', |
88
|
|
|
'targetEntity' => $this->categoryClass, |
89
|
|
|
'mappedBy' => 'parent', |
90
|
|
|
]); |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|