@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | protected $customCommands = []; |
71 | 71 | |
72 | 72 | /** |
73 | - * @param EntityMap $entityMapper |
|
73 | + * @param EntityMap $entityMap |
|
74 | 74 | * @param DBAdapter $adapter |
75 | 75 | * @param Dispatcher $dispatcher |
76 | 76 | * @param Manager $manager |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | * Delete an entity or an entity collection from the database |
166 | 166 | * |
167 | 167 | * @param mixed|Collection |
168 | - * @return void |
|
168 | + * @return Collection|null |
|
169 | 169 | */ |
170 | 170 | public function delete($entity) |
171 | 171 | { |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | * Register an entity event with the dispatcher. |
256 | 256 | * |
257 | 257 | * @param string $event |
258 | - * @param \Closure|string $callback |
|
258 | + * @param \Closure $callback |
|
259 | 259 | * @return void |
260 | 260 | */ |
261 | 261 | public function registerEvent($event, $callback) |
@@ -313,7 +313,7 @@ discard block |
||
313 | 313 | /** |
314 | 314 | * Apply all of the global scopes to an Analogue Query builder. |
315 | 315 | * |
316 | - * @param \Analogue\ORM\System\Query $builder |
|
316 | + * @param Query $query |
|
317 | 317 | * @return \Analogue\ORM\System\Query |
318 | 318 | */ |
319 | 319 | public function applyGlobalScopes($query) |
@@ -328,7 +328,7 @@ discard block |
||
328 | 328 | /** |
329 | 329 | * Remove all of the global scopes from an Analogue Query builder. |
330 | 330 | * |
331 | - * @param \Analogue\ORM\System\Query $builder |
|
331 | + * @param Query $query |
|
332 | 332 | * @return \Analogue\ORM\System\Query |
333 | 333 | */ |
334 | 334 | public function removeGlobalScopes($query) |
@@ -356,7 +356,7 @@ discard block |
||
356 | 356 | /** |
357 | 357 | * Get a new query builder that doesn't have any global scopes. |
358 | 358 | * |
359 | - * @return Analogue\ORM\System\Query|static |
|
359 | + * @return Query |
|
360 | 360 | */ |
361 | 361 | public function newQueryWithoutScopes() |
362 | 362 | { |
@@ -398,7 +398,7 @@ discard block |
||
398 | 398 | /** |
399 | 399 | * Execute a single command instance |
400 | 400 | * |
401 | - * @param string $command |
|
401 | + * @param string $commandClass |
|
402 | 402 | * @param mixed $entity |
403 | 403 | * @return mixed |
404 | 404 | */ |
@@ -476,6 +476,7 @@ discard block |
||
476 | 476 | * Use a trick to generate a class prototype that we |
477 | 477 | * can instantiate without calling the constructor. |
478 | 478 | * |
479 | + * @param string|null $className |
|
479 | 480 | * @return mixed |
480 | 481 | */ |
481 | 482 | protected function customClassInstance($className) |
@@ -7,7 +7,6 @@ |
||
7 | 7 | use Analogue\ORM\Commands\Delete; |
8 | 8 | use Illuminate\Contracts\Events\Dispatcher; |
9 | 9 | use Illuminate\Support\Collection; |
10 | -use Analogue\ORM\Commands\Command; |
|
11 | 10 | use Analogue\ORM\Drivers\DBAdapter; |
12 | 11 | use Analogue\ORM\Exceptions\MappingException; |
13 | 12 |
@@ -19,549 +19,549 @@ |
||
19 | 19 | class Mapper |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * The Manager instance |
|
24 | - * |
|
25 | - * @var \Analogue\ORM\System\Manager |
|
26 | - */ |
|
27 | - protected $manager; |
|
28 | - |
|
29 | - /** |
|
30 | - * Instance of EntityMapper Obect |
|
31 | - * |
|
32 | - * @var \Analogue\ORM\EntityMap |
|
33 | - */ |
|
34 | - protected $entityMap; |
|
35 | - |
|
36 | - /** |
|
37 | - * The instance of db adapter |
|
38 | - * |
|
39 | - * @var \Analogue\ORM\Drivers\DBAdapter |
|
40 | - */ |
|
41 | - protected $adapter; |
|
42 | - |
|
43 | - |
|
44 | - /** |
|
45 | - * Event dispatcher instance |
|
46 | - * |
|
47 | - * @var \Illuminate\Contracts\Events\Dispatcher |
|
48 | - */ |
|
49 | - protected $dispatcher; |
|
50 | - |
|
51 | - /** |
|
52 | - * Entity Cache |
|
53 | - * |
|
54 | - * @var \Analogue\ORM\System\EntityCache |
|
55 | - */ |
|
56 | - protected $cache; |
|
57 | - |
|
58 | - /** |
|
59 | - * Global scopes |
|
60 | - * |
|
61 | - * @var array |
|
62 | - */ |
|
63 | - protected $globalScopes = []; |
|
64 | - |
|
65 | - /** |
|
66 | - * Custom Commands |
|
67 | - * |
|
68 | - * @var array |
|
69 | - */ |
|
70 | - protected $customCommands = []; |
|
71 | - |
|
72 | - /** |
|
73 | - * @param EntityMap $entityMapper |
|
74 | - * @param DBAdapter $adapter |
|
75 | - * @param Dispatcher $dispatcher |
|
76 | - * @param Manager $manager |
|
77 | - */ |
|
78 | - public function __construct(EntityMap $entityMap, DBAdapter $adapter, Dispatcher $dispatcher, Manager $manager) |
|
79 | - { |
|
80 | - $this->entityMap = $entityMap; |
|
81 | - |
|
82 | - $this->adapter = $adapter; |
|
83 | - |
|
84 | - $this->dispatcher = $dispatcher; |
|
85 | - |
|
86 | - $this->manager = $manager; |
|
87 | - |
|
88 | - $this->cache = new EntityCache($entityMap); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Persist an entity or an entity collection into the database |
|
93 | - * |
|
94 | - * @param Mappable|Collection $entity |
|
95 | - * @return Mappable|Collection |
|
96 | - */ |
|
97 | - public function store($entity) |
|
98 | - { |
|
99 | - if ($this->isArrayOrCollection($entity)) { |
|
100 | - return $this->storeCollection($entity); |
|
101 | - } else { |
|
102 | - return $this->storeEntity($entity); |
|
103 | - } |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Return true if an object is an array or collection |
|
108 | - * |
|
109 | - * @param mixed $argument |
|
110 | - * @return boolean |
|
111 | - */ |
|
112 | - protected function isArrayOrCollection($argument) |
|
113 | - { |
|
114 | - return ($argument instanceof Collection || is_array($argument)) ? true : false; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Store a single entity into the database |
|
119 | - * |
|
120 | - * @param Mappable $entity |
|
121 | - * @return Entity |
|
122 | - */ |
|
123 | - protected function storeEntity($entity) |
|
124 | - { |
|
125 | - $this->checkEntityType($entity); |
|
22 | + /** |
|
23 | + * The Manager instance |
|
24 | + * |
|
25 | + * @var \Analogue\ORM\System\Manager |
|
26 | + */ |
|
27 | + protected $manager; |
|
28 | + |
|
29 | + /** |
|
30 | + * Instance of EntityMapper Obect |
|
31 | + * |
|
32 | + * @var \Analogue\ORM\EntityMap |
|
33 | + */ |
|
34 | + protected $entityMap; |
|
35 | + |
|
36 | + /** |
|
37 | + * The instance of db adapter |
|
38 | + * |
|
39 | + * @var \Analogue\ORM\Drivers\DBAdapter |
|
40 | + */ |
|
41 | + protected $adapter; |
|
42 | + |
|
43 | + |
|
44 | + /** |
|
45 | + * Event dispatcher instance |
|
46 | + * |
|
47 | + * @var \Illuminate\Contracts\Events\Dispatcher |
|
48 | + */ |
|
49 | + protected $dispatcher; |
|
50 | + |
|
51 | + /** |
|
52 | + * Entity Cache |
|
53 | + * |
|
54 | + * @var \Analogue\ORM\System\EntityCache |
|
55 | + */ |
|
56 | + protected $cache; |
|
57 | + |
|
58 | + /** |
|
59 | + * Global scopes |
|
60 | + * |
|
61 | + * @var array |
|
62 | + */ |
|
63 | + protected $globalScopes = []; |
|
64 | + |
|
65 | + /** |
|
66 | + * Custom Commands |
|
67 | + * |
|
68 | + * @var array |
|
69 | + */ |
|
70 | + protected $customCommands = []; |
|
71 | + |
|
72 | + /** |
|
73 | + * @param EntityMap $entityMapper |
|
74 | + * @param DBAdapter $adapter |
|
75 | + * @param Dispatcher $dispatcher |
|
76 | + * @param Manager $manager |
|
77 | + */ |
|
78 | + public function __construct(EntityMap $entityMap, DBAdapter $adapter, Dispatcher $dispatcher, Manager $manager) |
|
79 | + { |
|
80 | + $this->entityMap = $entityMap; |
|
81 | + |
|
82 | + $this->adapter = $adapter; |
|
83 | + |
|
84 | + $this->dispatcher = $dispatcher; |
|
85 | + |
|
86 | + $this->manager = $manager; |
|
87 | + |
|
88 | + $this->cache = new EntityCache($entityMap); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Persist an entity or an entity collection into the database |
|
93 | + * |
|
94 | + * @param Mappable|Collection $entity |
|
95 | + * @return Mappable|Collection |
|
96 | + */ |
|
97 | + public function store($entity) |
|
98 | + { |
|
99 | + if ($this->isArrayOrCollection($entity)) { |
|
100 | + return $this->storeCollection($entity); |
|
101 | + } else { |
|
102 | + return $this->storeEntity($entity); |
|
103 | + } |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Return true if an object is an array or collection |
|
108 | + * |
|
109 | + * @param mixed $argument |
|
110 | + * @return boolean |
|
111 | + */ |
|
112 | + protected function isArrayOrCollection($argument) |
|
113 | + { |
|
114 | + return ($argument instanceof Collection || is_array($argument)) ? true : false; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Store a single entity into the database |
|
119 | + * |
|
120 | + * @param Mappable $entity |
|
121 | + * @return Entity |
|
122 | + */ |
|
123 | + protected function storeEntity($entity) |
|
124 | + { |
|
125 | + $this->checkEntityType($entity); |
|
126 | 126 | |
127 | - $store = new Store($this->aggregate($entity), $this->newQueryBuilder()); |
|
128 | - |
|
129 | - return $store->execute(); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Convert an entity into an aggregate root |
|
134 | - * |
|
135 | - * @param mixed $entity |
|
136 | - * @return \Analogue\ORM\System\Aggregate |
|
137 | - */ |
|
138 | - protected function aggregate($entity) |
|
139 | - { |
|
140 | - $aggregate = new Aggregate($entity); |
|
141 | - |
|
142 | - return $aggregate; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Store an entity collection inside a single DB Transaction |
|
147 | - * |
|
148 | - * @param Collection|array $entities [description] |
|
149 | - * @return Collection |
|
150 | - */ |
|
151 | - protected function storeCollection($entities) |
|
152 | - { |
|
153 | - $this->adapter->beginTransaction(); |
|
154 | - |
|
155 | - foreach ($entities as $entity) { |
|
156 | - $this->storeEntity($entity); |
|
157 | - } |
|
158 | - |
|
159 | - $this->adapter->commit(); |
|
160 | - |
|
161 | - return $entities; |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Delete an entity or an entity collection from the database |
|
166 | - * |
|
167 | - * @param mixed|Collection |
|
168 | - * @return void |
|
169 | - */ |
|
170 | - public function delete($entity) |
|
171 | - { |
|
172 | - if ($this->isArrayOrCollection($entity)) { |
|
173 | - return $this->deleteCollection($entity); |
|
174 | - } else { |
|
175 | - return $this->deleteEntity($entity); |
|
176 | - } |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Delete a single entity from the database. |
|
181 | - * |
|
182 | - * @param Mappable $entity |
|
183 | - * @return void |
|
184 | - */ |
|
185 | - protected function deleteEntity($entity) |
|
186 | - { |
|
187 | - $this->checkEntityType($entity); |
|
188 | - |
|
189 | - $delete = new Delete($this->aggregate($entity), $this->newQueryBuilder()); |
|
190 | - |
|
191 | - $delete->execute(); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Delete an Entity Collection inside a single db transaction |
|
196 | - * |
|
197 | - * @param Collection|array $entities |
|
198 | - * @return Collection |
|
199 | - */ |
|
200 | - protected function deleteCollection($entities) |
|
201 | - { |
|
202 | - $this->adapter->beginTransaction(); |
|
203 | - |
|
204 | - foreach ($entities as $entity) { |
|
205 | - $this->deleteEntity($entity); |
|
206 | - } |
|
207 | - |
|
208 | - $this->adapter->commit(); |
|
127 | + $store = new Store($this->aggregate($entity), $this->newQueryBuilder()); |
|
128 | + |
|
129 | + return $store->execute(); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Convert an entity into an aggregate root |
|
134 | + * |
|
135 | + * @param mixed $entity |
|
136 | + * @return \Analogue\ORM\System\Aggregate |
|
137 | + */ |
|
138 | + protected function aggregate($entity) |
|
139 | + { |
|
140 | + $aggregate = new Aggregate($entity); |
|
141 | + |
|
142 | + return $aggregate; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Store an entity collection inside a single DB Transaction |
|
147 | + * |
|
148 | + * @param Collection|array $entities [description] |
|
149 | + * @return Collection |
|
150 | + */ |
|
151 | + protected function storeCollection($entities) |
|
152 | + { |
|
153 | + $this->adapter->beginTransaction(); |
|
154 | + |
|
155 | + foreach ($entities as $entity) { |
|
156 | + $this->storeEntity($entity); |
|
157 | + } |
|
158 | + |
|
159 | + $this->adapter->commit(); |
|
160 | + |
|
161 | + return $entities; |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Delete an entity or an entity collection from the database |
|
166 | + * |
|
167 | + * @param mixed|Collection |
|
168 | + * @return void |
|
169 | + */ |
|
170 | + public function delete($entity) |
|
171 | + { |
|
172 | + if ($this->isArrayOrCollection($entity)) { |
|
173 | + return $this->deleteCollection($entity); |
|
174 | + } else { |
|
175 | + return $this->deleteEntity($entity); |
|
176 | + } |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Delete a single entity from the database. |
|
181 | + * |
|
182 | + * @param Mappable $entity |
|
183 | + * @return void |
|
184 | + */ |
|
185 | + protected function deleteEntity($entity) |
|
186 | + { |
|
187 | + $this->checkEntityType($entity); |
|
188 | + |
|
189 | + $delete = new Delete($this->aggregate($entity), $this->newQueryBuilder()); |
|
190 | + |
|
191 | + $delete->execute(); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Delete an Entity Collection inside a single db transaction |
|
196 | + * |
|
197 | + * @param Collection|array $entities |
|
198 | + * @return Collection |
|
199 | + */ |
|
200 | + protected function deleteCollection($entities) |
|
201 | + { |
|
202 | + $this->adapter->beginTransaction(); |
|
203 | + |
|
204 | + foreach ($entities as $entity) { |
|
205 | + $this->deleteEntity($entity); |
|
206 | + } |
|
207 | + |
|
208 | + $this->adapter->commit(); |
|
209 | 209 | |
210 | - return $entities; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Return the entity map for this mapper |
|
215 | - * |
|
216 | - * @return EntityMap |
|
217 | - */ |
|
218 | - public function getEntityMap() |
|
219 | - { |
|
220 | - return $this->entityMap; |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * Get the entity cache for the current mapper |
|
225 | - * |
|
226 | - * @return EntityCache $entityCache |
|
227 | - */ |
|
228 | - public function getEntityCache() |
|
229 | - { |
|
230 | - return $this->cache; |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Fire the given event for the entity |
|
235 | - * |
|
236 | - * @param string $event |
|
237 | - * @param \Analogue\ORM\Entity $entity |
|
238 | - * @param bool $halt |
|
239 | - * @return mixed |
|
240 | - */ |
|
241 | - public function fireEvent($event, $entity, $halt = true) |
|
242 | - { |
|
243 | - if ($entity instanceof Wrapper) { |
|
244 | - throw new InvalidArgumentException("Fired Event with invalid Entity Object"); |
|
245 | - } |
|
246 | - |
|
247 | - $event = "analogue.{$event}.".$this->entityMap->getClass(); |
|
248 | - |
|
249 | - $method = $halt ? 'until' : 'fire'; |
|
250 | - |
|
251 | - return $this->dispatcher->$method($event, $entity); |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * Register an entity event with the dispatcher. |
|
256 | - * |
|
257 | - * @param string $event |
|
258 | - * @param \Closure|string $callback |
|
259 | - * @return void |
|
260 | - */ |
|
261 | - public function registerEvent($event, $callback) |
|
262 | - { |
|
263 | - $name = $this->entityMap->getClass(); |
|
264 | - |
|
265 | - $this->dispatcher->listen("analogue.{$event}.{$name}", $callback); |
|
266 | - } |
|
267 | - |
|
268 | - /** |
|
269 | - * Add a global scope to this mapper query builder |
|
270 | - * |
|
271 | - * @param ScopeInterface $scope |
|
272 | - * @return void |
|
273 | - */ |
|
274 | - public function addGlobalScope(ScopeInterface $scope) |
|
275 | - { |
|
276 | - $this->globalScopes[get_class($scope)] = $scope; |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Determine if the mapper has a global scope. |
|
281 | - * |
|
282 | - * @param \Analogue\ORM\System\ScopeInterface $scope |
|
283 | - * @return bool |
|
284 | - */ |
|
285 | - public function hasGlobalScope($scope) |
|
286 | - { |
|
287 | - return ! is_null($this->getGlobalScope($scope)); |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Get a global scope registered with the modal. |
|
292 | - * |
|
293 | - * @param \Analogue\ORM\System\ScopeInterface $scope |
|
294 | - * @return \Analogue\ORM\System\ScopeInterface |null |
|
295 | - */ |
|
296 | - public function getGlobalScope($scope) |
|
297 | - { |
|
298 | - return array_first($this->globalScopes, function ($key, $value) use ($scope) { |
|
299 | - return $scope instanceof $value; |
|
300 | - }); |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Get the global scopes for this class instance. |
|
305 | - * |
|
306 | - * @return \Analogue\ORM\System\ScopeInterface |
|
307 | - */ |
|
308 | - public function getGlobalScopes() |
|
309 | - { |
|
310 | - return $this->globalScopes; |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * Apply all of the global scopes to an Analogue Query builder. |
|
315 | - * |
|
316 | - * @param \Analogue\ORM\System\Query $builder |
|
317 | - * @return \Analogue\ORM\System\Query |
|
318 | - */ |
|
319 | - public function applyGlobalScopes($query) |
|
320 | - { |
|
321 | - foreach ($this->getGlobalScopes() as $scope) { |
|
322 | - $scope->apply($query, $this); |
|
323 | - } |
|
324 | - |
|
325 | - return $query; |
|
326 | - } |
|
327 | - |
|
328 | - /** |
|
329 | - * Remove all of the global scopes from an Analogue Query builder. |
|
330 | - * |
|
331 | - * @param \Analogue\ORM\System\Query $builder |
|
332 | - * @return \Analogue\ORM\System\Query |
|
333 | - */ |
|
334 | - public function removeGlobalScopes($query) |
|
335 | - { |
|
336 | - foreach ($this->getGlobalScopes() as $scope) { |
|
337 | - $scope->remove($query, $this); |
|
338 | - } |
|
339 | - |
|
340 | - return $query; |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * Get a new query instance without a given scope. |
|
345 | - * |
|
346 | - * @param \Analogue\ORM\System\ScopeInterface $scope |
|
347 | - * @return \Analogue\ORM\System\Query |
|
348 | - */ |
|
349 | - public function newQueryWithoutScope($scope) |
|
350 | - { |
|
351 | - $this->getGlobalScope($scope)->remove($query = $this->getQuery(), $this); |
|
352 | - |
|
353 | - return $query; |
|
354 | - } |
|
355 | - |
|
356 | - /** |
|
357 | - * Get a new query builder that doesn't have any global scopes. |
|
358 | - * |
|
359 | - * @return Analogue\ORM\System\Query|static |
|
360 | - */ |
|
361 | - public function newQueryWithoutScopes() |
|
362 | - { |
|
363 | - return $this->removeGlobalScopes($this->getQuery()); |
|
364 | - } |
|
365 | - |
|
366 | - /** |
|
367 | - * Add a dynamic method that extends the mapper/repository |
|
368 | - * |
|
369 | - * @param string $command |
|
370 | - */ |
|
371 | - public function addCustomCommand($command) |
|
372 | - { |
|
373 | - $name = lcfirst(class_basename($command)); |
|
374 | - |
|
375 | - $this->customCommands[$name] = $command; |
|
376 | - } |
|
377 | - |
|
378 | - /** |
|
379 | - * Execute a custom command on an Entity |
|
380 | - * |
|
381 | - * @param string $command |
|
382 | - * @param mixed|Collection|array $entity |
|
383 | - * @return void |
|
384 | - */ |
|
385 | - public function executeCustomCommand($command, $entity) |
|
386 | - { |
|
387 | - $commandClass = $this->customCommands[$command]; |
|
388 | - |
|
389 | - if ($this->isArrayOrCollection($entity)) { |
|
390 | - foreach ($entity as $instance) { |
|
391 | - $this->executeSingleCustomCommand($commandClass, $instance); |
|
392 | - } |
|
393 | - } else { |
|
394 | - return $this->executeSingleCustomCommand($commandClass, $entity); |
|
395 | - } |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * Execute a single command instance |
|
400 | - * |
|
401 | - * @param string $command |
|
402 | - * @param mixed $entity |
|
403 | - * @return mixed |
|
404 | - */ |
|
405 | - protected function executeSingleCustomCommand($commandClass, $entity) |
|
406 | - { |
|
407 | - $this->checkEntityType($entity); |
|
408 | - |
|
409 | - $instance = new $commandClass($this->aggregate($entity), $this->newQueryBuilder()); |
|
410 | - |
|
411 | - return $instance->execute(); |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * Check that the entity correspond to the current mapper. |
|
416 | - * |
|
417 | - * @param mixed $entity |
|
418 | - * @return void |
|
419 | - * |
|
420 | - * @throws InvalidArgumentException |
|
421 | - */ |
|
422 | - protected function checkEntityType($entity) |
|
423 | - { |
|
424 | - if (get_class($entity) != $this->entityMap->getClass()) { |
|
425 | - $expected = $this->entityMap->getClass(); |
|
426 | - $actual = get_class($entity); |
|
427 | - throw new InvalidArgumentException("Expected : $expected, got $actual."); |
|
428 | - } |
|
429 | - } |
|
430 | - |
|
431 | - /** |
|
432 | - * Get all the custom commands regitered on this mapper |
|
433 | - * |
|
434 | - * @return array |
|
435 | - */ |
|
436 | - public function getCustomCommands() |
|
437 | - { |
|
438 | - return array_keys($this->customCommands); |
|
439 | - } |
|
440 | - |
|
441 | - /** |
|
442 | - * Check if this mapper supports this command |
|
443 | - * @param string $command |
|
444 | - * @return boolean |
|
445 | - */ |
|
446 | - public function hasCustomCommand($command) |
|
447 | - { |
|
448 | - return in_array($command, $this->getCustomCommands()); |
|
449 | - } |
|
450 | - |
|
451 | - /** |
|
452 | - * Create a new instance of the mapped entity class |
|
453 | - * |
|
454 | - * @param array $attributes |
|
455 | - * @return mixed |
|
456 | - */ |
|
457 | - public function newInstance($attributes = array()) |
|
458 | - { |
|
459 | - $class = $this->entityMap->getClass(); |
|
460 | - |
|
461 | - if ($this->entityMap->activator() != null) { |
|
462 | - $entity = $this->entityMap->activator(); |
|
463 | - } else { |
|
464 | - $entity = $this->customClassInstance($class); |
|
465 | - } |
|
466 | - |
|
467 | - // prevent hydrating with an empty array |
|
468 | - if (count($attributes) > 0) { |
|
469 | - $entity->setEntityAttributes($attributes); |
|
470 | - } |
|
471 | - |
|
472 | - return $entity; |
|
473 | - } |
|
474 | - |
|
475 | - /** |
|
476 | - * Use a trick to generate a class prototype that we |
|
477 | - * can instantiate without calling the constructor. |
|
478 | - * |
|
479 | - * @return mixed |
|
480 | - */ |
|
481 | - protected function customClassInstance($className) |
|
482 | - { |
|
483 | - if (! class_exists($className)) { |
|
484 | - throw new MappingException("Tried to instantiate a non-existing Entity class : $className"); |
|
485 | - } |
|
486 | - |
|
487 | - $prototype = unserialize(sprintf('O:%d:"%s":0:{}', |
|
488 | - strlen($className), |
|
489 | - $className |
|
490 | - ) |
|
491 | - ); |
|
492 | - return $prototype; |
|
493 | - } |
|
210 | + return $entities; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Return the entity map for this mapper |
|
215 | + * |
|
216 | + * @return EntityMap |
|
217 | + */ |
|
218 | + public function getEntityMap() |
|
219 | + { |
|
220 | + return $this->entityMap; |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * Get the entity cache for the current mapper |
|
225 | + * |
|
226 | + * @return EntityCache $entityCache |
|
227 | + */ |
|
228 | + public function getEntityCache() |
|
229 | + { |
|
230 | + return $this->cache; |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Fire the given event for the entity |
|
235 | + * |
|
236 | + * @param string $event |
|
237 | + * @param \Analogue\ORM\Entity $entity |
|
238 | + * @param bool $halt |
|
239 | + * @return mixed |
|
240 | + */ |
|
241 | + public function fireEvent($event, $entity, $halt = true) |
|
242 | + { |
|
243 | + if ($entity instanceof Wrapper) { |
|
244 | + throw new InvalidArgumentException("Fired Event with invalid Entity Object"); |
|
245 | + } |
|
246 | + |
|
247 | + $event = "analogue.{$event}.".$this->entityMap->getClass(); |
|
248 | + |
|
249 | + $method = $halt ? 'until' : 'fire'; |
|
250 | + |
|
251 | + return $this->dispatcher->$method($event, $entity); |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * Register an entity event with the dispatcher. |
|
256 | + * |
|
257 | + * @param string $event |
|
258 | + * @param \Closure|string $callback |
|
259 | + * @return void |
|
260 | + */ |
|
261 | + public function registerEvent($event, $callback) |
|
262 | + { |
|
263 | + $name = $this->entityMap->getClass(); |
|
264 | + |
|
265 | + $this->dispatcher->listen("analogue.{$event}.{$name}", $callback); |
|
266 | + } |
|
267 | + |
|
268 | + /** |
|
269 | + * Add a global scope to this mapper query builder |
|
270 | + * |
|
271 | + * @param ScopeInterface $scope |
|
272 | + * @return void |
|
273 | + */ |
|
274 | + public function addGlobalScope(ScopeInterface $scope) |
|
275 | + { |
|
276 | + $this->globalScopes[get_class($scope)] = $scope; |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Determine if the mapper has a global scope. |
|
281 | + * |
|
282 | + * @param \Analogue\ORM\System\ScopeInterface $scope |
|
283 | + * @return bool |
|
284 | + */ |
|
285 | + public function hasGlobalScope($scope) |
|
286 | + { |
|
287 | + return ! is_null($this->getGlobalScope($scope)); |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Get a global scope registered with the modal. |
|
292 | + * |
|
293 | + * @param \Analogue\ORM\System\ScopeInterface $scope |
|
294 | + * @return \Analogue\ORM\System\ScopeInterface |null |
|
295 | + */ |
|
296 | + public function getGlobalScope($scope) |
|
297 | + { |
|
298 | + return array_first($this->globalScopes, function ($key, $value) use ($scope) { |
|
299 | + return $scope instanceof $value; |
|
300 | + }); |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Get the global scopes for this class instance. |
|
305 | + * |
|
306 | + * @return \Analogue\ORM\System\ScopeInterface |
|
307 | + */ |
|
308 | + public function getGlobalScopes() |
|
309 | + { |
|
310 | + return $this->globalScopes; |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * Apply all of the global scopes to an Analogue Query builder. |
|
315 | + * |
|
316 | + * @param \Analogue\ORM\System\Query $builder |
|
317 | + * @return \Analogue\ORM\System\Query |
|
318 | + */ |
|
319 | + public function applyGlobalScopes($query) |
|
320 | + { |
|
321 | + foreach ($this->getGlobalScopes() as $scope) { |
|
322 | + $scope->apply($query, $this); |
|
323 | + } |
|
324 | + |
|
325 | + return $query; |
|
326 | + } |
|
327 | + |
|
328 | + /** |
|
329 | + * Remove all of the global scopes from an Analogue Query builder. |
|
330 | + * |
|
331 | + * @param \Analogue\ORM\System\Query $builder |
|
332 | + * @return \Analogue\ORM\System\Query |
|
333 | + */ |
|
334 | + public function removeGlobalScopes($query) |
|
335 | + { |
|
336 | + foreach ($this->getGlobalScopes() as $scope) { |
|
337 | + $scope->remove($query, $this); |
|
338 | + } |
|
339 | + |
|
340 | + return $query; |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * Get a new query instance without a given scope. |
|
345 | + * |
|
346 | + * @param \Analogue\ORM\System\ScopeInterface $scope |
|
347 | + * @return \Analogue\ORM\System\Query |
|
348 | + */ |
|
349 | + public function newQueryWithoutScope($scope) |
|
350 | + { |
|
351 | + $this->getGlobalScope($scope)->remove($query = $this->getQuery(), $this); |
|
352 | + |
|
353 | + return $query; |
|
354 | + } |
|
355 | + |
|
356 | + /** |
|
357 | + * Get a new query builder that doesn't have any global scopes. |
|
358 | + * |
|
359 | + * @return Analogue\ORM\System\Query|static |
|
360 | + */ |
|
361 | + public function newQueryWithoutScopes() |
|
362 | + { |
|
363 | + return $this->removeGlobalScopes($this->getQuery()); |
|
364 | + } |
|
365 | + |
|
366 | + /** |
|
367 | + * Add a dynamic method that extends the mapper/repository |
|
368 | + * |
|
369 | + * @param string $command |
|
370 | + */ |
|
371 | + public function addCustomCommand($command) |
|
372 | + { |
|
373 | + $name = lcfirst(class_basename($command)); |
|
374 | + |
|
375 | + $this->customCommands[$name] = $command; |
|
376 | + } |
|
377 | + |
|
378 | + /** |
|
379 | + * Execute a custom command on an Entity |
|
380 | + * |
|
381 | + * @param string $command |
|
382 | + * @param mixed|Collection|array $entity |
|
383 | + * @return void |
|
384 | + */ |
|
385 | + public function executeCustomCommand($command, $entity) |
|
386 | + { |
|
387 | + $commandClass = $this->customCommands[$command]; |
|
388 | + |
|
389 | + if ($this->isArrayOrCollection($entity)) { |
|
390 | + foreach ($entity as $instance) { |
|
391 | + $this->executeSingleCustomCommand($commandClass, $instance); |
|
392 | + } |
|
393 | + } else { |
|
394 | + return $this->executeSingleCustomCommand($commandClass, $entity); |
|
395 | + } |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * Execute a single command instance |
|
400 | + * |
|
401 | + * @param string $command |
|
402 | + * @param mixed $entity |
|
403 | + * @return mixed |
|
404 | + */ |
|
405 | + protected function executeSingleCustomCommand($commandClass, $entity) |
|
406 | + { |
|
407 | + $this->checkEntityType($entity); |
|
408 | + |
|
409 | + $instance = new $commandClass($this->aggregate($entity), $this->newQueryBuilder()); |
|
410 | + |
|
411 | + return $instance->execute(); |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * Check that the entity correspond to the current mapper. |
|
416 | + * |
|
417 | + * @param mixed $entity |
|
418 | + * @return void |
|
419 | + * |
|
420 | + * @throws InvalidArgumentException |
|
421 | + */ |
|
422 | + protected function checkEntityType($entity) |
|
423 | + { |
|
424 | + if (get_class($entity) != $this->entityMap->getClass()) { |
|
425 | + $expected = $this->entityMap->getClass(); |
|
426 | + $actual = get_class($entity); |
|
427 | + throw new InvalidArgumentException("Expected : $expected, got $actual."); |
|
428 | + } |
|
429 | + } |
|
430 | + |
|
431 | + /** |
|
432 | + * Get all the custom commands regitered on this mapper |
|
433 | + * |
|
434 | + * @return array |
|
435 | + */ |
|
436 | + public function getCustomCommands() |
|
437 | + { |
|
438 | + return array_keys($this->customCommands); |
|
439 | + } |
|
440 | + |
|
441 | + /** |
|
442 | + * Check if this mapper supports this command |
|
443 | + * @param string $command |
|
444 | + * @return boolean |
|
445 | + */ |
|
446 | + public function hasCustomCommand($command) |
|
447 | + { |
|
448 | + return in_array($command, $this->getCustomCommands()); |
|
449 | + } |
|
450 | + |
|
451 | + /** |
|
452 | + * Create a new instance of the mapped entity class |
|
453 | + * |
|
454 | + * @param array $attributes |
|
455 | + * @return mixed |
|
456 | + */ |
|
457 | + public function newInstance($attributes = array()) |
|
458 | + { |
|
459 | + $class = $this->entityMap->getClass(); |
|
460 | + |
|
461 | + if ($this->entityMap->activator() != null) { |
|
462 | + $entity = $this->entityMap->activator(); |
|
463 | + } else { |
|
464 | + $entity = $this->customClassInstance($class); |
|
465 | + } |
|
466 | + |
|
467 | + // prevent hydrating with an empty array |
|
468 | + if (count($attributes) > 0) { |
|
469 | + $entity->setEntityAttributes($attributes); |
|
470 | + } |
|
471 | + |
|
472 | + return $entity; |
|
473 | + } |
|
474 | + |
|
475 | + /** |
|
476 | + * Use a trick to generate a class prototype that we |
|
477 | + * can instantiate without calling the constructor. |
|
478 | + * |
|
479 | + * @return mixed |
|
480 | + */ |
|
481 | + protected function customClassInstance($className) |
|
482 | + { |
|
483 | + if (! class_exists($className)) { |
|
484 | + throw new MappingException("Tried to instantiate a non-existing Entity class : $className"); |
|
485 | + } |
|
486 | + |
|
487 | + $prototype = unserialize(sprintf('O:%d:"%s":0:{}', |
|
488 | + strlen($className), |
|
489 | + $className |
|
490 | + ) |
|
491 | + ); |
|
492 | + return $prototype; |
|
493 | + } |
|
494 | 494 | |
495 | - /** |
|
496 | - * Get the Analogue Query Builer for this instance |
|
497 | - * |
|
498 | - * @return \Analogue\ORM\System\Query |
|
499 | - */ |
|
500 | - public function getQuery() |
|
501 | - { |
|
502 | - $query = new Query($this, $this->adapter); |
|
503 | - |
|
504 | - return $this->applyGlobalScopes($query); |
|
505 | - } |
|
495 | + /** |
|
496 | + * Get the Analogue Query Builer for this instance |
|
497 | + * |
|
498 | + * @return \Analogue\ORM\System\Query |
|
499 | + */ |
|
500 | + public function getQuery() |
|
501 | + { |
|
502 | + $query = new Query($this, $this->adapter); |
|
503 | + |
|
504 | + return $this->applyGlobalScopes($query); |
|
505 | + } |
|
506 | 506 | |
507 | - /** |
|
508 | - * Get the Analogue Query Builer for this instance |
|
509 | - * |
|
510 | - * @return \Analogue\ORM\System\Query |
|
511 | - */ |
|
512 | - public function query() |
|
513 | - { |
|
514 | - return $this->getQuery(); |
|
515 | - } |
|
516 | - |
|
517 | - /** |
|
518 | - * Get an unscoped Analogue Query Builer for this instance |
|
519 | - * |
|
520 | - * @return \Analogue\ORM\System\Query |
|
521 | - */ |
|
522 | - public function globalQuery() |
|
523 | - { |
|
524 | - return $this->newQueryWithoutScopes(); |
|
525 | - } |
|
526 | - |
|
527 | - /** |
|
528 | - * Get a the Underlying QueryAdapter. |
|
529 | - * |
|
530 | - * @return \Analogue\ORM\Drivers\QueryAdapter |
|
531 | - */ |
|
532 | - public function newQueryBuilder() |
|
533 | - { |
|
534 | - return $this->adapter->getQuery(); |
|
535 | - } |
|
536 | - |
|
537 | - /** |
|
538 | - * Return the manager instance |
|
539 | - * |
|
540 | - * @return \Analogue\ORM\System\Manager |
|
541 | - */ |
|
542 | - public function getManager() |
|
543 | - { |
|
544 | - return $this->manager; |
|
545 | - } |
|
546 | - |
|
547 | - /** |
|
548 | - * Dynamically handle calls to custom commands, or Redirects to query() |
|
549 | - * |
|
550 | - * @param string $method |
|
551 | - * @param array $parameters |
|
552 | - * @return mixed |
|
553 | - */ |
|
554 | - public function __call($method, $parameters) |
|
555 | - { |
|
556 | - // Check if method is a custom command on the mapper |
|
557 | - if ($this->hasCustomCommand($method)) { |
|
558 | - if (count($parameters) == 0) { |
|
559 | - throw new \Exception("$method must at least have 1 argument"); |
|
560 | - } |
|
561 | - return $this->executeCustomCommand($method, $parameters[0]); |
|
562 | - } |
|
563 | - |
|
564 | - // Redirect call on a new query instance |
|
565 | - return call_user_func_array(array($this->query(), $method), $parameters); |
|
566 | - } |
|
507 | + /** |
|
508 | + * Get the Analogue Query Builer for this instance |
|
509 | + * |
|
510 | + * @return \Analogue\ORM\System\Query |
|
511 | + */ |
|
512 | + public function query() |
|
513 | + { |
|
514 | + return $this->getQuery(); |
|
515 | + } |
|
516 | + |
|
517 | + /** |
|
518 | + * Get an unscoped Analogue Query Builer for this instance |
|
519 | + * |
|
520 | + * @return \Analogue\ORM\System\Query |
|
521 | + */ |
|
522 | + public function globalQuery() |
|
523 | + { |
|
524 | + return $this->newQueryWithoutScopes(); |
|
525 | + } |
|
526 | + |
|
527 | + /** |
|
528 | + * Get a the Underlying QueryAdapter. |
|
529 | + * |
|
530 | + * @return \Analogue\ORM\Drivers\QueryAdapter |
|
531 | + */ |
|
532 | + public function newQueryBuilder() |
|
533 | + { |
|
534 | + return $this->adapter->getQuery(); |
|
535 | + } |
|
536 | + |
|
537 | + /** |
|
538 | + * Return the manager instance |
|
539 | + * |
|
540 | + * @return \Analogue\ORM\System\Manager |
|
541 | + */ |
|
542 | + public function getManager() |
|
543 | + { |
|
544 | + return $this->manager; |
|
545 | + } |
|
546 | + |
|
547 | + /** |
|
548 | + * Dynamically handle calls to custom commands, or Redirects to query() |
|
549 | + * |
|
550 | + * @param string $method |
|
551 | + * @param array $parameters |
|
552 | + * @return mixed |
|
553 | + */ |
|
554 | + public function __call($method, $parameters) |
|
555 | + { |
|
556 | + // Check if method is a custom command on the mapper |
|
557 | + if ($this->hasCustomCommand($method)) { |
|
558 | + if (count($parameters) == 0) { |
|
559 | + throw new \Exception("$method must at least have 1 argument"); |
|
560 | + } |
|
561 | + return $this->executeCustomCommand($method, $parameters[0]); |
|
562 | + } |
|
563 | + |
|
564 | + // Redirect call on a new query instance |
|
565 | + return call_user_func_array(array($this->query(), $method), $parameters); |
|
566 | + } |
|
567 | 567 | } |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | */ |
285 | 285 | public function hasGlobalScope($scope) |
286 | 286 | { |
287 | - return ! is_null($this->getGlobalScope($scope)); |
|
287 | + return !is_null($this->getGlobalScope($scope)); |
|
288 | 288 | } |
289 | 289 | |
290 | 290 | /** |
@@ -295,7 +295,7 @@ discard block |
||
295 | 295 | */ |
296 | 296 | public function getGlobalScope($scope) |
297 | 297 | { |
298 | - return array_first($this->globalScopes, function ($key, $value) use ($scope) { |
|
298 | + return array_first($this->globalScopes, function($key, $value) use ($scope) { |
|
299 | 299 | return $scope instanceof $value; |
300 | 300 | }); |
301 | 301 | } |
@@ -480,7 +480,7 @@ discard block |
||
480 | 480 | */ |
481 | 481 | protected function customClassInstance($className) |
482 | 482 | { |
483 | - if (! class_exists($className)) { |
|
483 | + if (!class_exists($className)) { |
|
484 | 484 | throw new MappingException("Tried to instantiate a non-existing Entity class : $className"); |
485 | 485 | } |
486 | 486 |
@@ -46,7 +46,7 @@ |
||
46 | 46 | * Return a new Mapper instance |
47 | 47 | * |
48 | 48 | * @param string $entityClass |
49 | - * @param string $entityMap |
|
49 | + * @param EntityMap $entityMap |
|
50 | 50 | * @return Mapper |
51 | 51 | */ |
52 | 52 | public function make($entityClass, EntityMap $entityMap) |
@@ -12,64 +12,64 @@ |
||
12 | 12 | class MapperFactory |
13 | 13 | { |
14 | 14 | |
15 | - /** |
|
16 | - * Manager instance |
|
17 | - * |
|
18 | - * @var \Analogue\ORM\System\Manager |
|
19 | - */ |
|
20 | - protected $manager; |
|
15 | + /** |
|
16 | + * Manager instance |
|
17 | + * |
|
18 | + * @var \Analogue\ORM\System\Manager |
|
19 | + */ |
|
20 | + protected $manager; |
|
21 | 21 | |
22 | - /** |
|
23 | - * DriverManager instance |
|
24 | - * |
|
25 | - * @var \Analogue\ORM\Drivers\Manager |
|
26 | - */ |
|
27 | - protected $drivers; |
|
22 | + /** |
|
23 | + * DriverManager instance |
|
24 | + * |
|
25 | + * @var \Analogue\ORM\Drivers\Manager |
|
26 | + */ |
|
27 | + protected $drivers; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Event dispatcher instance |
|
31 | - * |
|
32 | - * @var \Illuminate\Contracts\Events\Dispatcher |
|
33 | - */ |
|
34 | - protected $dispatcher; |
|
29 | + /** |
|
30 | + * Event dispatcher instance |
|
31 | + * |
|
32 | + * @var \Illuminate\Contracts\Events\Dispatcher |
|
33 | + */ |
|
34 | + protected $dispatcher; |
|
35 | 35 | |
36 | - public function __construct(DriverManager $drivers, Dispatcher $dispatcher, Manager $manager) |
|
37 | - { |
|
38 | - $this->drivers = $drivers; |
|
36 | + public function __construct(DriverManager $drivers, Dispatcher $dispatcher, Manager $manager) |
|
37 | + { |
|
38 | + $this->drivers = $drivers; |
|
39 | 39 | |
40 | - $this->dispatcher = $dispatcher; |
|
40 | + $this->dispatcher = $dispatcher; |
|
41 | 41 | |
42 | - $this->manager = $manager; |
|
43 | - } |
|
42 | + $this->manager = $manager; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * Return a new Mapper instance |
|
47 | - * |
|
48 | - * @param string $entityClass |
|
49 | - * @param string $entityMap |
|
50 | - * @return Mapper |
|
51 | - */ |
|
52 | - public function make($entityClass, EntityMap $entityMap) |
|
53 | - { |
|
54 | - $driver = $entityMap->getDriver(); |
|
45 | + /** |
|
46 | + * Return a new Mapper instance |
|
47 | + * |
|
48 | + * @param string $entityClass |
|
49 | + * @param string $entityMap |
|
50 | + * @return Mapper |
|
51 | + */ |
|
52 | + public function make($entityClass, EntityMap $entityMap) |
|
53 | + { |
|
54 | + $driver = $entityMap->getDriver(); |
|
55 | 55 | |
56 | - $connection = $entityMap->getConnection(); |
|
56 | + $connection = $entityMap->getConnection(); |
|
57 | 57 | |
58 | - $adapter = $this->drivers->getAdapter($driver, $connection); |
|
58 | + $adapter = $this->drivers->getAdapter($driver, $connection); |
|
59 | 59 | |
60 | - $entityMap->setDateFormat($adapter->getDateFormat()); |
|
60 | + $entityMap->setDateFormat($adapter->getDateFormat()); |
|
61 | 61 | |
62 | - $mapper = new Mapper($entityMap, $adapter, $this->dispatcher, $this->manager); |
|
62 | + $mapper = new Mapper($entityMap, $adapter, $this->dispatcher, $this->manager); |
|
63 | 63 | |
64 | - // Fire Initializing Event |
|
65 | - $mapper->fireEvent('initializing', $mapper); |
|
64 | + // Fire Initializing Event |
|
65 | + $mapper->fireEvent('initializing', $mapper); |
|
66 | 66 | |
67 | - // Proceed necessary parsing on the EntityMap object |
|
68 | - $entityMap->initialize(); |
|
67 | + // Proceed necessary parsing on the EntityMap object |
|
68 | + $entityMap->initialize(); |
|
69 | 69 | |
70 | - // Fire Initialized Event |
|
71 | - $mapper->fireEvent('initialized', $mapper); |
|
70 | + // Fire Initialized Event |
|
71 | + $mapper->fireEvent('initialized', $mapper); |
|
72 | 72 | |
73 | - return $mapper; |
|
74 | - } |
|
73 | + return $mapper; |
|
74 | + } |
|
75 | 75 | } |
@@ -81,7 +81,6 @@ discard block |
||
81 | 81 | /** |
82 | 82 | * Create a new Analogue Query Builder instance. |
83 | 83 | * |
84 | - * @param \Analogue\ORM\Drivers\QueryAdapter $query |
|
85 | 84 | * @param \Analogue\ORM\System\Mapper $mapper |
86 | 85 | * @return void |
87 | 86 | */ |
@@ -190,7 +189,7 @@ discard block |
||
190 | 189 | * Execute the query and get the first result or throw an exception. |
191 | 190 | * |
192 | 191 | * @param array $columns |
193 | - * @return Mappable |
|
192 | + * @return \Analogue\ORM\Entity |
|
194 | 193 | * |
195 | 194 | * @throws EntityNotFoundException |
196 | 195 | */ |
@@ -258,7 +257,7 @@ discard block |
||
258 | 257 | * |
259 | 258 | * @param int $perPage |
260 | 259 | * @param array $columns |
261 | - * @return \Illuminate\Pagination\Paginator |
|
260 | + * @return LengthAwarePaginator |
|
262 | 261 | */ |
263 | 262 | public function paginate($perPage = null, $columns = array('*')) |
264 | 263 | { |
@@ -17,833 +17,833 @@ |
||
17 | 17 | class Query |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * Mapper Instance |
|
22 | - * |
|
23 | - * @var \Analogue\ORM\System\Mapper |
|
24 | - */ |
|
25 | - protected $mapper; |
|
26 | - |
|
27 | - /** |
|
28 | - * DB Adatper |
|
29 | - * |
|
30 | - * @var \Analogue\ORM\Drivers\DBAdapter |
|
31 | - */ |
|
32 | - protected $adapter; |
|
33 | - |
|
34 | - /** |
|
35 | - * Query Builder Instance |
|
36 | - * |
|
37 | - * @var \Analogue\ORM\Drivers\QueryAdapter |
|
38 | - */ |
|
39 | - protected $query; |
|
40 | - |
|
41 | - /** |
|
42 | - * Entity Map Instance |
|
43 | - * |
|
44 | - * @var \Analogue\ORM\EntityMap |
|
45 | - */ |
|
46 | - protected $entityMap; |
|
20 | + /** |
|
21 | + * Mapper Instance |
|
22 | + * |
|
23 | + * @var \Analogue\ORM\System\Mapper |
|
24 | + */ |
|
25 | + protected $mapper; |
|
26 | + |
|
27 | + /** |
|
28 | + * DB Adatper |
|
29 | + * |
|
30 | + * @var \Analogue\ORM\Drivers\DBAdapter |
|
31 | + */ |
|
32 | + protected $adapter; |
|
33 | + |
|
34 | + /** |
|
35 | + * Query Builder Instance |
|
36 | + * |
|
37 | + * @var \Analogue\ORM\Drivers\QueryAdapter |
|
38 | + */ |
|
39 | + protected $query; |
|
40 | + |
|
41 | + /** |
|
42 | + * Entity Map Instance |
|
43 | + * |
|
44 | + * @var \Analogue\ORM\EntityMap |
|
45 | + */ |
|
46 | + protected $entityMap; |
|
47 | 47 | |
48 | - /** |
|
49 | - * The relationships that should be eager loaded. |
|
50 | - * |
|
51 | - * @var array |
|
52 | - */ |
|
53 | - protected $eagerLoad = []; |
|
54 | - |
|
55 | - /** |
|
56 | - * All of the registered builder macros. |
|
57 | - * |
|
58 | - * @var array |
|
59 | - */ |
|
60 | - protected $macros = []; |
|
61 | - |
|
62 | - /** |
|
63 | - * The methods that should be returned from query builder. |
|
64 | - * |
|
65 | - * @var array |
|
66 | - */ |
|
67 | - protected $passthru = [ |
|
68 | - 'toSql', 'lists', 'pluck', 'count', |
|
69 | - 'min', 'max', 'avg', 'sum', 'exists', 'getBindings', |
|
70 | - ]; |
|
71 | - |
|
72 | - /** |
|
73 | - * Query Builder Blacklist |
|
74 | - */ |
|
75 | - protected $blacklist = [ |
|
76 | - 'insert', 'insertGetId', 'lock', 'lockForUpdate', |
|
77 | - 'sharedLock', 'update', 'increment', 'decrement', 'delete', |
|
78 | - 'truncate', 'raw', |
|
79 | - ]; |
|
80 | - |
|
81 | - /** |
|
82 | - * Create a new Analogue Query Builder instance. |
|
83 | - * |
|
84 | - * @param \Analogue\ORM\Drivers\QueryAdapter $query |
|
85 | - * @param \Analogue\ORM\System\Mapper $mapper |
|
86 | - * @return void |
|
87 | - */ |
|
88 | - public function __construct(Mapper $mapper, DBAdapter $adapter) |
|
89 | - { |
|
90 | - $this->mapper = $mapper; |
|
91 | - |
|
92 | - $this->adapter = $adapter; |
|
93 | - |
|
94 | - $this->entityMap = $mapper->getEntityMap(); |
|
95 | - |
|
96 | - // Specify the table to work on |
|
97 | - $this->query = $adapter->getQuery()->from($this->entityMap->getTable()); |
|
98 | - |
|
99 | - $this->with($this->entityMap->getEagerloadedRelationships()); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Run the query and return the result |
|
104 | - * |
|
105 | - * @param array $columns |
|
106 | - * @return \Analogue\ORM\EntityCollection |
|
107 | - */ |
|
108 | - public function get($columns = array('*')) |
|
109 | - { |
|
110 | - $entities = $this->getEntities($columns); |
|
111 | - |
|
112 | - // If we actually found models we will also eager load any relationships that |
|
113 | - // have been specified as needing to be eager loaded, which will solve the |
|
114 | - // n+1 query issue for the developers to avoid running a lot of queries. |
|
48 | + /** |
|
49 | + * The relationships that should be eager loaded. |
|
50 | + * |
|
51 | + * @var array |
|
52 | + */ |
|
53 | + protected $eagerLoad = []; |
|
54 | + |
|
55 | + /** |
|
56 | + * All of the registered builder macros. |
|
57 | + * |
|
58 | + * @var array |
|
59 | + */ |
|
60 | + protected $macros = []; |
|
61 | + |
|
62 | + /** |
|
63 | + * The methods that should be returned from query builder. |
|
64 | + * |
|
65 | + * @var array |
|
66 | + */ |
|
67 | + protected $passthru = [ |
|
68 | + 'toSql', 'lists', 'pluck', 'count', |
|
69 | + 'min', 'max', 'avg', 'sum', 'exists', 'getBindings', |
|
70 | + ]; |
|
71 | + |
|
72 | + /** |
|
73 | + * Query Builder Blacklist |
|
74 | + */ |
|
75 | + protected $blacklist = [ |
|
76 | + 'insert', 'insertGetId', 'lock', 'lockForUpdate', |
|
77 | + 'sharedLock', 'update', 'increment', 'decrement', 'delete', |
|
78 | + 'truncate', 'raw', |
|
79 | + ]; |
|
80 | + |
|
81 | + /** |
|
82 | + * Create a new Analogue Query Builder instance. |
|
83 | + * |
|
84 | + * @param \Analogue\ORM\Drivers\QueryAdapter $query |
|
85 | + * @param \Analogue\ORM\System\Mapper $mapper |
|
86 | + * @return void |
|
87 | + */ |
|
88 | + public function __construct(Mapper $mapper, DBAdapter $adapter) |
|
89 | + { |
|
90 | + $this->mapper = $mapper; |
|
91 | + |
|
92 | + $this->adapter = $adapter; |
|
93 | + |
|
94 | + $this->entityMap = $mapper->getEntityMap(); |
|
95 | + |
|
96 | + // Specify the table to work on |
|
97 | + $this->query = $adapter->getQuery()->from($this->entityMap->getTable()); |
|
98 | + |
|
99 | + $this->with($this->entityMap->getEagerloadedRelationships()); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Run the query and return the result |
|
104 | + * |
|
105 | + * @param array $columns |
|
106 | + * @return \Analogue\ORM\EntityCollection |
|
107 | + */ |
|
108 | + public function get($columns = array('*')) |
|
109 | + { |
|
110 | + $entities = $this->getEntities($columns); |
|
111 | + |
|
112 | + // If we actually found models we will also eager load any relationships that |
|
113 | + // have been specified as needing to be eager loaded, which will solve the |
|
114 | + // n+1 query issue for the developers to avoid running a lot of queries. |
|
115 | 115 | |
116 | - if (count($entities) > 0) { |
|
117 | - $entities = $this->eagerLoadRelations($entities); |
|
118 | - } |
|
119 | - |
|
120 | - return $this->entityMap->newCollection($entities); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Find an entity by its primary key |
|
125 | - * |
|
126 | - * @param string|integer $id |
|
127 | - * @param array $columns |
|
128 | - * @return \Analogue\ORM\Mappable |
|
129 | - */ |
|
130 | - public function find($id, $columns = array('*')) |
|
131 | - { |
|
132 | - if (is_array($id)) { |
|
133 | - return $this->findMany($id, $columns); |
|
134 | - } |
|
135 | - |
|
136 | - $this->query->where($this->entityMap->getQualifiedKeyName(), '=', $id); |
|
137 | - |
|
138 | - return $this->first($columns); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Find many entities by their primary keys. |
|
143 | - * |
|
144 | - * @param array $id |
|
145 | - * @param array $columns |
|
146 | - * @return EntityCollection |
|
147 | - */ |
|
148 | - public function findMany($id, $columns = array('*')) |
|
149 | - { |
|
150 | - if (empty($id)) { |
|
151 | - return new EntityCollection; |
|
152 | - } |
|
153 | - |
|
154 | - $this->query->whereIn($this->entityMap->getQualifiedKeyName(), $id); |
|
155 | - |
|
156 | - return $this->get($columns); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Find a model by its primary key or throw an exception. |
|
161 | - * |
|
162 | - * @param mixed $id |
|
163 | - * @param array $columns |
|
164 | - * @return mixed|static |
|
165 | - * |
|
166 | - * @throws \Analogue\ORM\Exception\EntityNotFoundException |
|
167 | - */ |
|
168 | - public function findOrFail($id, $columns = array('*')) |
|
169 | - { |
|
170 | - if (! is_null($entity = $this->find($id, $columns))) { |
|
171 | - return $entity; |
|
172 | - } |
|
173 | - |
|
174 | - throw (new EntityNotFoundException)->setEntity(get_class($this->entityMap)); |
|
175 | - } |
|
176 | - |
|
177 | - |
|
178 | - /** |
|
179 | - * Execute the query and get the first result. |
|
180 | - * |
|
181 | - * @param array $columns |
|
182 | - * @return \Analogue\ORM\Entity |
|
183 | - */ |
|
184 | - public function first($columns = array('*')) |
|
185 | - { |
|
186 | - return $this->take(1)->get($columns)->first(); |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Execute the query and get the first result or throw an exception. |
|
191 | - * |
|
192 | - * @param array $columns |
|
193 | - * @return Mappable |
|
194 | - * |
|
195 | - * @throws EntityNotFoundException |
|
196 | - */ |
|
197 | - public function firstOrFail($columns = array('*')) |
|
198 | - { |
|
199 | - if (! is_null($entity = $this->first($columns))) { |
|
200 | - return $entity; |
|
201 | - } |
|
202 | - |
|
203 | - throw (new EntityNotFoundException)->setEntity(get_class($this->entityMap)); |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Pluck a single column from the database. |
|
208 | - * |
|
209 | - * @param string $column |
|
210 | - * @return mixed |
|
211 | - */ |
|
212 | - public function pluck($column) |
|
213 | - { |
|
214 | - $result = $this->first(array($column)); |
|
215 | - |
|
216 | - if ($result) { |
|
217 | - return $result->{$column}; |
|
218 | - } |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Chunk the results of the query. |
|
223 | - * |
|
224 | - * @param int $count |
|
225 | - * @param callable $callback |
|
226 | - * @return void |
|
227 | - */ |
|
228 | - public function chunk($count, callable $callback) |
|
229 | - { |
|
230 | - $results = $this->forPage($page = 1, $count)->get(); |
|
231 | - |
|
232 | - while (count($results) > 0) { |
|
233 | - // On each chunk result set, we will pass them to the callback and then let the |
|
234 | - // developer take care of everything within the callback, which allows us to |
|
235 | - // keep the memory low for spinning through large result sets for working. |
|
236 | - call_user_func($callback, $results); |
|
237 | - |
|
238 | - $page++; |
|
239 | - |
|
240 | - $results = $this->forPage($page, $count)->get(); |
|
241 | - } |
|
242 | - } |
|
116 | + if (count($entities) > 0) { |
|
117 | + $entities = $this->eagerLoadRelations($entities); |
|
118 | + } |
|
119 | + |
|
120 | + return $this->entityMap->newCollection($entities); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Find an entity by its primary key |
|
125 | + * |
|
126 | + * @param string|integer $id |
|
127 | + * @param array $columns |
|
128 | + * @return \Analogue\ORM\Mappable |
|
129 | + */ |
|
130 | + public function find($id, $columns = array('*')) |
|
131 | + { |
|
132 | + if (is_array($id)) { |
|
133 | + return $this->findMany($id, $columns); |
|
134 | + } |
|
135 | + |
|
136 | + $this->query->where($this->entityMap->getQualifiedKeyName(), '=', $id); |
|
137 | + |
|
138 | + return $this->first($columns); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Find many entities by their primary keys. |
|
143 | + * |
|
144 | + * @param array $id |
|
145 | + * @param array $columns |
|
146 | + * @return EntityCollection |
|
147 | + */ |
|
148 | + public function findMany($id, $columns = array('*')) |
|
149 | + { |
|
150 | + if (empty($id)) { |
|
151 | + return new EntityCollection; |
|
152 | + } |
|
153 | + |
|
154 | + $this->query->whereIn($this->entityMap->getQualifiedKeyName(), $id); |
|
155 | + |
|
156 | + return $this->get($columns); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Find a model by its primary key or throw an exception. |
|
161 | + * |
|
162 | + * @param mixed $id |
|
163 | + * @param array $columns |
|
164 | + * @return mixed|static |
|
165 | + * |
|
166 | + * @throws \Analogue\ORM\Exception\EntityNotFoundException |
|
167 | + */ |
|
168 | + public function findOrFail($id, $columns = array('*')) |
|
169 | + { |
|
170 | + if (! is_null($entity = $this->find($id, $columns))) { |
|
171 | + return $entity; |
|
172 | + } |
|
173 | + |
|
174 | + throw (new EntityNotFoundException)->setEntity(get_class($this->entityMap)); |
|
175 | + } |
|
176 | + |
|
177 | + |
|
178 | + /** |
|
179 | + * Execute the query and get the first result. |
|
180 | + * |
|
181 | + * @param array $columns |
|
182 | + * @return \Analogue\ORM\Entity |
|
183 | + */ |
|
184 | + public function first($columns = array('*')) |
|
185 | + { |
|
186 | + return $this->take(1)->get($columns)->first(); |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Execute the query and get the first result or throw an exception. |
|
191 | + * |
|
192 | + * @param array $columns |
|
193 | + * @return Mappable |
|
194 | + * |
|
195 | + * @throws EntityNotFoundException |
|
196 | + */ |
|
197 | + public function firstOrFail($columns = array('*')) |
|
198 | + { |
|
199 | + if (! is_null($entity = $this->first($columns))) { |
|
200 | + return $entity; |
|
201 | + } |
|
202 | + |
|
203 | + throw (new EntityNotFoundException)->setEntity(get_class($this->entityMap)); |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Pluck a single column from the database. |
|
208 | + * |
|
209 | + * @param string $column |
|
210 | + * @return mixed |
|
211 | + */ |
|
212 | + public function pluck($column) |
|
213 | + { |
|
214 | + $result = $this->first(array($column)); |
|
215 | + |
|
216 | + if ($result) { |
|
217 | + return $result->{$column}; |
|
218 | + } |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Chunk the results of the query. |
|
223 | + * |
|
224 | + * @param int $count |
|
225 | + * @param callable $callback |
|
226 | + * @return void |
|
227 | + */ |
|
228 | + public function chunk($count, callable $callback) |
|
229 | + { |
|
230 | + $results = $this->forPage($page = 1, $count)->get(); |
|
231 | + |
|
232 | + while (count($results) > 0) { |
|
233 | + // On each chunk result set, we will pass them to the callback and then let the |
|
234 | + // developer take care of everything within the callback, which allows us to |
|
235 | + // keep the memory low for spinning through large result sets for working. |
|
236 | + call_user_func($callback, $results); |
|
237 | + |
|
238 | + $page++; |
|
239 | + |
|
240 | + $results = $this->forPage($page, $count)->get(); |
|
241 | + } |
|
242 | + } |
|
243 | 243 | |
244 | - /** |
|
245 | - * Get an array with the values of a given column. |
|
246 | - * |
|
247 | - * @param string $column |
|
248 | - * @param string $key |
|
249 | - * @return array |
|
250 | - */ |
|
251 | - public function lists($column, $key = null) |
|
252 | - { |
|
253 | - return $this->query->lists($column, $key); |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * Get a paginator for the "select" statement. |
|
258 | - * |
|
259 | - * @param int $perPage |
|
260 | - * @param array $columns |
|
261 | - * @return \Illuminate\Pagination\Paginator |
|
262 | - */ |
|
263 | - public function paginate($perPage = null, $columns = array('*')) |
|
264 | - { |
|
265 | - $total = $this->query->getCountForPagination(); |
|
266 | - |
|
267 | - $this->query->forPage( |
|
268 | - $page = Paginator::resolveCurrentPage(), |
|
269 | - $perPage = $perPage ?: $this->entityMap->getPerPage() |
|
270 | - ); |
|
271 | - |
|
272 | - return new LengthAwarePaginator($this->get($columns)->all(), $total, $perPage, $page, [ |
|
273 | - 'path' => Paginator::resolveCurrentPath() |
|
274 | - ]); |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * Get a paginator for a grouped statement. |
|
279 | - * |
|
280 | - * @param \Illuminate\Pagination\Factory $paginator |
|
281 | - * @param int $perPage |
|
282 | - * @param array $columns |
|
283 | - * @return \Illuminate\Pagination\Paginator |
|
284 | - */ |
|
285 | - protected function groupedPaginate($paginator, $perPage, $columns) |
|
286 | - { |
|
287 | - $results = $this->get($columns)->all(); |
|
288 | - |
|
289 | - return $this->query->buildRawPaginator($paginator, $results, $perPage); |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * Get a paginator for an ungrouped statement. |
|
294 | - * |
|
295 | - * @param \Illuminate\Pagination\Factory $paginator |
|
296 | - * @param int $perPage |
|
297 | - * @param array $columns |
|
298 | - * @return \Illuminate\Pagination\Paginator |
|
299 | - */ |
|
300 | - protected function ungroupedPaginate($paginator, $perPage, $columns) |
|
301 | - { |
|
302 | - $total = $this->query->getPaginationCount(); |
|
303 | - |
|
304 | - // Once we have the paginator we need to set the limit and offset values for |
|
305 | - // the query so we can get the properly paginated items. Once we have an |
|
306 | - // array of items we can create the paginator instances for the items. |
|
307 | - $page = $paginator->getCurrentPage($total); |
|
308 | - |
|
309 | - $this->query->forPage($page, $perPage); |
|
310 | - |
|
311 | - return $paginator->make($this->get($columns)->all(), $total, $perPage); |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * Paginate the given query into a simple paginator. |
|
316 | - * |
|
317 | - * @param int $perPage |
|
318 | - * @param array $columns |
|
319 | - * @return \Illuminate\Contracts\Pagination\Paginator |
|
320 | - */ |
|
321 | - public function simplePaginate($perPage = null, $columns = ['*']) |
|
322 | - { |
|
323 | - $page = Paginator::resolveCurrentPage(); |
|
324 | - |
|
325 | - $perPage = $perPage ?: $this->entityMap->getPerPage(); |
|
326 | - |
|
327 | - $this->skip(($page - 1) * $perPage)->take($perPage + 1); |
|
328 | - |
|
329 | - return new Paginator($this->get($columns)->all(), $perPage, $page, [ |
|
330 | - 'path' => Paginator::resolveCurrentPath() |
|
331 | - ]); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * Add a basic where clause to the query. |
|
336 | - * |
|
337 | - * @param string $column |
|
338 | - * @param string $operator |
|
339 | - * @param mixed $value |
|
340 | - * @param string $boolean |
|
341 | - * @return $this |
|
342 | - */ |
|
343 | - public function where($column, $operator = null, $value = null, $boolean = 'and') |
|
344 | - { |
|
345 | - if ($column instanceof Closure) { |
|
346 | - $query = $this->newQueryWithoutScopes(); |
|
347 | - |
|
348 | - call_user_func($column, $query); |
|
349 | - |
|
350 | - $this->query->addNestedWhereQuery($query->getQuery(), $boolean); |
|
351 | - } else { |
|
352 | - call_user_func_array(array($this->query, 'where'), func_get_args()); |
|
353 | - } |
|
354 | - |
|
355 | - return $this; |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * Add an "or where" clause to the query. |
|
360 | - * |
|
361 | - * @param string $column |
|
362 | - * @param string $operator |
|
363 | - * @param mixed $value |
|
364 | - * @return \Analogue\ORM\System\Query |
|
365 | - */ |
|
366 | - public function orWhere($column, $operator = null, $value = null) |
|
367 | - { |
|
368 | - return $this->where($column, $operator, $value, 'or'); |
|
369 | - } |
|
370 | - |
|
371 | - /** |
|
372 | - * Add a relationship count condition to the query. |
|
373 | - * |
|
374 | - * @param string $relation |
|
375 | - * @param string $operator |
|
376 | - * @param int $count |
|
377 | - * @param string $boolean |
|
378 | - * @param \Closure $callback |
|
379 | - * @return \Analogue\ORM\System\Query |
|
380 | - */ |
|
381 | - public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', $callback = null) |
|
382 | - { |
|
383 | - $entity = $this->mapper->newInstance(); |
|
384 | - |
|
385 | - $relation = $this->getHasRelationQuery($relation, $entity); |
|
386 | - |
|
387 | - $query = $relation->getRelationCountQuery($relation->getRelatedMapper()->getQuery(), $this); |
|
388 | - |
|
389 | - if ($callback) { |
|
390 | - call_user_func($callback, $query); |
|
391 | - } |
|
392 | - |
|
393 | - return $this->addHasWhere($query, $relation, $operator, $count, $boolean); |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * Add a relationship count condition to the query with where clauses. |
|
398 | - * |
|
399 | - * @param string $relation |
|
400 | - * @param \Closure $callback |
|
401 | - * @param string $operator |
|
402 | - * @param int $count |
|
403 | - * @return \Analogue\ORM\System\Query |
|
404 | - */ |
|
405 | - public function whereHas($relation, Closure $callback, $operator = '>=', $count = 1) |
|
406 | - { |
|
407 | - return $this->has($relation, $operator, $count, 'and', $callback); |
|
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * Add a relationship count condition to the query with an "or". |
|
412 | - * |
|
413 | - * @param string $relation |
|
414 | - * @param string $operator |
|
415 | - * @param int $count |
|
416 | - * @return \Analogue\ORM\System\Query |
|
417 | - */ |
|
418 | - public function orHas($relation, $operator = '>=', $count = 1) |
|
419 | - { |
|
420 | - return $this->has($relation, $operator, $count, 'or'); |
|
421 | - } |
|
422 | - |
|
423 | - /** |
|
424 | - * Add a relationship count condition to the query with where clauses and an "or". |
|
425 | - * |
|
426 | - * @param string $relation |
|
427 | - * @param \Closure $callback |
|
428 | - * @param string $operator |
|
429 | - * @param int $count |
|
430 | - * @return \Analogue\ORM\System\Query |
|
431 | - */ |
|
432 | - public function orWhereHas($relation, Closure $callback, $operator = '>=', $count = 1) |
|
433 | - { |
|
434 | - return $this->has($relation, $operator, $count, 'or', $callback); |
|
435 | - } |
|
436 | - |
|
437 | - /** |
|
438 | - * Add the "has" condition where clause to the query. |
|
439 | - * |
|
440 | - * @param \Analogue\ORM\System\Query $hasQuery |
|
441 | - * @param \Analogue\ORM\Relationships\Relationship $relation |
|
442 | - * @param string $operator |
|
443 | - * @param int $count |
|
444 | - * @param string $boolean |
|
445 | - * @return \Analogue\ORM\System\Query |
|
446 | - */ |
|
447 | - protected function addHasWhere(Query $hasQuery, Relationship $relation, $operator, $count, $boolean) |
|
448 | - { |
|
449 | - $this->mergeWheresToHas($hasQuery, $relation); |
|
450 | - |
|
451 | - if (is_numeric($count)) { |
|
452 | - $count = new Expression($count); |
|
453 | - } |
|
454 | - |
|
455 | - return $this->where(new Expression('('.$hasQuery->toSql().')'), $operator, $count, $boolean); |
|
456 | - } |
|
457 | - |
|
458 | - /** |
|
459 | - * Merge the "wheres" from a relation query to a has query. |
|
460 | - * |
|
461 | - * @param \Analogue\ORM\System\Query $hasQuery |
|
462 | - * @param \Analogue\ORM\Relationships\Relationship $relation |
|
463 | - * @return void |
|
464 | - */ |
|
465 | - protected function mergeWheresToHas(Query $hasQuery, Relationship $relation) |
|
466 | - { |
|
467 | - // Here we have the "has" query and the original relation. We need to copy over any |
|
468 | - // where clauses the developer may have put in the relationship function over to |
|
469 | - // the has query, and then copy the bindings from the "has" query to the main. |
|
470 | - $relationQuery = $relation->getBaseQuery(); |
|
471 | - |
|
472 | - $hasQuery->mergeWheres( |
|
473 | - $relationQuery->wheres, $relationQuery->getBindings() |
|
474 | - ); |
|
475 | - |
|
476 | - $this->query->mergeBindings($hasQuery->getQuery()); |
|
477 | - } |
|
478 | - |
|
479 | - /** |
|
480 | - * Get the "has relation" base query instance. |
|
481 | - * |
|
482 | - * @param string $relation |
|
483 | - * @return \Analogue\ORM\System\Query |
|
484 | - */ |
|
485 | - protected function getHasRelationQuery($relation, $entity) |
|
486 | - { |
|
487 | - return Relationship::noConstraints(function () use ($relation, $entity) { |
|
488 | - return $this->entityMap->$relation($entity); |
|
489 | - }); |
|
490 | - } |
|
491 | - |
|
492 | - /** |
|
493 | - * Get the table for the current query object |
|
494 | - * |
|
495 | - * @return string |
|
496 | - */ |
|
497 | - public function getTable() |
|
498 | - { |
|
499 | - return $this->entityMap->getTable(); |
|
500 | - } |
|
501 | - |
|
502 | - /** |
|
503 | - * Set the relationships that should be eager loaded. |
|
504 | - * |
|
505 | - * @param mixed $relations |
|
506 | - * @return $this |
|
507 | - */ |
|
508 | - public function with($relations) |
|
509 | - { |
|
510 | - if (is_string($relations)) { |
|
511 | - $relations = func_get_args(); |
|
512 | - } |
|
513 | - |
|
514 | - $eagers = $this->parseRelations($relations); |
|
515 | - |
|
516 | - $this->eagerLoad = array_merge($this->eagerLoad, $eagers); |
|
517 | - |
|
518 | - return $this; |
|
519 | - } |
|
520 | - |
|
521 | - /** |
|
522 | - * Parse a list of relations into individuals. |
|
523 | - * |
|
524 | - * @param array $relations |
|
525 | - * @return array |
|
526 | - */ |
|
527 | - protected function parseRelations(array $relations) |
|
528 | - { |
|
529 | - $results = array(); |
|
530 | - |
|
531 | - foreach ($relations as $name => $constraints) { |
|
532 | - // If the "relation" value is actually a numeric key, we can assume that no |
|
533 | - // constraints have been specified for the eager load and we'll just put |
|
534 | - // an empty Closure with the loader so that we can treat all the same. |
|
535 | - if (is_numeric($name)) { |
|
536 | - $f = function () {}; |
|
537 | - |
|
538 | - list($name, $constraints) = array($constraints, $f); |
|
539 | - } |
|
540 | - |
|
541 | - // We need to separate out any nested includes. Which allows the developers |
|
542 | - // to load deep relationships using "dots" without stating each level of |
|
543 | - // the relationship with its own key in the array of eager load names. |
|
544 | - $results = $this->parseNested($name, $results); |
|
545 | - |
|
546 | - $results[$name] = $constraints; |
|
547 | - } |
|
548 | - |
|
549 | - return $results; |
|
550 | - } |
|
551 | - |
|
552 | - |
|
553 | - /** |
|
554 | - * Parse the nested relationships in a relation. |
|
555 | - * |
|
556 | - * @param string $name |
|
557 | - * @param array $results |
|
558 | - * @return array |
|
559 | - */ |
|
560 | - protected function parseNested($name, $results) |
|
561 | - { |
|
562 | - $progress = array(); |
|
563 | - |
|
564 | - // If the relation has already been set on the result array, we will not set it |
|
565 | - // again, since that would override any constraints that were already placed |
|
566 | - // on the relationships. We will only set the ones that are not specified. |
|
567 | - foreach (explode('.', $name) as $segment) { |
|
568 | - $progress[] = $segment; |
|
569 | - |
|
570 | - if (! isset($results[$last = implode('.', $progress)])) { |
|
571 | - $results[$last] = function () {}; |
|
572 | - } |
|
573 | - } |
|
574 | - |
|
575 | - return $results; |
|
576 | - } |
|
577 | - |
|
578 | - /** |
|
579 | - * Get the relationships being eagerly loaded. |
|
580 | - * |
|
581 | - * @return array |
|
582 | - */ |
|
583 | - public function getEagerLoads() |
|
584 | - { |
|
585 | - return $this->eagerLoad; |
|
586 | - } |
|
587 | - |
|
588 | - /** |
|
589 | - * Set the relationships being eagerly loaded. |
|
590 | - * |
|
591 | - * @param array $eagerLoad |
|
592 | - * @return void |
|
593 | - */ |
|
594 | - public function setEagerLoads(array $eagerLoad) |
|
595 | - { |
|
596 | - $this->eagerLoad = $eagerLoad; |
|
597 | - } |
|
598 | - |
|
599 | - /** |
|
600 | - * Eager load the relationships for the entities. |
|
601 | - * |
|
602 | - * @param array $entities |
|
603 | - * @return array |
|
604 | - */ |
|
605 | - public function eagerLoadRelations($entities) |
|
606 | - { |
|
607 | - foreach ($this->eagerLoad as $name => $constraints) { |
|
608 | - // For nested eager loads we'll skip loading them here and they will be set as an |
|
609 | - // eager load on the query to retrieve the relation so that they will be eager |
|
610 | - // loaded on that query, because that is where they get hydrated as models. |
|
611 | - if (strpos($name, '.') === false) { |
|
612 | - $entities = $this->loadRelation($entities, $name, $constraints); |
|
613 | - } |
|
614 | - } |
|
615 | - |
|
616 | - return $entities; |
|
617 | - } |
|
618 | - |
|
619 | - /** |
|
620 | - * Eagerly load the relationship on a set of entities. |
|
621 | - * |
|
622 | - * @param array $entities |
|
623 | - * @param string $name |
|
624 | - * @param \Closure $constraints |
|
625 | - * @return array |
|
626 | - */ |
|
627 | - protected function loadRelation(array $entities, $name, Closure $constraints) |
|
628 | - { |
|
629 | - // First we will "back up" the existing where conditions on the query so we can |
|
630 | - // add our eager constraints. Then we will merge the wheres that were on the |
|
631 | - // query back to it in order that any where conditions might be specified. |
|
632 | - $relation = $this->getRelation($name); |
|
633 | - |
|
634 | - $relation->addEagerConstraints($entities); |
|
635 | - |
|
636 | - call_user_func($constraints, $relation); |
|
637 | - |
|
638 | - $entities = $relation->initRelation($entities, $name); |
|
639 | - |
|
640 | - // Once we have the results, we just match those back up to their parent models |
|
641 | - // using the relationship instance. Then we just return the finished arrays |
|
642 | - // of models which have been eagerly hydrated and are readied for return. |
|
244 | + /** |
|
245 | + * Get an array with the values of a given column. |
|
246 | + * |
|
247 | + * @param string $column |
|
248 | + * @param string $key |
|
249 | + * @return array |
|
250 | + */ |
|
251 | + public function lists($column, $key = null) |
|
252 | + { |
|
253 | + return $this->query->lists($column, $key); |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * Get a paginator for the "select" statement. |
|
258 | + * |
|
259 | + * @param int $perPage |
|
260 | + * @param array $columns |
|
261 | + * @return \Illuminate\Pagination\Paginator |
|
262 | + */ |
|
263 | + public function paginate($perPage = null, $columns = array('*')) |
|
264 | + { |
|
265 | + $total = $this->query->getCountForPagination(); |
|
266 | + |
|
267 | + $this->query->forPage( |
|
268 | + $page = Paginator::resolveCurrentPage(), |
|
269 | + $perPage = $perPage ?: $this->entityMap->getPerPage() |
|
270 | + ); |
|
271 | + |
|
272 | + return new LengthAwarePaginator($this->get($columns)->all(), $total, $perPage, $page, [ |
|
273 | + 'path' => Paginator::resolveCurrentPath() |
|
274 | + ]); |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * Get a paginator for a grouped statement. |
|
279 | + * |
|
280 | + * @param \Illuminate\Pagination\Factory $paginator |
|
281 | + * @param int $perPage |
|
282 | + * @param array $columns |
|
283 | + * @return \Illuminate\Pagination\Paginator |
|
284 | + */ |
|
285 | + protected function groupedPaginate($paginator, $perPage, $columns) |
|
286 | + { |
|
287 | + $results = $this->get($columns)->all(); |
|
288 | + |
|
289 | + return $this->query->buildRawPaginator($paginator, $results, $perPage); |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * Get a paginator for an ungrouped statement. |
|
294 | + * |
|
295 | + * @param \Illuminate\Pagination\Factory $paginator |
|
296 | + * @param int $perPage |
|
297 | + * @param array $columns |
|
298 | + * @return \Illuminate\Pagination\Paginator |
|
299 | + */ |
|
300 | + protected function ungroupedPaginate($paginator, $perPage, $columns) |
|
301 | + { |
|
302 | + $total = $this->query->getPaginationCount(); |
|
303 | + |
|
304 | + // Once we have the paginator we need to set the limit and offset values for |
|
305 | + // the query so we can get the properly paginated items. Once we have an |
|
306 | + // array of items we can create the paginator instances for the items. |
|
307 | + $page = $paginator->getCurrentPage($total); |
|
308 | + |
|
309 | + $this->query->forPage($page, $perPage); |
|
310 | + |
|
311 | + return $paginator->make($this->get($columns)->all(), $total, $perPage); |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * Paginate the given query into a simple paginator. |
|
316 | + * |
|
317 | + * @param int $perPage |
|
318 | + * @param array $columns |
|
319 | + * @return \Illuminate\Contracts\Pagination\Paginator |
|
320 | + */ |
|
321 | + public function simplePaginate($perPage = null, $columns = ['*']) |
|
322 | + { |
|
323 | + $page = Paginator::resolveCurrentPage(); |
|
324 | + |
|
325 | + $perPage = $perPage ?: $this->entityMap->getPerPage(); |
|
326 | + |
|
327 | + $this->skip(($page - 1) * $perPage)->take($perPage + 1); |
|
328 | + |
|
329 | + return new Paginator($this->get($columns)->all(), $perPage, $page, [ |
|
330 | + 'path' => Paginator::resolveCurrentPath() |
|
331 | + ]); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * Add a basic where clause to the query. |
|
336 | + * |
|
337 | + * @param string $column |
|
338 | + * @param string $operator |
|
339 | + * @param mixed $value |
|
340 | + * @param string $boolean |
|
341 | + * @return $this |
|
342 | + */ |
|
343 | + public function where($column, $operator = null, $value = null, $boolean = 'and') |
|
344 | + { |
|
345 | + if ($column instanceof Closure) { |
|
346 | + $query = $this->newQueryWithoutScopes(); |
|
347 | + |
|
348 | + call_user_func($column, $query); |
|
349 | + |
|
350 | + $this->query->addNestedWhereQuery($query->getQuery(), $boolean); |
|
351 | + } else { |
|
352 | + call_user_func_array(array($this->query, 'where'), func_get_args()); |
|
353 | + } |
|
354 | + |
|
355 | + return $this; |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * Add an "or where" clause to the query. |
|
360 | + * |
|
361 | + * @param string $column |
|
362 | + * @param string $operator |
|
363 | + * @param mixed $value |
|
364 | + * @return \Analogue\ORM\System\Query |
|
365 | + */ |
|
366 | + public function orWhere($column, $operator = null, $value = null) |
|
367 | + { |
|
368 | + return $this->where($column, $operator, $value, 'or'); |
|
369 | + } |
|
370 | + |
|
371 | + /** |
|
372 | + * Add a relationship count condition to the query. |
|
373 | + * |
|
374 | + * @param string $relation |
|
375 | + * @param string $operator |
|
376 | + * @param int $count |
|
377 | + * @param string $boolean |
|
378 | + * @param \Closure $callback |
|
379 | + * @return \Analogue\ORM\System\Query |
|
380 | + */ |
|
381 | + public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', $callback = null) |
|
382 | + { |
|
383 | + $entity = $this->mapper->newInstance(); |
|
384 | + |
|
385 | + $relation = $this->getHasRelationQuery($relation, $entity); |
|
386 | + |
|
387 | + $query = $relation->getRelationCountQuery($relation->getRelatedMapper()->getQuery(), $this); |
|
388 | + |
|
389 | + if ($callback) { |
|
390 | + call_user_func($callback, $query); |
|
391 | + } |
|
392 | + |
|
393 | + return $this->addHasWhere($query, $relation, $operator, $count, $boolean); |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * Add a relationship count condition to the query with where clauses. |
|
398 | + * |
|
399 | + * @param string $relation |
|
400 | + * @param \Closure $callback |
|
401 | + * @param string $operator |
|
402 | + * @param int $count |
|
403 | + * @return \Analogue\ORM\System\Query |
|
404 | + */ |
|
405 | + public function whereHas($relation, Closure $callback, $operator = '>=', $count = 1) |
|
406 | + { |
|
407 | + return $this->has($relation, $operator, $count, 'and', $callback); |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * Add a relationship count condition to the query with an "or". |
|
412 | + * |
|
413 | + * @param string $relation |
|
414 | + * @param string $operator |
|
415 | + * @param int $count |
|
416 | + * @return \Analogue\ORM\System\Query |
|
417 | + */ |
|
418 | + public function orHas($relation, $operator = '>=', $count = 1) |
|
419 | + { |
|
420 | + return $this->has($relation, $operator, $count, 'or'); |
|
421 | + } |
|
422 | + |
|
423 | + /** |
|
424 | + * Add a relationship count condition to the query with where clauses and an "or". |
|
425 | + * |
|
426 | + * @param string $relation |
|
427 | + * @param \Closure $callback |
|
428 | + * @param string $operator |
|
429 | + * @param int $count |
|
430 | + * @return \Analogue\ORM\System\Query |
|
431 | + */ |
|
432 | + public function orWhereHas($relation, Closure $callback, $operator = '>=', $count = 1) |
|
433 | + { |
|
434 | + return $this->has($relation, $operator, $count, 'or', $callback); |
|
435 | + } |
|
436 | + |
|
437 | + /** |
|
438 | + * Add the "has" condition where clause to the query. |
|
439 | + * |
|
440 | + * @param \Analogue\ORM\System\Query $hasQuery |
|
441 | + * @param \Analogue\ORM\Relationships\Relationship $relation |
|
442 | + * @param string $operator |
|
443 | + * @param int $count |
|
444 | + * @param string $boolean |
|
445 | + * @return \Analogue\ORM\System\Query |
|
446 | + */ |
|
447 | + protected function addHasWhere(Query $hasQuery, Relationship $relation, $operator, $count, $boolean) |
|
448 | + { |
|
449 | + $this->mergeWheresToHas($hasQuery, $relation); |
|
450 | + |
|
451 | + if (is_numeric($count)) { |
|
452 | + $count = new Expression($count); |
|
453 | + } |
|
454 | + |
|
455 | + return $this->where(new Expression('('.$hasQuery->toSql().')'), $operator, $count, $boolean); |
|
456 | + } |
|
457 | + |
|
458 | + /** |
|
459 | + * Merge the "wheres" from a relation query to a has query. |
|
460 | + * |
|
461 | + * @param \Analogue\ORM\System\Query $hasQuery |
|
462 | + * @param \Analogue\ORM\Relationships\Relationship $relation |
|
463 | + * @return void |
|
464 | + */ |
|
465 | + protected function mergeWheresToHas(Query $hasQuery, Relationship $relation) |
|
466 | + { |
|
467 | + // Here we have the "has" query and the original relation. We need to copy over any |
|
468 | + // where clauses the developer may have put in the relationship function over to |
|
469 | + // the has query, and then copy the bindings from the "has" query to the main. |
|
470 | + $relationQuery = $relation->getBaseQuery(); |
|
471 | + |
|
472 | + $hasQuery->mergeWheres( |
|
473 | + $relationQuery->wheres, $relationQuery->getBindings() |
|
474 | + ); |
|
475 | + |
|
476 | + $this->query->mergeBindings($hasQuery->getQuery()); |
|
477 | + } |
|
478 | + |
|
479 | + /** |
|
480 | + * Get the "has relation" base query instance. |
|
481 | + * |
|
482 | + * @param string $relation |
|
483 | + * @return \Analogue\ORM\System\Query |
|
484 | + */ |
|
485 | + protected function getHasRelationQuery($relation, $entity) |
|
486 | + { |
|
487 | + return Relationship::noConstraints(function () use ($relation, $entity) { |
|
488 | + return $this->entityMap->$relation($entity); |
|
489 | + }); |
|
490 | + } |
|
491 | + |
|
492 | + /** |
|
493 | + * Get the table for the current query object |
|
494 | + * |
|
495 | + * @return string |
|
496 | + */ |
|
497 | + public function getTable() |
|
498 | + { |
|
499 | + return $this->entityMap->getTable(); |
|
500 | + } |
|
501 | + |
|
502 | + /** |
|
503 | + * Set the relationships that should be eager loaded. |
|
504 | + * |
|
505 | + * @param mixed $relations |
|
506 | + * @return $this |
|
507 | + */ |
|
508 | + public function with($relations) |
|
509 | + { |
|
510 | + if (is_string($relations)) { |
|
511 | + $relations = func_get_args(); |
|
512 | + } |
|
513 | + |
|
514 | + $eagers = $this->parseRelations($relations); |
|
515 | + |
|
516 | + $this->eagerLoad = array_merge($this->eagerLoad, $eagers); |
|
517 | + |
|
518 | + return $this; |
|
519 | + } |
|
520 | + |
|
521 | + /** |
|
522 | + * Parse a list of relations into individuals. |
|
523 | + * |
|
524 | + * @param array $relations |
|
525 | + * @return array |
|
526 | + */ |
|
527 | + protected function parseRelations(array $relations) |
|
528 | + { |
|
529 | + $results = array(); |
|
530 | + |
|
531 | + foreach ($relations as $name => $constraints) { |
|
532 | + // If the "relation" value is actually a numeric key, we can assume that no |
|
533 | + // constraints have been specified for the eager load and we'll just put |
|
534 | + // an empty Closure with the loader so that we can treat all the same. |
|
535 | + if (is_numeric($name)) { |
|
536 | + $f = function () {}; |
|
537 | + |
|
538 | + list($name, $constraints) = array($constraints, $f); |
|
539 | + } |
|
540 | + |
|
541 | + // We need to separate out any nested includes. Which allows the developers |
|
542 | + // to load deep relationships using "dots" without stating each level of |
|
543 | + // the relationship with its own key in the array of eager load names. |
|
544 | + $results = $this->parseNested($name, $results); |
|
545 | + |
|
546 | + $results[$name] = $constraints; |
|
547 | + } |
|
548 | + |
|
549 | + return $results; |
|
550 | + } |
|
551 | + |
|
552 | + |
|
553 | + /** |
|
554 | + * Parse the nested relationships in a relation. |
|
555 | + * |
|
556 | + * @param string $name |
|
557 | + * @param array $results |
|
558 | + * @return array |
|
559 | + */ |
|
560 | + protected function parseNested($name, $results) |
|
561 | + { |
|
562 | + $progress = array(); |
|
563 | + |
|
564 | + // If the relation has already been set on the result array, we will not set it |
|
565 | + // again, since that would override any constraints that were already placed |
|
566 | + // on the relationships. We will only set the ones that are not specified. |
|
567 | + foreach (explode('.', $name) as $segment) { |
|
568 | + $progress[] = $segment; |
|
569 | + |
|
570 | + if (! isset($results[$last = implode('.', $progress)])) { |
|
571 | + $results[$last] = function () {}; |
|
572 | + } |
|
573 | + } |
|
574 | + |
|
575 | + return $results; |
|
576 | + } |
|
577 | + |
|
578 | + /** |
|
579 | + * Get the relationships being eagerly loaded. |
|
580 | + * |
|
581 | + * @return array |
|
582 | + */ |
|
583 | + public function getEagerLoads() |
|
584 | + { |
|
585 | + return $this->eagerLoad; |
|
586 | + } |
|
587 | + |
|
588 | + /** |
|
589 | + * Set the relationships being eagerly loaded. |
|
590 | + * |
|
591 | + * @param array $eagerLoad |
|
592 | + * @return void |
|
593 | + */ |
|
594 | + public function setEagerLoads(array $eagerLoad) |
|
595 | + { |
|
596 | + $this->eagerLoad = $eagerLoad; |
|
597 | + } |
|
598 | + |
|
599 | + /** |
|
600 | + * Eager load the relationships for the entities. |
|
601 | + * |
|
602 | + * @param array $entities |
|
603 | + * @return array |
|
604 | + */ |
|
605 | + public function eagerLoadRelations($entities) |
|
606 | + { |
|
607 | + foreach ($this->eagerLoad as $name => $constraints) { |
|
608 | + // For nested eager loads we'll skip loading them here and they will be set as an |
|
609 | + // eager load on the query to retrieve the relation so that they will be eager |
|
610 | + // loaded on that query, because that is where they get hydrated as models. |
|
611 | + if (strpos($name, '.') === false) { |
|
612 | + $entities = $this->loadRelation($entities, $name, $constraints); |
|
613 | + } |
|
614 | + } |
|
615 | + |
|
616 | + return $entities; |
|
617 | + } |
|
618 | + |
|
619 | + /** |
|
620 | + * Eagerly load the relationship on a set of entities. |
|
621 | + * |
|
622 | + * @param array $entities |
|
623 | + * @param string $name |
|
624 | + * @param \Closure $constraints |
|
625 | + * @return array |
|
626 | + */ |
|
627 | + protected function loadRelation(array $entities, $name, Closure $constraints) |
|
628 | + { |
|
629 | + // First we will "back up" the existing where conditions on the query so we can |
|
630 | + // add our eager constraints. Then we will merge the wheres that were on the |
|
631 | + // query back to it in order that any where conditions might be specified. |
|
632 | + $relation = $this->getRelation($name); |
|
633 | + |
|
634 | + $relation->addEagerConstraints($entities); |
|
635 | + |
|
636 | + call_user_func($constraints, $relation); |
|
637 | + |
|
638 | + $entities = $relation->initRelation($entities, $name); |
|
639 | + |
|
640 | + // Once we have the results, we just match those back up to their parent models |
|
641 | + // using the relationship instance. Then we just return the finished arrays |
|
642 | + // of models which have been eagerly hydrated and are readied for return. |
|
643 | 643 | |
644 | - $results = $relation->getEager(); |
|
645 | - |
|
646 | - return $relation->match($entities, $results, $name); |
|
647 | - } |
|
648 | - |
|
649 | - /** |
|
650 | - * Get the relation instance for the given relation name. |
|
651 | - * |
|
652 | - * @param string $relation |
|
653 | - * @return \Analogue\ORM\Relationships\Relationship |
|
654 | - */ |
|
655 | - public function getRelation($relation) |
|
656 | - { |
|
657 | - // We want to run a relationship query without any constrains so that we will |
|
658 | - // not have to remove these where clauses manually which gets really hacky |
|
659 | - // and is error prone while we remove the developer's own where clauses. |
|
660 | - $query = Relationship::noConstraints(function () use ($relation) { |
|
661 | - return $this->entityMap->$relation($this->getEntityInstance()); |
|
662 | - }); |
|
663 | - |
|
664 | - $nested = $this->nestedRelations($relation); |
|
665 | - |
|
666 | - // If there are nested relationships set on the query, we will put those onto |
|
667 | - // the query instances so that they can be handled after this relationship |
|
668 | - // is loaded. In this way they will all trickle down as they are loaded. |
|
669 | - if (count($nested) > 0) { |
|
670 | - $query->getQuery()->with($nested); |
|
671 | - } |
|
672 | - |
|
673 | - return $query; |
|
674 | - } |
|
675 | - |
|
676 | - /** |
|
677 | - * Get the deeply nested relations for a given top-level relation. |
|
678 | - * |
|
679 | - * @param string $relation |
|
680 | - * @return array |
|
681 | - */ |
|
682 | - protected function nestedRelations($relation) |
|
683 | - { |
|
684 | - $nested = array(); |
|
685 | - |
|
686 | - // We are basically looking for any relationships that are nested deeper than |
|
687 | - // the given top-level relationship. We will just check for any relations |
|
688 | - // that start with the given top relations and adds them to our arrays. |
|
689 | - foreach ($this->eagerLoad as $name => $constraints) { |
|
690 | - if ($this->isNested($name, $relation)) { |
|
691 | - $nested[substr($name, strlen($relation.'.'))] = $constraints; |
|
692 | - } |
|
693 | - } |
|
694 | - |
|
695 | - return $nested; |
|
696 | - } |
|
697 | - |
|
698 | - /** |
|
699 | - * Determine if the relationship is nested. |
|
700 | - * |
|
701 | - * @param string $name |
|
702 | - * @param string $relation |
|
703 | - * @return bool |
|
704 | - */ |
|
705 | - protected function isNested($name, $relation) |
|
706 | - { |
|
707 | - $dots = str_contains($name, '.'); |
|
708 | - |
|
709 | - return $dots && starts_with($name, $relation.'.'); |
|
710 | - } |
|
711 | - |
|
712 | - /** |
|
713 | - * Add the Entity primary key if not in requested columns |
|
714 | - * |
|
715 | - * @param array $columns |
|
716 | - * @return array |
|
717 | - */ |
|
718 | - protected function enforceIdColumn($columns) |
|
719 | - { |
|
720 | - if (! in_array($this->entityMap->getKeyName(), $columns)) { |
|
721 | - $columns[] = $this->entityMap->getKeyName(); |
|
722 | - } |
|
723 | - return $columns; |
|
724 | - } |
|
725 | - |
|
726 | - /** |
|
727 | - * Get the hydrated models without eager loading. |
|
728 | - * |
|
729 | - * @param array $columns |
|
730 | - * @return Analogue\ORM\EntityCollection |
|
731 | - */ |
|
732 | - public function getEntities($columns = array('*')) |
|
733 | - { |
|
734 | - // As we need the primary key to feed the |
|
735 | - // entity cache, we need it loaded on each |
|
736 | - // request |
|
737 | - $columns = $this->enforceIdColumn($columns); |
|
738 | - |
|
739 | - // Run the query |
|
740 | - $results = $this->query->get($columns); |
|
741 | - |
|
742 | - $builder = new EntityBuilder($this->mapper, array_keys($this->getEagerLoads())); |
|
743 | - |
|
744 | - return $builder->build($results); |
|
745 | - } |
|
746 | - |
|
747 | - /** |
|
748 | - * Get a new instance for the entity |
|
749 | - * |
|
750 | - * @param array $attributes |
|
751 | - * @return Entity |
|
752 | - */ |
|
753 | - public function getEntityInstance(array $attributes = array()) |
|
754 | - { |
|
755 | - return $this->mapper->newInstance($attributes); |
|
756 | - } |
|
757 | - |
|
758 | - /** |
|
759 | - * Extend the builder with a given callback. |
|
760 | - * |
|
761 | - * @param string $name |
|
762 | - * @param \Closure $callback |
|
763 | - * @return void |
|
764 | - */ |
|
765 | - public function macro($name, Closure $callback) |
|
766 | - { |
|
767 | - $this->macros[$name] = $callback; |
|
768 | - } |
|
769 | - |
|
770 | - /** |
|
771 | - * Get the given macro by name. |
|
772 | - * |
|
773 | - * @param string $name |
|
774 | - * @return \Closure |
|
775 | - */ |
|
776 | - public function getMacro($name) |
|
777 | - { |
|
778 | - return array_get($this->macros, $name); |
|
779 | - } |
|
780 | - |
|
781 | - /** |
|
782 | - * Get a new query builder for the model's table. |
|
783 | - * |
|
784 | - * @return \Analogue\ORM\System\Query |
|
785 | - */ |
|
786 | - public function newQuery() |
|
787 | - { |
|
788 | - $builder = new Query($this->mapper, $this->adapter); |
|
789 | - |
|
790 | - return $this->applyGlobalScopes($builder); |
|
791 | - } |
|
792 | - |
|
793 | - /** |
|
794 | - * Get a new query builder without any scope applied. |
|
795 | - * |
|
796 | - * @return \Analogue\ORM\System\Query |
|
797 | - */ |
|
798 | - public function newQueryWithoutScopes() |
|
799 | - { |
|
800 | - return new Query($this->mapper, $this->adapter); |
|
801 | - } |
|
802 | - |
|
803 | - /** |
|
804 | - * Get the Mapper instance for this Query Builder |
|
805 | - * |
|
806 | - * @return \Analogue\ORM\System\Mapper |
|
807 | - */ |
|
808 | - public function getMapper() |
|
809 | - { |
|
810 | - return $this->mapper; |
|
811 | - } |
|
812 | - |
|
813 | - /** |
|
814 | - * Get the underlying query adapter |
|
815 | - * |
|
816 | - * (REFACTOR: this method should move out, we need to provide the client classes |
|
817 | - * with the adapter instead.) |
|
818 | - * |
|
819 | - * @return \Analogue\ORM\Drivers\QueryAdapter |
|
820 | - */ |
|
821 | - public function getQuery() |
|
822 | - { |
|
823 | - return $this->query; |
|
824 | - } |
|
825 | - |
|
826 | - /** |
|
827 | - * Dynamically handle calls into the query instance. |
|
828 | - * |
|
829 | - * @param string $method |
|
830 | - * @param array $parameters |
|
831 | - * @return mixed |
|
832 | - */ |
|
833 | - public function __call($method, $parameters) |
|
834 | - { |
|
835 | - if (isset($this->macros[$method])) { |
|
836 | - array_unshift($parameters, $this); |
|
837 | - |
|
838 | - return call_user_func_array($this->macros[$method], $parameters); |
|
839 | - } |
|
644 | + $results = $relation->getEager(); |
|
645 | + |
|
646 | + return $relation->match($entities, $results, $name); |
|
647 | + } |
|
648 | + |
|
649 | + /** |
|
650 | + * Get the relation instance for the given relation name. |
|
651 | + * |
|
652 | + * @param string $relation |
|
653 | + * @return \Analogue\ORM\Relationships\Relationship |
|
654 | + */ |
|
655 | + public function getRelation($relation) |
|
656 | + { |
|
657 | + // We want to run a relationship query without any constrains so that we will |
|
658 | + // not have to remove these where clauses manually which gets really hacky |
|
659 | + // and is error prone while we remove the developer's own where clauses. |
|
660 | + $query = Relationship::noConstraints(function () use ($relation) { |
|
661 | + return $this->entityMap->$relation($this->getEntityInstance()); |
|
662 | + }); |
|
663 | + |
|
664 | + $nested = $this->nestedRelations($relation); |
|
665 | + |
|
666 | + // If there are nested relationships set on the query, we will put those onto |
|
667 | + // the query instances so that they can be handled after this relationship |
|
668 | + // is loaded. In this way they will all trickle down as they are loaded. |
|
669 | + if (count($nested) > 0) { |
|
670 | + $query->getQuery()->with($nested); |
|
671 | + } |
|
672 | + |
|
673 | + return $query; |
|
674 | + } |
|
675 | + |
|
676 | + /** |
|
677 | + * Get the deeply nested relations for a given top-level relation. |
|
678 | + * |
|
679 | + * @param string $relation |
|
680 | + * @return array |
|
681 | + */ |
|
682 | + protected function nestedRelations($relation) |
|
683 | + { |
|
684 | + $nested = array(); |
|
685 | + |
|
686 | + // We are basically looking for any relationships that are nested deeper than |
|
687 | + // the given top-level relationship. We will just check for any relations |
|
688 | + // that start with the given top relations and adds them to our arrays. |
|
689 | + foreach ($this->eagerLoad as $name => $constraints) { |
|
690 | + if ($this->isNested($name, $relation)) { |
|
691 | + $nested[substr($name, strlen($relation.'.'))] = $constraints; |
|
692 | + } |
|
693 | + } |
|
694 | + |
|
695 | + return $nested; |
|
696 | + } |
|
697 | + |
|
698 | + /** |
|
699 | + * Determine if the relationship is nested. |
|
700 | + * |
|
701 | + * @param string $name |
|
702 | + * @param string $relation |
|
703 | + * @return bool |
|
704 | + */ |
|
705 | + protected function isNested($name, $relation) |
|
706 | + { |
|
707 | + $dots = str_contains($name, '.'); |
|
708 | + |
|
709 | + return $dots && starts_with($name, $relation.'.'); |
|
710 | + } |
|
711 | + |
|
712 | + /** |
|
713 | + * Add the Entity primary key if not in requested columns |
|
714 | + * |
|
715 | + * @param array $columns |
|
716 | + * @return array |
|
717 | + */ |
|
718 | + protected function enforceIdColumn($columns) |
|
719 | + { |
|
720 | + if (! in_array($this->entityMap->getKeyName(), $columns)) { |
|
721 | + $columns[] = $this->entityMap->getKeyName(); |
|
722 | + } |
|
723 | + return $columns; |
|
724 | + } |
|
725 | + |
|
726 | + /** |
|
727 | + * Get the hydrated models without eager loading. |
|
728 | + * |
|
729 | + * @param array $columns |
|
730 | + * @return Analogue\ORM\EntityCollection |
|
731 | + */ |
|
732 | + public function getEntities($columns = array('*')) |
|
733 | + { |
|
734 | + // As we need the primary key to feed the |
|
735 | + // entity cache, we need it loaded on each |
|
736 | + // request |
|
737 | + $columns = $this->enforceIdColumn($columns); |
|
738 | + |
|
739 | + // Run the query |
|
740 | + $results = $this->query->get($columns); |
|
741 | + |
|
742 | + $builder = new EntityBuilder($this->mapper, array_keys($this->getEagerLoads())); |
|
743 | + |
|
744 | + return $builder->build($results); |
|
745 | + } |
|
746 | + |
|
747 | + /** |
|
748 | + * Get a new instance for the entity |
|
749 | + * |
|
750 | + * @param array $attributes |
|
751 | + * @return Entity |
|
752 | + */ |
|
753 | + public function getEntityInstance(array $attributes = array()) |
|
754 | + { |
|
755 | + return $this->mapper->newInstance($attributes); |
|
756 | + } |
|
757 | + |
|
758 | + /** |
|
759 | + * Extend the builder with a given callback. |
|
760 | + * |
|
761 | + * @param string $name |
|
762 | + * @param \Closure $callback |
|
763 | + * @return void |
|
764 | + */ |
|
765 | + public function macro($name, Closure $callback) |
|
766 | + { |
|
767 | + $this->macros[$name] = $callback; |
|
768 | + } |
|
769 | + |
|
770 | + /** |
|
771 | + * Get the given macro by name. |
|
772 | + * |
|
773 | + * @param string $name |
|
774 | + * @return \Closure |
|
775 | + */ |
|
776 | + public function getMacro($name) |
|
777 | + { |
|
778 | + return array_get($this->macros, $name); |
|
779 | + } |
|
780 | + |
|
781 | + /** |
|
782 | + * Get a new query builder for the model's table. |
|
783 | + * |
|
784 | + * @return \Analogue\ORM\System\Query |
|
785 | + */ |
|
786 | + public function newQuery() |
|
787 | + { |
|
788 | + $builder = new Query($this->mapper, $this->adapter); |
|
789 | + |
|
790 | + return $this->applyGlobalScopes($builder); |
|
791 | + } |
|
792 | + |
|
793 | + /** |
|
794 | + * Get a new query builder without any scope applied. |
|
795 | + * |
|
796 | + * @return \Analogue\ORM\System\Query |
|
797 | + */ |
|
798 | + public function newQueryWithoutScopes() |
|
799 | + { |
|
800 | + return new Query($this->mapper, $this->adapter); |
|
801 | + } |
|
802 | + |
|
803 | + /** |
|
804 | + * Get the Mapper instance for this Query Builder |
|
805 | + * |
|
806 | + * @return \Analogue\ORM\System\Mapper |
|
807 | + */ |
|
808 | + public function getMapper() |
|
809 | + { |
|
810 | + return $this->mapper; |
|
811 | + } |
|
812 | + |
|
813 | + /** |
|
814 | + * Get the underlying query adapter |
|
815 | + * |
|
816 | + * (REFACTOR: this method should move out, we need to provide the client classes |
|
817 | + * with the adapter instead.) |
|
818 | + * |
|
819 | + * @return \Analogue\ORM\Drivers\QueryAdapter |
|
820 | + */ |
|
821 | + public function getQuery() |
|
822 | + { |
|
823 | + return $this->query; |
|
824 | + } |
|
825 | + |
|
826 | + /** |
|
827 | + * Dynamically handle calls into the query instance. |
|
828 | + * |
|
829 | + * @param string $method |
|
830 | + * @param array $parameters |
|
831 | + * @return mixed |
|
832 | + */ |
|
833 | + public function __call($method, $parameters) |
|
834 | + { |
|
835 | + if (isset($this->macros[$method])) { |
|
836 | + array_unshift($parameters, $this); |
|
837 | + |
|
838 | + return call_user_func_array($this->macros[$method], $parameters); |
|
839 | + } |
|
840 | 840 | |
841 | - if (in_array($method, $this->blacklist)) { |
|
842 | - throw new Exception("Method $method doesn't exist"); |
|
843 | - } |
|
841 | + if (in_array($method, $this->blacklist)) { |
|
842 | + throw new Exception("Method $method doesn't exist"); |
|
843 | + } |
|
844 | 844 | |
845 | - $result = call_user_func_array(array($this->query, $method), $parameters); |
|
845 | + $result = call_user_func_array(array($this->query, $method), $parameters); |
|
846 | 846 | |
847 | - return in_array($method, $this->passthru) ? $result : $this; |
|
848 | - } |
|
847 | + return in_array($method, $this->passthru) ? $result : $this; |
|
848 | + } |
|
849 | 849 | } |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | */ |
168 | 168 | public function findOrFail($id, $columns = array('*')) |
169 | 169 | { |
170 | - if (! is_null($entity = $this->find($id, $columns))) { |
|
170 | + if (!is_null($entity = $this->find($id, $columns))) { |
|
171 | 171 | return $entity; |
172 | 172 | } |
173 | 173 | |
@@ -196,7 +196,7 @@ discard block |
||
196 | 196 | */ |
197 | 197 | public function firstOrFail($columns = array('*')) |
198 | 198 | { |
199 | - if (! is_null($entity = $this->first($columns))) { |
|
199 | + if (!is_null($entity = $this->first($columns))) { |
|
200 | 200 | return $entity; |
201 | 201 | } |
202 | 202 | |
@@ -484,7 +484,7 @@ discard block |
||
484 | 484 | */ |
485 | 485 | protected function getHasRelationQuery($relation, $entity) |
486 | 486 | { |
487 | - return Relationship::noConstraints(function () use ($relation, $entity) { |
|
487 | + return Relationship::noConstraints(function() use ($relation, $entity) { |
|
488 | 488 | return $this->entityMap->$relation($entity); |
489 | 489 | }); |
490 | 490 | } |
@@ -533,7 +533,7 @@ discard block |
||
533 | 533 | // constraints have been specified for the eager load and we'll just put |
534 | 534 | // an empty Closure with the loader so that we can treat all the same. |
535 | 535 | if (is_numeric($name)) { |
536 | - $f = function () {}; |
|
536 | + $f = function() {}; |
|
537 | 537 | |
538 | 538 | list($name, $constraints) = array($constraints, $f); |
539 | 539 | } |
@@ -567,8 +567,8 @@ discard block |
||
567 | 567 | foreach (explode('.', $name) as $segment) { |
568 | 568 | $progress[] = $segment; |
569 | 569 | |
570 | - if (! isset($results[$last = implode('.', $progress)])) { |
|
571 | - $results[$last] = function () {}; |
|
570 | + if (!isset($results[$last = implode('.', $progress)])) { |
|
571 | + $results[$last] = function() {}; |
|
572 | 572 | } |
573 | 573 | } |
574 | 574 | |
@@ -657,7 +657,7 @@ discard block |
||
657 | 657 | // We want to run a relationship query without any constrains so that we will |
658 | 658 | // not have to remove these where clauses manually which gets really hacky |
659 | 659 | // and is error prone while we remove the developer's own where clauses. |
660 | - $query = Relationship::noConstraints(function () use ($relation) { |
|
660 | + $query = Relationship::noConstraints(function() use ($relation) { |
|
661 | 661 | return $this->entityMap->$relation($this->getEntityInstance()); |
662 | 662 | }); |
663 | 663 | |
@@ -717,7 +717,7 @@ discard block |
||
717 | 717 | */ |
718 | 718 | protected function enforceIdColumn($columns) |
719 | 719 | { |
720 | - if (! in_array($this->entityMap->getKeyName(), $columns)) { |
|
720 | + if (!in_array($this->entityMap->getKeyName(), $columns)) { |
|
721 | 721 | $columns[] = $this->entityMap->getKeyName(); |
722 | 722 | } |
723 | 723 | return $columns; |
@@ -47,8 +47,6 @@ discard block |
||
47 | 47 | /** |
48 | 48 | * Extract Attributes from a Plain Php Object |
49 | 49 | * |
50 | - * @param mixed $entity |
|
51 | - * @param array $attributeList |
|
52 | 50 | * @return array $attributes |
53 | 51 | */ |
54 | 52 | protected function extract() |
@@ -74,8 +72,6 @@ discard block |
||
74 | 72 | |
75 | 73 | /** |
76 | 74 | * [getMappedProperties description] |
77 | - * @param [type] $entity [description] |
|
78 | - * @param array $attributeList [description] |
|
79 | 75 | * @return [type] [description] |
80 | 76 | */ |
81 | 77 | protected function getMappedProperties() |
@@ -93,6 +89,9 @@ discard block |
||
93 | 89 | }); |
94 | 90 | } |
95 | 91 | |
92 | + /** |
|
93 | + * @param string $name |
|
94 | + */ |
|
96 | 95 | protected function getMappedProperty($name) |
97 | 96 | { |
98 | 97 | return $this->reflection->getProperty($name); |
@@ -1,7 +1,6 @@ |
||
1 | 1 | <?php namespace Analogue\ORM\System\Wrappers; |
2 | 2 | |
3 | 3 | use ReflectionClass; |
4 | -use Analogue\ORM\EntityMap; |
|
5 | 4 | |
6 | 5 | class PlainObjectWrapper extends Wrapper |
7 | 6 | { |
@@ -6,203 +6,203 @@ |
||
6 | 6 | class PlainObjectWrapper extends Wrapper |
7 | 7 | { |
8 | 8 | |
9 | - /** |
|
10 | - * The list of attributes for the managed entity |
|
11 | - * |
|
12 | - * @var array |
|
13 | - */ |
|
14 | - protected $attributeList; |
|
15 | - |
|
16 | - /** |
|
17 | - * The reflection class for the managed entity |
|
18 | - * |
|
19 | - * @var \ReflectionClass |
|
20 | - */ |
|
21 | - protected $reflection; |
|
22 | - |
|
23 | - /** |
|
24 | - * |
|
25 | - * @param [type] $popoEntity [description] |
|
26 | - * @param [type] $entityMap [description] |
|
27 | - */ |
|
28 | - public function __construct($popoEntity, $entityMap) |
|
29 | - { |
|
30 | - $this->reflection = new ReflectionClass($popoEntity); |
|
31 | - |
|
32 | - $this->attributeList = $this->getAttributeList(); |
|
33 | - |
|
34 | - parent::__construct($popoEntity, $entityMap); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Get Compiled Attributes (key, attributes, embed, relations) |
|
39 | - * |
|
40 | - * @return array |
|
41 | - */ |
|
42 | - protected function getAttributeList() |
|
43 | - { |
|
44 | - return $this->entityMap->getCompiledAttributes(); |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Extract Attributes from a Plain Php Object |
|
49 | - * |
|
50 | - * @param mixed $entity |
|
51 | - * @param array $attributeList |
|
52 | - * @return array $attributes |
|
53 | - */ |
|
54 | - protected function extract() |
|
55 | - { |
|
56 | - $properties = $this->getMappedProperties(); |
|
57 | - |
|
58 | - $attributes = []; |
|
59 | - |
|
60 | - foreach ($properties as $property) { |
|
61 | - $name = $property->getName(); |
|
62 | - |
|
63 | - if ($property->isPublic()) { |
|
64 | - $attributes[$name] = $this->entity->$name; |
|
65 | - } else { |
|
66 | - $property->setAccessible(true); |
|
9 | + /** |
|
10 | + * The list of attributes for the managed entity |
|
11 | + * |
|
12 | + * @var array |
|
13 | + */ |
|
14 | + protected $attributeList; |
|
15 | + |
|
16 | + /** |
|
17 | + * The reflection class for the managed entity |
|
18 | + * |
|
19 | + * @var \ReflectionClass |
|
20 | + */ |
|
21 | + protected $reflection; |
|
22 | + |
|
23 | + /** |
|
24 | + * |
|
25 | + * @param [type] $popoEntity [description] |
|
26 | + * @param [type] $entityMap [description] |
|
27 | + */ |
|
28 | + public function __construct($popoEntity, $entityMap) |
|
29 | + { |
|
30 | + $this->reflection = new ReflectionClass($popoEntity); |
|
31 | + |
|
32 | + $this->attributeList = $this->getAttributeList(); |
|
33 | + |
|
34 | + parent::__construct($popoEntity, $entityMap); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Get Compiled Attributes (key, attributes, embed, relations) |
|
39 | + * |
|
40 | + * @return array |
|
41 | + */ |
|
42 | + protected function getAttributeList() |
|
43 | + { |
|
44 | + return $this->entityMap->getCompiledAttributes(); |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Extract Attributes from a Plain Php Object |
|
49 | + * |
|
50 | + * @param mixed $entity |
|
51 | + * @param array $attributeList |
|
52 | + * @return array $attributes |
|
53 | + */ |
|
54 | + protected function extract() |
|
55 | + { |
|
56 | + $properties = $this->getMappedProperties(); |
|
57 | + |
|
58 | + $attributes = []; |
|
59 | + |
|
60 | + foreach ($properties as $property) { |
|
61 | + $name = $property->getName(); |
|
62 | + |
|
63 | + if ($property->isPublic()) { |
|
64 | + $attributes[$name] = $this->entity->$name; |
|
65 | + } else { |
|
66 | + $property->setAccessible(true); |
|
67 | 67 | |
68 | - $attributes[$name] = $property->getValue($this->entity); |
|
69 | - } |
|
70 | - } |
|
71 | - |
|
72 | - return $attributes; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * [getMappedProperties description] |
|
77 | - * @param [type] $entity [description] |
|
78 | - * @param array $attributeList [description] |
|
79 | - * @return [type] [description] |
|
80 | - */ |
|
81 | - protected function getMappedProperties() |
|
82 | - { |
|
83 | - $objectProperties = $this->reflection->getProperties(); |
|
84 | - |
|
85 | - $attributeList = $this->getAttributeList(); |
|
86 | - |
|
87 | - // We need to filter out properties that could belong to the object |
|
88 | - // and which are not intended to be handled by the ORM |
|
89 | - return array_filter($objectProperties, function ($item) use ($attributeList) { |
|
90 | - if (in_array($item->getName(), $attributeList)) { |
|
91 | - return true; |
|
92 | - } |
|
93 | - }); |
|
94 | - } |
|
95 | - |
|
96 | - protected function getMappedProperty($name) |
|
97 | - { |
|
98 | - return $this->reflection->getProperty($name); |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * Hydrate Plain PHP Object with wrapped attributes |
|
103 | - * |
|
104 | - * @return void |
|
105 | - */ |
|
106 | - protected function hydrate($attributes) |
|
107 | - { |
|
108 | - $properties = $this->getMappedProperties(); |
|
109 | - |
|
110 | - foreach ($properties as $property) { |
|
111 | - $name = $property->getName(); |
|
112 | - |
|
113 | - if ($property->isPublic()) { |
|
114 | - $this->entity->$name = $attributes[$name]; |
|
115 | - } else { |
|
116 | - $property->setAccessible(true); |
|
68 | + $attributes[$name] = $property->getValue($this->entity); |
|
69 | + } |
|
70 | + } |
|
71 | + |
|
72 | + return $attributes; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * [getMappedProperties description] |
|
77 | + * @param [type] $entity [description] |
|
78 | + * @param array $attributeList [description] |
|
79 | + * @return [type] [description] |
|
80 | + */ |
|
81 | + protected function getMappedProperties() |
|
82 | + { |
|
83 | + $objectProperties = $this->reflection->getProperties(); |
|
84 | + |
|
85 | + $attributeList = $this->getAttributeList(); |
|
86 | + |
|
87 | + // We need to filter out properties that could belong to the object |
|
88 | + // and which are not intended to be handled by the ORM |
|
89 | + return array_filter($objectProperties, function ($item) use ($attributeList) { |
|
90 | + if (in_array($item->getName(), $attributeList)) { |
|
91 | + return true; |
|
92 | + } |
|
93 | + }); |
|
94 | + } |
|
95 | + |
|
96 | + protected function getMappedProperty($name) |
|
97 | + { |
|
98 | + return $this->reflection->getProperty($name); |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * Hydrate Plain PHP Object with wrapped attributes |
|
103 | + * |
|
104 | + * @return void |
|
105 | + */ |
|
106 | + protected function hydrate($attributes) |
|
107 | + { |
|
108 | + $properties = $this->getMappedProperties(); |
|
109 | + |
|
110 | + foreach ($properties as $property) { |
|
111 | + $name = $property->getName(); |
|
112 | + |
|
113 | + if ($property->isPublic()) { |
|
114 | + $this->entity->$name = $attributes[$name]; |
|
115 | + } else { |
|
116 | + $property->setAccessible(true); |
|
117 | 117 | |
118 | - $property->setValue($this->entity, $attributes[$name]); |
|
119 | - } |
|
120 | - } |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Method used by the mapper to set the object |
|
125 | - * attribute raw values (hydration) |
|
126 | - * |
|
127 | - * @param array $attributes |
|
128 | - * |
|
129 | - * @return void |
|
130 | - */ |
|
131 | - public function setEntityAttributes(array $attributes) |
|
132 | - { |
|
133 | - $this->hydrate($attributes); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Method used by the mapper to get the |
|
138 | - * raw object's values. |
|
139 | - * |
|
140 | - * @return array |
|
141 | - */ |
|
142 | - public function getEntityAttributes() |
|
143 | - { |
|
144 | - return $this->extract(); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Method used by the mapper to set raw |
|
149 | - * key-value pair |
|
150 | - * |
|
151 | - * @param string $key |
|
152 | - * @param string $value |
|
153 | - * |
|
154 | - * @return void |
|
155 | - */ |
|
156 | - public function setEntityAttribute($key, $value) |
|
157 | - { |
|
158 | - $property = $this->getMappedProperty($key); |
|
159 | - |
|
160 | - if ($property->isPublic()) { |
|
161 | - $this->entity->$key = $value; |
|
162 | - } else { |
|
163 | - $property->setAccessible(true); |
|
118 | + $property->setValue($this->entity, $attributes[$name]); |
|
119 | + } |
|
120 | + } |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Method used by the mapper to set the object |
|
125 | + * attribute raw values (hydration) |
|
126 | + * |
|
127 | + * @param array $attributes |
|
128 | + * |
|
129 | + * @return void |
|
130 | + */ |
|
131 | + public function setEntityAttributes(array $attributes) |
|
132 | + { |
|
133 | + $this->hydrate($attributes); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Method used by the mapper to get the |
|
138 | + * raw object's values. |
|
139 | + * |
|
140 | + * @return array |
|
141 | + */ |
|
142 | + public function getEntityAttributes() |
|
143 | + { |
|
144 | + return $this->extract(); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Method used by the mapper to set raw |
|
149 | + * key-value pair |
|
150 | + * |
|
151 | + * @param string $key |
|
152 | + * @param string $value |
|
153 | + * |
|
154 | + * @return void |
|
155 | + */ |
|
156 | + public function setEntityAttribute($key, $value) |
|
157 | + { |
|
158 | + $property = $this->getMappedProperty($key); |
|
159 | + |
|
160 | + if ($property->isPublic()) { |
|
161 | + $this->entity->$key = $value; |
|
162 | + } else { |
|
163 | + $property->setAccessible(true); |
|
164 | 164 | |
165 | - $property->setValue($this->entity, $value); |
|
166 | - } |
|
167 | - |
|
168 | - $this->attributes[$key] = $value; |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Method used by the mapper to get single |
|
173 | - * key-value pair |
|
174 | - * |
|
175 | - * @param string $key |
|
176 | - * @return mixed |
|
177 | - */ |
|
178 | - public function getEntityAttribute($key) |
|
179 | - { |
|
180 | - $property = $this->getMappedProperty($key); |
|
181 | - |
|
182 | - if ($property->isPublic()) { |
|
183 | - $value = $this->entity->$key; |
|
184 | - } else { |
|
185 | - $property->setAccessible(true); |
|
186 | - $value = $property->getValue($this->entity); |
|
187 | - } |
|
188 | - |
|
189 | - return $value; |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Test if a given attribute exists |
|
194 | - * |
|
195 | - * @param string $key |
|
196 | - * @return boolean |
|
197 | - */ |
|
198 | - public function hasAttribute($key) |
|
199 | - { |
|
200 | - $attributes = $this->entity->getEntityAttributes(); |
|
201 | - |
|
202 | - if (array_key_exists($key, $$this->attributeList)) { |
|
203 | - return true; |
|
204 | - } else { |
|
205 | - return false; |
|
206 | - } |
|
207 | - } |
|
165 | + $property->setValue($this->entity, $value); |
|
166 | + } |
|
167 | + |
|
168 | + $this->attributes[$key] = $value; |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Method used by the mapper to get single |
|
173 | + * key-value pair |
|
174 | + * |
|
175 | + * @param string $key |
|
176 | + * @return mixed |
|
177 | + */ |
|
178 | + public function getEntityAttribute($key) |
|
179 | + { |
|
180 | + $property = $this->getMappedProperty($key); |
|
181 | + |
|
182 | + if ($property->isPublic()) { |
|
183 | + $value = $this->entity->$key; |
|
184 | + } else { |
|
185 | + $property->setAccessible(true); |
|
186 | + $value = $property->getValue($this->entity); |
|
187 | + } |
|
188 | + |
|
189 | + return $value; |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Test if a given attribute exists |
|
194 | + * |
|
195 | + * @param string $key |
|
196 | + * @return boolean |
|
197 | + */ |
|
198 | + public function hasAttribute($key) |
|
199 | + { |
|
200 | + $attributes = $this->entity->getEntityAttributes(); |
|
201 | + |
|
202 | + if (array_key_exists($key, $$this->attributeList)) { |
|
203 | + return true; |
|
204 | + } else { |
|
205 | + return false; |
|
206 | + } |
|
207 | + } |
|
208 | 208 | } |
@@ -86,7 +86,7 @@ |
||
86 | 86 | |
87 | 87 | // We need to filter out properties that could belong to the object |
88 | 88 | // and which are not intended to be handled by the ORM |
89 | - return array_filter($objectProperties, function ($item) use ($attributeList) { |
|
89 | + return array_filter($objectProperties, function($item) use ($attributeList) { |
|
90 | 90 | if (in_array($item->getName(), $attributeList)) { |
91 | 91 | return true; |
92 | 92 | } |
@@ -9,158 +9,158 @@ |
||
9 | 9 | |
10 | 10 | class ValueObject implements Mappable, ArrayAccess, Jsonable, JsonSerializable, Arrayable |
11 | 11 | { |
12 | - use MappableTrait; |
|
12 | + use MappableTrait; |
|
13 | 13 | |
14 | - /** |
|
15 | - * Dynamically retrieve attributes on the entity. |
|
16 | - * |
|
17 | - * @param string $key |
|
18 | - * @return mixed |
|
19 | - */ |
|
20 | - public function __get($key) |
|
21 | - { |
|
22 | - return $this->attributes[$key]; |
|
23 | - } |
|
14 | + /** |
|
15 | + * Dynamically retrieve attributes on the entity. |
|
16 | + * |
|
17 | + * @param string $key |
|
18 | + * @return mixed |
|
19 | + */ |
|
20 | + public function __get($key) |
|
21 | + { |
|
22 | + return $this->attributes[$key]; |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * Dynamically set attributes on the entity. |
|
27 | - * |
|
28 | - * @param string $key |
|
29 | - * @param mixed $value |
|
30 | - * @return void |
|
31 | - */ |
|
32 | - public function __set($key, $value) |
|
33 | - { |
|
34 | - $this->attributes[$key] = $value; |
|
35 | - } |
|
25 | + /** |
|
26 | + * Dynamically set attributes on the entity. |
|
27 | + * |
|
28 | + * @param string $key |
|
29 | + * @param mixed $value |
|
30 | + * @return void |
|
31 | + */ |
|
32 | + public function __set($key, $value) |
|
33 | + { |
|
34 | + $this->attributes[$key] = $value; |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * Determine if an attribute exists on the entity. |
|
39 | - * |
|
40 | - * @param string $key |
|
41 | - * @return bool |
|
42 | - */ |
|
43 | - public function __isset($key) |
|
44 | - { |
|
45 | - return array_key_exists($key, $this->attributes); |
|
46 | - } |
|
37 | + /** |
|
38 | + * Determine if an attribute exists on the entity. |
|
39 | + * |
|
40 | + * @param string $key |
|
41 | + * @return bool |
|
42 | + */ |
|
43 | + public function __isset($key) |
|
44 | + { |
|
45 | + return array_key_exists($key, $this->attributes); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Unset an attribute on the entity. |
|
50 | - * |
|
51 | - * @param string $key |
|
52 | - * @return void |
|
53 | - */ |
|
54 | - public function __unset($key) |
|
55 | - { |
|
56 | - unset($this->attributes[$key]); |
|
57 | - } |
|
48 | + /** |
|
49 | + * Unset an attribute on the entity. |
|
50 | + * |
|
51 | + * @param string $key |
|
52 | + * @return void |
|
53 | + */ |
|
54 | + public function __unset($key) |
|
55 | + { |
|
56 | + unset($this->attributes[$key]); |
|
57 | + } |
|
58 | 58 | |
59 | 59 | |
60 | - /** |
|
61 | - * Determine if the given attribute exists. |
|
62 | - * |
|
63 | - * @param mixed $offset |
|
64 | - * @return bool |
|
65 | - */ |
|
66 | - public function offsetExists($offset) |
|
67 | - { |
|
68 | - return isset($this->$offset); |
|
69 | - } |
|
60 | + /** |
|
61 | + * Determine if the given attribute exists. |
|
62 | + * |
|
63 | + * @param mixed $offset |
|
64 | + * @return bool |
|
65 | + */ |
|
66 | + public function offsetExists($offset) |
|
67 | + { |
|
68 | + return isset($this->$offset); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * Get the value for a given offset. |
|
73 | - * |
|
74 | - * @param mixed $offset |
|
75 | - * @return mixed |
|
76 | - */ |
|
77 | - public function offsetGet($offset) |
|
78 | - { |
|
79 | - return $this->$offset; |
|
80 | - } |
|
71 | + /** |
|
72 | + * Get the value for a given offset. |
|
73 | + * |
|
74 | + * @param mixed $offset |
|
75 | + * @return mixed |
|
76 | + */ |
|
77 | + public function offsetGet($offset) |
|
78 | + { |
|
79 | + return $this->$offset; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * Set the value for a given offset. |
|
84 | - * |
|
85 | - * @param mixed $offset |
|
86 | - * @param mixed $value |
|
87 | - * @return void |
|
88 | - */ |
|
89 | - public function offsetSet($offset, $value) |
|
90 | - { |
|
91 | - $this->$offset = $value; |
|
92 | - } |
|
82 | + /** |
|
83 | + * Set the value for a given offset. |
|
84 | + * |
|
85 | + * @param mixed $offset |
|
86 | + * @param mixed $value |
|
87 | + * @return void |
|
88 | + */ |
|
89 | + public function offsetSet($offset, $value) |
|
90 | + { |
|
91 | + $this->$offset = $value; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Unset the value for a given offset. |
|
96 | - * |
|
97 | - * @param mixed $offset |
|
98 | - * @return void |
|
99 | - */ |
|
100 | - public function offsetUnset($offset) |
|
101 | - { |
|
102 | - unset($this->$offset); |
|
103 | - } |
|
94 | + /** |
|
95 | + * Unset the value for a given offset. |
|
96 | + * |
|
97 | + * @param mixed $offset |
|
98 | + * @return void |
|
99 | + */ |
|
100 | + public function offsetUnset($offset) |
|
101 | + { |
|
102 | + unset($this->$offset); |
|
103 | + } |
|
104 | 104 | |
105 | - /** |
|
106 | - * Convert the object into something JSON serializable. |
|
107 | - * |
|
108 | - * @return array |
|
109 | - */ |
|
110 | - public function jsonSerialize() |
|
111 | - { |
|
112 | - return $this->toArray(); |
|
113 | - } |
|
105 | + /** |
|
106 | + * Convert the object into something JSON serializable. |
|
107 | + * |
|
108 | + * @return array |
|
109 | + */ |
|
110 | + public function jsonSerialize() |
|
111 | + { |
|
112 | + return $this->toArray(); |
|
113 | + } |
|
114 | 114 | |
115 | - /** |
|
116 | - * Convert the entity instance to JSON. |
|
117 | - * |
|
118 | - * @param int $options |
|
119 | - * @return string |
|
120 | - */ |
|
121 | - public function toJson($options = 0) |
|
122 | - { |
|
123 | - return json_encode($this->toArray(), $options); |
|
124 | - } |
|
115 | + /** |
|
116 | + * Convert the entity instance to JSON. |
|
117 | + * |
|
118 | + * @param int $options |
|
119 | + * @return string |
|
120 | + */ |
|
121 | + public function toJson($options = 0) |
|
122 | + { |
|
123 | + return json_encode($this->toArray(), $options); |
|
124 | + } |
|
125 | 125 | |
126 | 126 | |
127 | - /** |
|
128 | - * Convert Mappable object to array; |
|
129 | - * |
|
130 | - * @return array |
|
131 | - */ |
|
132 | - public function toArray() |
|
133 | - { |
|
134 | - return $this->attributesToArray($this->attributes); |
|
135 | - } |
|
127 | + /** |
|
128 | + * Convert Mappable object to array; |
|
129 | + * |
|
130 | + * @return array |
|
131 | + */ |
|
132 | + public function toArray() |
|
133 | + { |
|
134 | + return $this->attributesToArray($this->attributes); |
|
135 | + } |
|
136 | 136 | |
137 | - /** |
|
138 | - * Transform the Object to array/json, |
|
139 | - * |
|
140 | - * @return array |
|
141 | - */ |
|
142 | - protected function attributesToArray(array $sourceAttributes) |
|
143 | - { |
|
144 | - $attributes = []; |
|
137 | + /** |
|
138 | + * Transform the Object to array/json, |
|
139 | + * |
|
140 | + * @return array |
|
141 | + */ |
|
142 | + protected function attributesToArray(array $sourceAttributes) |
|
143 | + { |
|
144 | + $attributes = []; |
|
145 | 145 | |
146 | - foreach ($sourceAttributes as $key => $attribute) { |
|
147 | - // If the attribute is a proxy, and hasn't be loaded, we discard |
|
148 | - // it from the returned set. |
|
149 | - if ($attribute instanceof ProxyInterface && ! $attribute->isLoaded()) { |
|
150 | - continue; |
|
151 | - } |
|
146 | + foreach ($sourceAttributes as $key => $attribute) { |
|
147 | + // If the attribute is a proxy, and hasn't be loaded, we discard |
|
148 | + // it from the returned set. |
|
149 | + if ($attribute instanceof ProxyInterface && ! $attribute->isLoaded()) { |
|
150 | + continue; |
|
151 | + } |
|
152 | 152 | |
153 | - if ($attribute instanceof Carbon) { |
|
154 | - $attributes[$key] = $attribute->__toString(); |
|
155 | - continue; |
|
156 | - } |
|
153 | + if ($attribute instanceof Carbon) { |
|
154 | + $attributes[$key] = $attribute->__toString(); |
|
155 | + continue; |
|
156 | + } |
|
157 | 157 | |
158 | - if ($attribute instanceof Arrayable) { |
|
159 | - $attributes[$key] = $attribute->toArray(); |
|
160 | - } else { |
|
161 | - $attributes[$key] = $attribute; |
|
162 | - } |
|
163 | - } |
|
164 | - return $attributes; |
|
165 | - } |
|
158 | + if ($attribute instanceof Arrayable) { |
|
159 | + $attributes[$key] = $attribute->toArray(); |
|
160 | + } else { |
|
161 | + $attributes[$key] = $attribute; |
|
162 | + } |
|
163 | + } |
|
164 | + return $attributes; |
|
165 | + } |
|
166 | 166 | } |
@@ -146,7 +146,7 @@ |
||
146 | 146 | foreach ($sourceAttributes as $key => $attribute) { |
147 | 147 | // If the attribute is a proxy, and hasn't be loaded, we discard |
148 | 148 | // it from the returned set. |
149 | - if ($attribute instanceof ProxyInterface && ! $attribute->isLoaded()) { |
|
149 | + if ($attribute instanceof ProxyInterface && !$attribute->isLoaded()) { |
|
150 | 150 | continue; |
151 | 151 | } |
152 | 152 |
@@ -5,35 +5,35 @@ |
||
5 | 5 | class EntityNotFoundException extends RuntimeException |
6 | 6 | { |
7 | 7 | |
8 | - /** |
|
9 | - * Name of the affected Entity Map. |
|
10 | - * |
|
11 | - * @var string |
|
12 | - */ |
|
13 | - protected $entity; |
|
8 | + /** |
|
9 | + * Name of the affected Entity Map. |
|
10 | + * |
|
11 | + * @var string |
|
12 | + */ |
|
13 | + protected $entity; |
|
14 | 14 | |
15 | - /** |
|
16 | - * Set the affected Entity Map. |
|
17 | - * |
|
18 | - * @param string $entity |
|
19 | - * @return $this |
|
20 | - */ |
|
21 | - public function setEntity($entity) |
|
22 | - { |
|
23 | - $this->entity = $entity; |
|
15 | + /** |
|
16 | + * Set the affected Entity Map. |
|
17 | + * |
|
18 | + * @param string $entity |
|
19 | + * @return $this |
|
20 | + */ |
|
21 | + public function setEntity($entity) |
|
22 | + { |
|
23 | + $this->entity = $entity; |
|
24 | 24 | |
25 | - $this->message = "No query results for entity [{$entity}]."; |
|
25 | + $this->message = "No query results for entity [{$entity}]."; |
|
26 | 26 | |
27 | - return $this; |
|
28 | - } |
|
27 | + return $this; |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * Get the affected Entity. |
|
32 | - * |
|
33 | - * @return string |
|
34 | - */ |
|
35 | - public function getEnity() |
|
36 | - { |
|
37 | - return $this->entity; |
|
38 | - } |
|
30 | + /** |
|
31 | + * Get the affected Entity. |
|
32 | + * |
|
33 | + * @return string |
|
34 | + */ |
|
35 | + public function getEnity() |
|
36 | + { |
|
37 | + return $this->entity; |
|
38 | + } |
|
39 | 39 | } |
@@ -7,63 +7,63 @@ |
||
7 | 7 | trait MappableTrait |
8 | 8 | { |
9 | 9 | |
10 | - /** |
|
11 | - * The Entity's Attributes |
|
12 | - * @var array |
|
13 | - */ |
|
14 | - protected $attributes = []; |
|
10 | + /** |
|
11 | + * The Entity's Attributes |
|
12 | + * @var array |
|
13 | + */ |
|
14 | + protected $attributes = []; |
|
15 | 15 | |
16 | - /** |
|
17 | - * Method used by the mapper to set the object |
|
18 | - * attribute raw values (hydration) |
|
19 | - * |
|
20 | - * @param array $attributes |
|
21 | - * |
|
22 | - * @return void |
|
23 | - */ |
|
24 | - public function setEntityAttributes(array $attributes) |
|
25 | - { |
|
26 | - $this->attributes = $attributes; |
|
27 | - } |
|
16 | + /** |
|
17 | + * Method used by the mapper to set the object |
|
18 | + * attribute raw values (hydration) |
|
19 | + * |
|
20 | + * @param array $attributes |
|
21 | + * |
|
22 | + * @return void |
|
23 | + */ |
|
24 | + public function setEntityAttributes(array $attributes) |
|
25 | + { |
|
26 | + $this->attributes = $attributes; |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * Method used by the mapper to get the |
|
31 | - * raw object's values. |
|
32 | - * |
|
33 | - * @return array |
|
34 | - */ |
|
35 | - public function getEntityAttributes() |
|
36 | - { |
|
37 | - return $this->attributes; |
|
38 | - } |
|
29 | + /** |
|
30 | + * Method used by the mapper to get the |
|
31 | + * raw object's values. |
|
32 | + * |
|
33 | + * @return array |
|
34 | + */ |
|
35 | + public function getEntityAttributes() |
|
36 | + { |
|
37 | + return $this->attributes; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * Method used by the mapper to set raw |
|
42 | - * key-value pair |
|
43 | - * |
|
44 | - * @param string $key |
|
45 | - * @param string $value |
|
46 | - * |
|
47 | - * @return void |
|
48 | - */ |
|
49 | - public function setEntityAttribute($key, $value) |
|
50 | - { |
|
51 | - $this->attributes[$key] = $value; |
|
52 | - } |
|
40 | + /** |
|
41 | + * Method used by the mapper to set raw |
|
42 | + * key-value pair |
|
43 | + * |
|
44 | + * @param string $key |
|
45 | + * @param string $value |
|
46 | + * |
|
47 | + * @return void |
|
48 | + */ |
|
49 | + public function setEntityAttribute($key, $value) |
|
50 | + { |
|
51 | + $this->attributes[$key] = $value; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Method used by the mapper to get single |
|
56 | - * key-value pair |
|
57 | - * |
|
58 | - * @param string $key |
|
59 | - * @return mixed|null |
|
60 | - */ |
|
61 | - public function getEntityAttribute($key) |
|
62 | - { |
|
63 | - if(array_key_exists($key, $this->attributes)) |
|
64 | - { |
|
65 | - return $this->attributes[$key]; |
|
66 | - } |
|
67 | - else return null; |
|
68 | - } |
|
54 | + /** |
|
55 | + * Method used by the mapper to get single |
|
56 | + * key-value pair |
|
57 | + * |
|
58 | + * @param string $key |
|
59 | + * @return mixed|null |
|
60 | + */ |
|
61 | + public function getEntityAttribute($key) |
|
62 | + { |
|
63 | + if(array_key_exists($key, $this->attributes)) |
|
64 | + { |
|
65 | + return $this->attributes[$key]; |
|
66 | + } |
|
67 | + else return null; |
|
68 | + } |
|
69 | 69 | } |
@@ -60,7 +60,7 @@ |
||
60 | 60 | */ |
61 | 61 | public function getEntityAttribute($key) |
62 | 62 | { |
63 | - if(array_key_exists($key, $this->attributes)) |
|
63 | + if (array_key_exists($key, $this->attributes)) |
|
64 | 64 | { |
65 | 65 | return $this->attributes[$key]; |
66 | 66 | } |
@@ -63,7 +63,8 @@ |
||
63 | 63 | if(array_key_exists($key, $this->attributes)) |
64 | 64 | { |
65 | 65 | return $this->attributes[$key]; |
66 | + } else { |
|
67 | + return null; |
|
66 | 68 | } |
67 | - else return null; |
|
68 | 69 | } |
69 | 70 | } |
@@ -5,48 +5,48 @@ |
||
5 | 5 | class MorphMany extends MorphOneOrMany |
6 | 6 | { |
7 | 7 | |
8 | - /** |
|
9 | - * Get the results of the relationship. |
|
10 | - * |
|
11 | - * @return mixed |
|
12 | - */ |
|
13 | - public function getResults($relation) |
|
14 | - { |
|
15 | - $results = $this->query->get(); |
|
16 | - |
|
17 | - $this->cacheRelation($results, $relation); |
|
18 | - |
|
19 | - return $results; |
|
20 | - } |
|
21 | - |
|
22 | - /** |
|
23 | - * Initialize the relation on a set of models. |
|
24 | - * |
|
25 | - * @param array $entities |
|
26 | - * @param string $relation |
|
27 | - * @return array |
|
28 | - */ |
|
29 | - public function initRelation(array $entities, $relation) |
|
30 | - { |
|
31 | - foreach ($entities as $entity) { |
|
32 | - $entity = $this->factory->make($entity); |
|
33 | - |
|
34 | - $entity->setEntityAttribute($relation, $this->relatedMap->newCollection()); |
|
35 | - } |
|
36 | - |
|
37 | - return $entities; |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * Match the eagerly loaded results to their parents. |
|
42 | - * |
|
43 | - * @param array $entities |
|
44 | - * @param \Analogue\ORM\EntityCollection $results |
|
45 | - * @param string $relation |
|
46 | - * @return array |
|
47 | - */ |
|
48 | - public function match(array $entities, EntityCollection $results, $relation) |
|
49 | - { |
|
50 | - return $this->matchMany($entities, $results, $relation); |
|
51 | - } |
|
8 | + /** |
|
9 | + * Get the results of the relationship. |
|
10 | + * |
|
11 | + * @return mixed |
|
12 | + */ |
|
13 | + public function getResults($relation) |
|
14 | + { |
|
15 | + $results = $this->query->get(); |
|
16 | + |
|
17 | + $this->cacheRelation($results, $relation); |
|
18 | + |
|
19 | + return $results; |
|
20 | + } |
|
21 | + |
|
22 | + /** |
|
23 | + * Initialize the relation on a set of models. |
|
24 | + * |
|
25 | + * @param array $entities |
|
26 | + * @param string $relation |
|
27 | + * @return array |
|
28 | + */ |
|
29 | + public function initRelation(array $entities, $relation) |
|
30 | + { |
|
31 | + foreach ($entities as $entity) { |
|
32 | + $entity = $this->factory->make($entity); |
|
33 | + |
|
34 | + $entity->setEntityAttribute($relation, $this->relatedMap->newCollection()); |
|
35 | + } |
|
36 | + |
|
37 | + return $entities; |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * Match the eagerly loaded results to their parents. |
|
42 | + * |
|
43 | + * @param array $entities |
|
44 | + * @param \Analogue\ORM\EntityCollection $results |
|
45 | + * @param string $relation |
|
46 | + * @return array |
|
47 | + */ |
|
48 | + public function match(array $entities, EntityCollection $results, $relation) |
|
49 | + { |
|
50 | + return $this->matchMany($entities, $results, $relation); |
|
51 | + } |
|
52 | 52 | } |
@@ -10,303 +10,303 @@ |
||
10 | 10 | class BelongsTo extends Relationship |
11 | 11 | { |
12 | 12 | |
13 | - /** |
|
14 | - * The foreign key of the parent model. |
|
15 | - * |
|
16 | - * @var string |
|
17 | - */ |
|
18 | - protected $foreignKey; |
|
19 | - |
|
20 | - /** |
|
21 | - * The associated key on the parent model. |
|
22 | - * |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - protected $otherKey; |
|
26 | - |
|
27 | - /** |
|
28 | - * The name of the relationship. |
|
29 | - * |
|
30 | - * @var string |
|
31 | - */ |
|
32 | - protected $relation; |
|
33 | - |
|
34 | - /** |
|
35 | - * Indicate if the parent entity hold the key for the relation. |
|
36 | - * |
|
37 | - * @var boolean |
|
38 | - */ |
|
39 | - protected static $ownForeignKey = true; |
|
40 | - |
|
41 | - /** |
|
42 | - * Create a new belongs to relationship instance. |
|
43 | - * |
|
44 | - * @param \Analogue\ORM\System\Mapper $mapper |
|
45 | - * @param Mappable $parent |
|
46 | - * @param string $foreignKey |
|
47 | - * @param string $otherKey |
|
48 | - * @param string $relation |
|
49 | - */ |
|
50 | - public function __construct(Mapper $mapper, $parent, $foreignKey, $otherKey, $relation) |
|
51 | - { |
|
52 | - $this->otherKey = $otherKey; |
|
53 | - $this->relation = $relation; |
|
54 | - $this->foreignKey = $foreignKey; |
|
55 | - |
|
56 | - parent::__construct($mapper, $parent); |
|
57 | - } |
|
13 | + /** |
|
14 | + * The foreign key of the parent model. |
|
15 | + * |
|
16 | + * @var string |
|
17 | + */ |
|
18 | + protected $foreignKey; |
|
19 | + |
|
20 | + /** |
|
21 | + * The associated key on the parent model. |
|
22 | + * |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + protected $otherKey; |
|
26 | + |
|
27 | + /** |
|
28 | + * The name of the relationship. |
|
29 | + * |
|
30 | + * @var string |
|
31 | + */ |
|
32 | + protected $relation; |
|
33 | + |
|
34 | + /** |
|
35 | + * Indicate if the parent entity hold the key for the relation. |
|
36 | + * |
|
37 | + * @var boolean |
|
38 | + */ |
|
39 | + protected static $ownForeignKey = true; |
|
40 | + |
|
41 | + /** |
|
42 | + * Create a new belongs to relationship instance. |
|
43 | + * |
|
44 | + * @param \Analogue\ORM\System\Mapper $mapper |
|
45 | + * @param Mappable $parent |
|
46 | + * @param string $foreignKey |
|
47 | + * @param string $otherKey |
|
48 | + * @param string $relation |
|
49 | + */ |
|
50 | + public function __construct(Mapper $mapper, $parent, $foreignKey, $otherKey, $relation) |
|
51 | + { |
|
52 | + $this->otherKey = $otherKey; |
|
53 | + $this->relation = $relation; |
|
54 | + $this->foreignKey = $foreignKey; |
|
55 | + |
|
56 | + parent::__construct($mapper, $parent); |
|
57 | + } |
|
58 | 58 | |
59 | - public function attachTo($related) |
|
60 | - { |
|
61 | - return $this->associate($related); |
|
62 | - } |
|
63 | - |
|
64 | - public function detachFrom($related) |
|
65 | - { |
|
66 | - return $this->dissociate($related); |
|
67 | - } |
|
59 | + public function attachTo($related) |
|
60 | + { |
|
61 | + return $this->associate($related); |
|
62 | + } |
|
63 | + |
|
64 | + public function detachFrom($related) |
|
65 | + { |
|
66 | + return $this->dissociate($related); |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * Get the results of the relationship. |
|
71 | - * |
|
72 | - * @return mixed |
|
73 | - */ |
|
74 | - public function getResults($relation) |
|
75 | - { |
|
76 | - $result = $this->query->first(); |
|
77 | - |
|
78 | - $this->cacheRelation($result, $relation); |
|
79 | - |
|
80 | - return $result; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Set the base constraints on the relation query. |
|
85 | - * |
|
86 | - * @return void |
|
87 | - */ |
|
88 | - public function addConstraints() |
|
89 | - { |
|
90 | - if (static::$constraints) { |
|
91 | - // For belongs to relationships, which are essentially the inverse of has one |
|
92 | - // or has many relationships, we need to actually query on the primary key |
|
93 | - // of the related models matching on the foreign key that's on a parent. |
|
94 | - $table = $this->relatedMap->getTable(); |
|
95 | - |
|
96 | - $this->query->where($table.'.'.$this->otherKey, '=', $this->parent->getEntityAttribute($this->foreignKey)); |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Add the constraints for a relationship count query. |
|
102 | - * |
|
103 | - * @param \Analogue\ORM\System\Query $query |
|
104 | - * @param \Analogue\ORM\System\Query $parent |
|
105 | - * @return \Analogue\ORM\System\Query |
|
106 | - */ |
|
107 | - public function getRelationCountQuery(Query $query, Query $parent) |
|
108 | - { |
|
109 | - $query->select(new Expression('count(*)')); |
|
110 | - |
|
111 | - $otherKey = $this->wrap($query->getTable().'.'.$this->otherKey); |
|
112 | - |
|
113 | - return $query->where($this->getQualifiedForeignKey(), '=', new Expression($otherKey)); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Set the constraints for an eager load of the relation. |
|
118 | - * |
|
119 | - * @param array $entities |
|
120 | - * @return void |
|
121 | - */ |
|
122 | - public function addEagerConstraints(array $entities) |
|
123 | - { |
|
124 | - // We'll grab the primary key name of the related models since it could be set to |
|
125 | - // a non-standard name and not "id". We will then construct the constraint for |
|
126 | - // our eagerly loading query so it returns the proper models from execution. |
|
127 | - $key = $this->relatedMap->getTable().'.'.$this->otherKey; |
|
128 | - |
|
129 | - $this->query->whereIn($key, $this->getEagerModelKeys($entities)); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Gather the keys from an array of related models. |
|
134 | - * |
|
135 | - * @param array $entities |
|
136 | - * @return array |
|
137 | - */ |
|
138 | - protected function getEagerModelKeys(array $entities) |
|
139 | - { |
|
140 | - $keys = array(); |
|
141 | - |
|
142 | - // First we need to gather all of the keys from the parent models so we know what |
|
143 | - // to query for via the eager loading query. We will add them to an array then |
|
144 | - // execute a "where in" statement to gather up all of those related records. |
|
145 | - foreach ($entities as $entity) { |
|
146 | - $entity = $this->factory->make($entity); |
|
147 | - |
|
148 | - if (! is_null($value = $entity->getEntityAttribute($this->foreignKey))) { |
|
149 | - $keys[] = $value; |
|
150 | - } |
|
151 | - } |
|
152 | - |
|
153 | - // If there are no keys that were not null we will just return an array with 0 in |
|
154 | - // it so the query doesn't fail, but will not return any results, which should |
|
155 | - // be what this developer is expecting in a case where this happens to them. |
|
156 | - if (count($keys) == 0) { |
|
157 | - return array(0); |
|
158 | - } |
|
159 | - |
|
160 | - return array_values(array_unique($keys)); |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Initialize the relation on a set of models. |
|
165 | - * |
|
166 | - * @param array $entities |
|
167 | - * @param string $relation |
|
168 | - * @return array |
|
169 | - */ |
|
170 | - public function initRelation(array $entities, $relation) |
|
171 | - { |
|
172 | - foreach ($entities as $entity) { |
|
173 | - $entity = $this->factory->make($entity); |
|
174 | - $entity->setEntityAttribute($relation, null); |
|
175 | - } |
|
176 | - |
|
177 | - return $entities; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Match the eagerly loaded results to their parents. |
|
182 | - * |
|
183 | - * @param array $entities |
|
184 | - * @param \Analogue\ORM\EntityCollection $results |
|
185 | - * @param string $relation |
|
186 | - * @return array |
|
187 | - */ |
|
188 | - public function match(array $entities, EntityCollection $results, $relation) |
|
189 | - { |
|
190 | - $foreign = $this->foreignKey; |
|
191 | - |
|
192 | - $other = $this->otherKey; |
|
193 | - |
|
194 | - // First we will get to build a dictionary of the child models by their primary |
|
195 | - // key of the relationship, then we can easily match the children back onto |
|
196 | - // the parents using that dictionary and the primary key of the children. |
|
197 | - $dictionary = []; |
|
198 | - |
|
199 | - foreach ($results as $result) { |
|
200 | - $result = $this->factory->make($result); |
|
201 | - $dictionary[$result->getEntityAttribute($other)] = $result->getObject(); |
|
202 | - } |
|
203 | - |
|
204 | - // Once we have the dictionary constructed, we can loop through all the parents |
|
205 | - // and match back onto their children using these keys of the dictionary and |
|
206 | - // the primary key of the children to map them onto the correct instances. |
|
207 | - foreach ($entities as $entity) { |
|
208 | - $entity = $this->factory->make($entity); |
|
209 | - |
|
210 | - if (isset($dictionary[$entity->getEntityAttribute($foreign)])) { |
|
211 | - $entity->setEntityAttribute($relation, $dictionary[$entity->getEntityAttribute($foreign)]); |
|
212 | - } |
|
213 | - } |
|
214 | - |
|
215 | - return $entities; |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * Associate the model instance to the given parent. |
|
220 | - * |
|
221 | - * @param mixed $entity |
|
222 | - * @return void |
|
223 | - */ |
|
224 | - public function associate($entity) |
|
225 | - { |
|
226 | - // The Mapper will retrieve this association within the object model, we won't be using |
|
227 | - // the foreign key attribute inside the parent Entity. |
|
228 | - // |
|
229 | - //$this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey)); |
|
230 | - // |
|
231 | - // Instead, we'll just add the object to the Entity's attribute |
|
69 | + /** |
|
70 | + * Get the results of the relationship. |
|
71 | + * |
|
72 | + * @return mixed |
|
73 | + */ |
|
74 | + public function getResults($relation) |
|
75 | + { |
|
76 | + $result = $this->query->first(); |
|
77 | + |
|
78 | + $this->cacheRelation($result, $relation); |
|
79 | + |
|
80 | + return $result; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Set the base constraints on the relation query. |
|
85 | + * |
|
86 | + * @return void |
|
87 | + */ |
|
88 | + public function addConstraints() |
|
89 | + { |
|
90 | + if (static::$constraints) { |
|
91 | + // For belongs to relationships, which are essentially the inverse of has one |
|
92 | + // or has many relationships, we need to actually query on the primary key |
|
93 | + // of the related models matching on the foreign key that's on a parent. |
|
94 | + $table = $this->relatedMap->getTable(); |
|
95 | + |
|
96 | + $this->query->where($table.'.'.$this->otherKey, '=', $this->parent->getEntityAttribute($this->foreignKey)); |
|
97 | + } |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Add the constraints for a relationship count query. |
|
102 | + * |
|
103 | + * @param \Analogue\ORM\System\Query $query |
|
104 | + * @param \Analogue\ORM\System\Query $parent |
|
105 | + * @return \Analogue\ORM\System\Query |
|
106 | + */ |
|
107 | + public function getRelationCountQuery(Query $query, Query $parent) |
|
108 | + { |
|
109 | + $query->select(new Expression('count(*)')); |
|
110 | + |
|
111 | + $otherKey = $this->wrap($query->getTable().'.'.$this->otherKey); |
|
112 | + |
|
113 | + return $query->where($this->getQualifiedForeignKey(), '=', new Expression($otherKey)); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Set the constraints for an eager load of the relation. |
|
118 | + * |
|
119 | + * @param array $entities |
|
120 | + * @return void |
|
121 | + */ |
|
122 | + public function addEagerConstraints(array $entities) |
|
123 | + { |
|
124 | + // We'll grab the primary key name of the related models since it could be set to |
|
125 | + // a non-standard name and not "id". We will then construct the constraint for |
|
126 | + // our eagerly loading query so it returns the proper models from execution. |
|
127 | + $key = $this->relatedMap->getTable().'.'.$this->otherKey; |
|
128 | + |
|
129 | + $this->query->whereIn($key, $this->getEagerModelKeys($entities)); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Gather the keys from an array of related models. |
|
134 | + * |
|
135 | + * @param array $entities |
|
136 | + * @return array |
|
137 | + */ |
|
138 | + protected function getEagerModelKeys(array $entities) |
|
139 | + { |
|
140 | + $keys = array(); |
|
141 | + |
|
142 | + // First we need to gather all of the keys from the parent models so we know what |
|
143 | + // to query for via the eager loading query. We will add them to an array then |
|
144 | + // execute a "where in" statement to gather up all of those related records. |
|
145 | + foreach ($entities as $entity) { |
|
146 | + $entity = $this->factory->make($entity); |
|
147 | + |
|
148 | + if (! is_null($value = $entity->getEntityAttribute($this->foreignKey))) { |
|
149 | + $keys[] = $value; |
|
150 | + } |
|
151 | + } |
|
152 | + |
|
153 | + // If there are no keys that were not null we will just return an array with 0 in |
|
154 | + // it so the query doesn't fail, but will not return any results, which should |
|
155 | + // be what this developer is expecting in a case where this happens to them. |
|
156 | + if (count($keys) == 0) { |
|
157 | + return array(0); |
|
158 | + } |
|
159 | + |
|
160 | + return array_values(array_unique($keys)); |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Initialize the relation on a set of models. |
|
165 | + * |
|
166 | + * @param array $entities |
|
167 | + * @param string $relation |
|
168 | + * @return array |
|
169 | + */ |
|
170 | + public function initRelation(array $entities, $relation) |
|
171 | + { |
|
172 | + foreach ($entities as $entity) { |
|
173 | + $entity = $this->factory->make($entity); |
|
174 | + $entity->setEntityAttribute($relation, null); |
|
175 | + } |
|
176 | + |
|
177 | + return $entities; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Match the eagerly loaded results to their parents. |
|
182 | + * |
|
183 | + * @param array $entities |
|
184 | + * @param \Analogue\ORM\EntityCollection $results |
|
185 | + * @param string $relation |
|
186 | + * @return array |
|
187 | + */ |
|
188 | + public function match(array $entities, EntityCollection $results, $relation) |
|
189 | + { |
|
190 | + $foreign = $this->foreignKey; |
|
191 | + |
|
192 | + $other = $this->otherKey; |
|
193 | + |
|
194 | + // First we will get to build a dictionary of the child models by their primary |
|
195 | + // key of the relationship, then we can easily match the children back onto |
|
196 | + // the parents using that dictionary and the primary key of the children. |
|
197 | + $dictionary = []; |
|
198 | + |
|
199 | + foreach ($results as $result) { |
|
200 | + $result = $this->factory->make($result); |
|
201 | + $dictionary[$result->getEntityAttribute($other)] = $result->getObject(); |
|
202 | + } |
|
203 | + |
|
204 | + // Once we have the dictionary constructed, we can loop through all the parents |
|
205 | + // and match back onto their children using these keys of the dictionary and |
|
206 | + // the primary key of the children to map them onto the correct instances. |
|
207 | + foreach ($entities as $entity) { |
|
208 | + $entity = $this->factory->make($entity); |
|
209 | + |
|
210 | + if (isset($dictionary[$entity->getEntityAttribute($foreign)])) { |
|
211 | + $entity->setEntityAttribute($relation, $dictionary[$entity->getEntityAttribute($foreign)]); |
|
212 | + } |
|
213 | + } |
|
214 | + |
|
215 | + return $entities; |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * Associate the model instance to the given parent. |
|
220 | + * |
|
221 | + * @param mixed $entity |
|
222 | + * @return void |
|
223 | + */ |
|
224 | + public function associate($entity) |
|
225 | + { |
|
226 | + // The Mapper will retrieve this association within the object model, we won't be using |
|
227 | + // the foreign key attribute inside the parent Entity. |
|
228 | + // |
|
229 | + //$this->parent->setEntityAttribute($this->foreignKey, $entity->getEntityAttribute($this->otherKey)); |
|
230 | + // |
|
231 | + // Instead, we'll just add the object to the Entity's attribute |
|
232 | 232 | |
233 | - $this->parent->setEntityAttribute($this->relation, $entity); |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * Dissociate previously associated model from the given parent. |
|
238 | - * |
|
239 | - * @return Mappable |
|
240 | - */ |
|
241 | - public function dissociate() |
|
242 | - { |
|
243 | - // The Mapper will retrieve this association within the object model, we won't be using |
|
244 | - // the foreign key attribute inside the parent Entity. |
|
245 | - // |
|
246 | - //$this->parent->setEntityAttribute($this->foreignKey, null); |
|
233 | + $this->parent->setEntityAttribute($this->relation, $entity); |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * Dissociate previously associated model from the given parent. |
|
238 | + * |
|
239 | + * @return Mappable |
|
240 | + */ |
|
241 | + public function dissociate() |
|
242 | + { |
|
243 | + // The Mapper will retrieve this association within the object model, we won't be using |
|
244 | + // the foreign key attribute inside the parent Entity. |
|
245 | + // |
|
246 | + //$this->parent->setEntityAttribute($this->foreignKey, null); |
|
247 | 247 | |
248 | - $this->parent->setEntityAttribute($this->relation, null); |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * Get the foreign key of the relationship. |
|
253 | - * |
|
254 | - * @return string |
|
255 | - */ |
|
256 | - public function getForeignKey() |
|
257 | - { |
|
258 | - return $this->foreignKey; |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * Get the foreign key value pair for a related object |
|
263 | - * |
|
264 | - * @var mixed $related |
|
265 | - * |
|
266 | - * @return array |
|
267 | - */ |
|
268 | - public function getForeignKeyValuePair($related) |
|
269 | - { |
|
270 | - $foreignKey = $this->getForeignKey(); |
|
271 | - |
|
272 | - if ($related) { |
|
273 | - $wrapper = $this->factory->make($related); |
|
274 | - |
|
275 | - $relatedKey = $this->relatedMap->getKeyName(); |
|
276 | - |
|
277 | - return [$foreignKey => $wrapper->getEntityAttribute($relatedKey)]; |
|
278 | - } else { |
|
279 | - return [$foreignKey => null]; |
|
280 | - } |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * Get the fully qualified foreign key of the relationship. |
|
285 | - * |
|
286 | - * @return string |
|
287 | - */ |
|
288 | - public function getQualifiedForeignKey() |
|
289 | - { |
|
290 | - return $this->parentMap->getTable().'.'.$this->foreignKey; |
|
291 | - } |
|
292 | - |
|
293 | - /** |
|
294 | - * Get the associated key of the relationship. |
|
295 | - * |
|
296 | - * @return string |
|
297 | - */ |
|
298 | - public function getOtherKey() |
|
299 | - { |
|
300 | - return $this->otherKey; |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Get the fully qualified associated key of the relationship. |
|
305 | - * |
|
306 | - * @return string |
|
307 | - */ |
|
308 | - public function getQualifiedOtherKeyName() |
|
309 | - { |
|
310 | - return $this->relatedMap->getTable().'.'.$this->otherKey; |
|
311 | - } |
|
248 | + $this->parent->setEntityAttribute($this->relation, null); |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * Get the foreign key of the relationship. |
|
253 | + * |
|
254 | + * @return string |
|
255 | + */ |
|
256 | + public function getForeignKey() |
|
257 | + { |
|
258 | + return $this->foreignKey; |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * Get the foreign key value pair for a related object |
|
263 | + * |
|
264 | + * @var mixed $related |
|
265 | + * |
|
266 | + * @return array |
|
267 | + */ |
|
268 | + public function getForeignKeyValuePair($related) |
|
269 | + { |
|
270 | + $foreignKey = $this->getForeignKey(); |
|
271 | + |
|
272 | + if ($related) { |
|
273 | + $wrapper = $this->factory->make($related); |
|
274 | + |
|
275 | + $relatedKey = $this->relatedMap->getKeyName(); |
|
276 | + |
|
277 | + return [$foreignKey => $wrapper->getEntityAttribute($relatedKey)]; |
|
278 | + } else { |
|
279 | + return [$foreignKey => null]; |
|
280 | + } |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * Get the fully qualified foreign key of the relationship. |
|
285 | + * |
|
286 | + * @return string |
|
287 | + */ |
|
288 | + public function getQualifiedForeignKey() |
|
289 | + { |
|
290 | + return $this->parentMap->getTable().'.'.$this->foreignKey; |
|
291 | + } |
|
292 | + |
|
293 | + /** |
|
294 | + * Get the associated key of the relationship. |
|
295 | + * |
|
296 | + * @return string |
|
297 | + */ |
|
298 | + public function getOtherKey() |
|
299 | + { |
|
300 | + return $this->otherKey; |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Get the fully qualified associated key of the relationship. |
|
305 | + * |
|
306 | + * @return string |
|
307 | + */ |
|
308 | + public function getQualifiedOtherKeyName() |
|
309 | + { |
|
310 | + return $this->relatedMap->getTable().'.'.$this->otherKey; |
|
311 | + } |
|
312 | 312 | } |
@@ -145,7 +145,7 @@ |
||
145 | 145 | foreach ($entities as $entity) { |
146 | 146 | $entity = $this->factory->make($entity); |
147 | 147 | |
148 | - if (! is_null($value = $entity->getEntityAttribute($this->foreignKey))) { |
|
148 | + if (!is_null($value = $entity->getEntityAttribute($this->foreignKey))) { |
|
149 | 149 | $keys[] = $value; |
150 | 150 | } |
151 | 151 | } |