1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* WellCommerce Open-Source E-Commerce Platform |
4
|
|
|
* |
5
|
|
|
* This file is part of the WellCommerce package. |
6
|
|
|
* |
7
|
|
|
* (c) Adam Piotrowski <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, |
10
|
|
|
* please view the LICENSE file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WellCommerce\Bundle\AppBundle\EventListener; |
14
|
|
|
|
15
|
|
|
use Doctrine\Common\EventSubscriber; |
16
|
|
|
use Doctrine\ORM\Event\LoadClassMetadataEventArgs; |
17
|
|
|
use Doctrine\ORM\Events; |
18
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
19
|
|
|
use WellCommerce\Bundle\AppBundle\Entity\Shop; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class ShopableSubscriber |
23
|
|
|
* |
24
|
|
|
* @author Adam Piotrowski <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ShopableSubscriber implements EventSubscriber |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var \Doctrine\ORM\Mapping\ClassMetadataInfo |
30
|
|
|
*/ |
31
|
|
|
protected $classMetadata; |
32
|
|
|
|
33
|
|
|
public function getSubscribedEvents() |
34
|
|
|
{ |
35
|
|
|
return [Events::loadClassMetadata]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs) |
39
|
|
|
{ |
40
|
|
|
$this->classMetadata = $eventArgs->getClassMetadata(); |
|
|
|
|
41
|
|
|
$reflectionClass = $this->classMetadata->getReflectionClass(); |
|
|
|
|
42
|
|
|
|
43
|
|
|
if (null === $reflectionClass) { |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if ($reflectionClass->hasMethod('setShops') && $reflectionClass->hasMethod('getShops')) { |
48
|
|
|
$this->mapShopAssociation(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($reflectionClass->hasMethod('setShop') && $reflectionClass->hasMethod('getShop')) { |
52
|
|
|
$this->mapShopField(); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function mapShopAssociation() |
57
|
|
|
{ |
58
|
|
|
if (!$this->classMetadata->hasAssociation('shops')) { |
59
|
|
|
$this->classMetadata->mapManyToMany([ |
60
|
|
|
'fieldName' => 'shops', |
61
|
|
|
'fetch' => ClassMetadataInfo::FETCH_LAZY, |
62
|
|
|
'joinColumns' => [ |
63
|
|
|
[ |
64
|
|
|
'name' => 'foreign_id', |
65
|
|
|
'referencedColumnName' => 'id', |
66
|
|
|
'onDelete' => 'CASCADE', |
67
|
|
|
'nullable' => false, |
68
|
|
|
], |
69
|
|
|
], |
70
|
|
|
'inverseJoinColumns' => [ |
71
|
|
|
[ |
72
|
|
|
'name' => 'shop_id', |
73
|
|
|
'referencedColumnName' => 'id', |
74
|
|
|
'onDelete' => 'CASCADE', |
75
|
|
|
'nullable' => false, |
76
|
|
|
], |
77
|
|
|
], |
78
|
|
|
'targetEntity' => Shop::class, |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function mapShopField() |
84
|
|
|
{ |
85
|
|
|
if (!$this->classMetadata->hasAssociation('shop')) { |
86
|
|
|
$this->classMetadata->mapManyToOne([ |
87
|
|
|
'fieldName' => 'shop', |
88
|
|
|
'fetch' => ClassMetadataInfo::FETCH_LAZY, |
89
|
|
|
'joinColumns' => [ |
90
|
|
|
[ |
91
|
|
|
'name' => 'shop_id', |
92
|
|
|
'referencedColumnName' => 'id', |
93
|
|
|
'onDelete' => 'CASCADE', |
94
|
|
|
], |
95
|
|
|
], |
96
|
|
|
'targetEntity' => Shop::class, |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..