1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Model; |
13
|
|
|
|
14
|
|
|
use Zemit\Mvc\Model\EagerLoading\Loader; |
15
|
|
|
|
16
|
|
|
trait EagerLoading |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* <code> |
20
|
|
|
* <?php |
21
|
|
|
* |
22
|
|
|
* $limit = 100; |
23
|
|
|
* $offset = max(0, $this->request->getQuery('page', 'int') - 1) * $limit; |
24
|
|
|
* |
25
|
|
|
* $manufacturers = Manufacturer::with('Robots.Parts', [ |
26
|
|
|
* 'limit' => [$limit, $offset] |
27
|
|
|
* ]); |
28
|
|
|
* |
29
|
|
|
* foreach ($manufacturers as $manufacturer) { |
30
|
|
|
* foreach ($manufacturer->robots as $robot) { |
31
|
|
|
* foreach ($robot->parts as $part) { ... } |
32
|
|
|
* } |
33
|
|
|
* } |
34
|
|
|
* |
35
|
|
|
* </code> |
36
|
|
|
* |
37
|
|
|
* @param mixed ...$arguments |
38
|
|
|
*/ |
39
|
|
|
public static function findWith(array ...$arguments) |
40
|
|
|
{ |
41
|
|
|
$parameters = self::getParametersFromArguments($arguments); |
42
|
|
|
$list = static::find($parameters); |
|
|
|
|
43
|
|
|
if ($list->count()) { |
44
|
|
|
return Loader::fromResultset($list, ...$arguments); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $list; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Same as EagerLoadingTrait::findWith() for a single record |
52
|
|
|
* |
53
|
|
|
* @param mixed ...$arguments |
54
|
|
|
* @return false|\Phalcon\Mvc\ModelInterface |
55
|
|
|
*/ |
56
|
|
|
public static function findFirstWith(array ...$arguments) |
57
|
|
|
{ |
58
|
|
|
$parameters = self::getParametersFromArguments($arguments); |
59
|
|
|
$entity = static::findFirst($parameters); |
|
|
|
|
60
|
|
|
if ($entity) { |
61
|
|
|
return Loader::fromModel($entity, ...$arguments); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $entity; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @deprecated |
69
|
|
|
* Alias of findWith |
70
|
|
|
*/ |
71
|
|
|
public static function with(array ...$arguments) |
72
|
|
|
{ |
73
|
|
|
return self::findWith(...$arguments); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @deprecated |
78
|
|
|
* Alias of findWith |
79
|
|
|
*/ |
80
|
|
|
public static function firstWith(array ...$arguments) |
81
|
|
|
{ |
82
|
|
|
return self::findWith(...$arguments); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Call magic method to make the with works in an implicit way |
87
|
|
|
* @todo change it to behavior missingMethods() |
88
|
|
|
* @todo change it |
89
|
|
|
*/ |
90
|
|
|
public static function __callStatic(string $method, array $arguments = []) |
91
|
|
|
{ |
92
|
|
|
// Single - FindFirstBy... |
93
|
|
|
if (strpos($method, 'findFirstWithBy') === 0 || strpos($method, 'firstWithBy') === 0) { |
94
|
|
|
|
95
|
|
|
$forwardMethod = str_replace(['findFirstWithBy', 'firstWithBy'], 'findFirstBy', $method); |
96
|
|
|
$parameters = self::getParametersFromArguments($arguments); |
97
|
|
|
$entity = parent::$forwardMethod($parameters); |
98
|
|
|
|
99
|
|
|
if ($entity) { |
100
|
|
|
return Loader::fromModel($entity, ...$arguments); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $entity; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// List - FindWithBy... |
107
|
|
|
elseif (strpos($method, 'findWithBy') === 0 || strpos($method, 'withBy') === 0) { |
108
|
|
|
|
109
|
|
|
$forwardMethod = str_replace(['findWithBy', 'withBy'], 'findBy', $method); |
110
|
|
|
$parameters = self::getParametersFromArguments($arguments); |
111
|
|
|
$list = parent::$forwardMethod($parameters); |
112
|
|
|
|
113
|
|
|
if ($list->count()) { |
114
|
|
|
return Loader::fromResultset($list, ...$arguments); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $list; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return parent::$method(...$arguments); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* <code> |
125
|
|
|
* |
126
|
|
|
* $manufacturer = Manufacturer::findFirstById(51); |
127
|
|
|
* |
128
|
|
|
* $manufacturer->load('Robots.Parts'); |
129
|
|
|
* |
130
|
|
|
* foreach ($manufacturer->robots as $robot) { |
131
|
|
|
* foreach ($robot->parts as $part) { ... } |
132
|
|
|
* } |
133
|
|
|
* |
134
|
|
|
* </code> |
135
|
|
|
* |
136
|
|
|
* @param mixed ...$arguments |
137
|
|
|
* @return \Phalcon\Mvc\ModelInterface |
138
|
|
|
*/ |
139
|
|
|
public function load(array ...$arguments) |
140
|
|
|
{ |
141
|
|
|
return Loader::fromModel($this, ...$arguments); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the query parameters from a list of arguments |
146
|
|
|
* @return mixed |
147
|
|
|
*/ |
148
|
|
|
public static function getParametersFromArguments(array &$arguments) |
149
|
|
|
{ |
150
|
|
|
$parameters = null; |
151
|
|
|
|
152
|
|
|
if (!empty($arguments)) { |
153
|
|
|
$numArgs = count($arguments); |
154
|
|
|
$lastArg = $numArgs - 1; |
155
|
|
|
|
156
|
|
|
if ($numArgs >= 2) { |
157
|
|
|
$parameters = $arguments[$lastArg]; |
158
|
|
|
unset($arguments[$lastArg]); |
159
|
|
|
|
160
|
|
|
if (isset($parameters['columns'])) { |
161
|
|
|
throw new \LogicException('Results from database must be full models, do not use `columns` key'); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $parameters; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|