1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/orm package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Orm\Mapper\Relation; |
11
|
|
|
|
12
|
|
|
use Slick\Common\Base; |
13
|
|
|
use Slick\Common\Utils\Text; |
14
|
|
|
use Slick\Database\Adapter\AdapterInterface; |
15
|
|
|
use Slick\Orm\Descriptor\EntityDescriptorInterface; |
16
|
|
|
use Slick\Orm\Descriptor\EntityDescriptorRegistry; |
17
|
|
|
use Slick\Orm\Entity\EntityCollection; |
18
|
|
|
use Slick\Orm\EntityInterface; |
19
|
|
|
use Slick\Orm\Orm; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* AbstractRelation |
23
|
|
|
* |
24
|
|
|
* @package Slick\Orm\Mapper\Relation |
25
|
|
|
* @author Filipe Silva <[email protected]> |
26
|
|
|
* |
27
|
|
|
* @property AdapterInterface $adapter |
28
|
|
|
* @property bool $lazyLoaded |
29
|
|
|
* |
30
|
|
|
* @method bool isLazyLoaded() |
31
|
|
|
*/ |
32
|
|
|
abstract class AbstractRelation extends Base |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @readwrite |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $propertyName; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @readwrite |
42
|
|
|
* @var EntityDescriptorInterface |
43
|
|
|
*/ |
44
|
|
|
protected $entityDescriptor; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Parent or related entity class name |
48
|
|
|
* @readwrite |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $parentEntity; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @readwrite |
55
|
|
|
* @var EntityDescriptorInterface |
56
|
|
|
*/ |
57
|
|
|
protected $parentEntityDescriptor; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @readwrite |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $foreignKey; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @readwrite |
67
|
|
|
* @var AdapterInterface |
68
|
|
|
*/ |
69
|
|
|
protected $adapter; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @readwrite |
73
|
|
|
* @var bool |
74
|
|
|
*/ |
75
|
|
|
protected $lazyLoaded = true; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the property holding the relation |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
2 |
|
public function getPropertyName() |
83
|
|
|
{ |
84
|
2 |
|
return $this->propertyName; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Gets the entity descriptor |
90
|
|
|
* |
91
|
|
|
* @return EntityDescriptorInterface |
92
|
|
|
*/ |
93
|
4 |
|
public function getEntityDescriptor() |
94
|
|
|
{ |
95
|
4 |
|
return $this->entityDescriptor; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Sets entity descriptor |
100
|
|
|
* |
101
|
|
|
* @param EntityDescriptorInterface $descriptor |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
26 |
|
public function setEntityDescriptor(EntityDescriptorInterface $descriptor) |
105
|
|
|
{ |
106
|
26 |
|
$this->entityDescriptor = $descriptor; |
107
|
26 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Gets parent entity class name |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
6 |
|
public function getParentEntity() |
116
|
|
|
{ |
117
|
6 |
|
return $this->parentEntity; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Gets the parent or related entity descriptor |
122
|
|
|
* |
123
|
|
|
* @return EntityDescriptorInterface |
124
|
|
|
*/ |
125
|
8 |
|
public function getParentEntityDescriptor() |
126
|
|
|
{ |
127
|
8 |
|
if (is_null($this->parentEntityDescriptor)) { |
128
|
8 |
|
$this->setParentEntityDescriptor( |
129
|
8 |
|
EntityDescriptorRegistry::getInstance() |
130
|
8 |
|
->getDescriptorFor($this->parentEntity) |
131
|
4 |
|
); |
132
|
4 |
|
} |
133
|
8 |
|
return $this->parentEntityDescriptor; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Sets parent entity descriptor |
138
|
|
|
* |
139
|
|
|
* @param EntityDescriptorInterface $parentEntityDescriptor |
140
|
|
|
* @return BelongsTo |
141
|
|
|
*/ |
142
|
8 |
|
public function setParentEntityDescriptor( |
143
|
|
|
EntityDescriptorInterface $parentEntityDescriptor |
144
|
|
|
) { |
145
|
8 |
|
$this->parentEntityDescriptor = $parentEntityDescriptor; |
146
|
8 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Gets the foreign key field name |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
8 |
|
public function getForeignKey() |
155
|
|
|
{ |
156
|
8 |
|
if (is_null($this->foreignKey)) { |
157
|
8 |
|
$name = $this->getParentEntityDescriptor()->getTableName(); |
158
|
8 |
|
$name = Text::singular(strtolower($name)); |
159
|
8 |
|
$this->foreignKey = "{$name}_id"; |
160
|
4 |
|
} |
161
|
8 |
|
return $this->foreignKey; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Register the retrieved entities in the repository identity map |
166
|
|
|
* |
167
|
|
|
* @param EntityInterface|EntityCollection $entity |
168
|
|
|
* |
169
|
|
|
* @return EntityInterface|EntityCollection |
170
|
|
|
*/ |
171
|
2 |
|
protected function registerEntity($entity) |
172
|
|
|
{ |
173
|
2 |
|
if ($entity instanceof EntityCollection) { |
174
|
|
|
foreach ($entity as $item) { |
175
|
|
|
$this->registerEntity($item); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
2 |
|
Orm::getRepository($this->getParentEntity()) |
180
|
2 |
|
->getIdentityMap() |
181
|
2 |
|
->set($entity); |
|
|
|
|
182
|
|
|
|
183
|
2 |
|
return $entity; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Set the database adapter for this relation |
188
|
|
|
* |
189
|
|
|
* @param AdapterInterface $adapter |
190
|
|
|
* @return $this|self|AbstractRelation |
191
|
|
|
*/ |
192
|
4 |
|
public function setAdapter(AdapterInterface $adapter) |
193
|
|
|
{ |
194
|
4 |
|
$this->adapter = $adapter; |
195
|
4 |
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Gets relation adapter |
200
|
|
|
* |
201
|
|
|
* @return AdapterInterface |
202
|
|
|
*/ |
203
|
4 |
|
public function getAdapter() |
204
|
|
|
{ |
205
|
4 |
|
if (null == $this->adapter) { |
206
|
2 |
|
$className = $this->getEntityDescriptor()->className(); |
207
|
2 |
|
$repository = Orm::getRepository($className); |
208
|
2 |
|
$this->setAdapter($repository->getAdapter()); |
209
|
1 |
|
} |
210
|
4 |
|
return $this->adapter; |
211
|
|
|
} |
212
|
|
|
} |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.