|
1 | 1 | <?php namespace Jenssegers\Eloquent;
|
2 | 2 |
|
3 |
| -use Illuminate\Database\Eloquent\Relations\MorphOne; |
4 |
| -use Illuminate\Database\Eloquent\Relations\MorphMany; |
5 |
| -use Illuminate\Database\Eloquent\Relations\Relation; |
6 |
| -use Jenssegers\Mongodb\Relations\HasOne; |
7 |
| -use Jenssegers\Mongodb\Relations\HasMany; |
8 |
| -use Jenssegers\Mongodb\Relations\BelongsTo; |
9 |
| -use Jenssegers\Mongodb\Relations\BelongsToMany; |
10 |
| -use Jenssegers\Mongodb\Relations\MorphTo; |
11 |
| -use Jenssegers\Mongodb\Query\Builder as QueryBuilder; |
| 3 | +use Jenssegers\Mongodb\Eloquent\HybridRelations; |
12 | 4 |
|
13 | 5 | abstract class Model extends \Illuminate\Database\Eloquent\Model {
|
14 | 6 |
|
15 |
| - /** |
16 |
| - * Define a one-to-one relationship. |
17 |
| - * |
18 |
| - * @param string $related |
19 |
| - * @param string $foreignKey |
20 |
| - * @param string $localKey |
21 |
| - * @return \Illuminate\Database\Eloquent\Relations\HasOne |
22 |
| - */ |
23 |
| - public function hasOne($related, $foreignKey = null, $localKey = null) |
24 |
| - { |
25 |
| - // Check if it is a relation with an original model. |
26 |
| - if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model')) |
27 |
| - { |
28 |
| - return parent::hasOne($related, $foreignKey, $localKey); |
29 |
| - } |
30 |
| - |
31 |
| - $foreignKey = $foreignKey ?: $this->getForeignKey(); |
32 |
| - |
33 |
| - $instance = new $related; |
34 |
| - |
35 |
| - $localKey = $localKey ?: $this->getKeyName(); |
36 |
| - |
37 |
| - return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey); |
38 |
| - } |
39 |
| - |
40 |
| - /** |
41 |
| - * Define a polymorphic one-to-one relationship. |
42 |
| - * |
43 |
| - * @param string $related |
44 |
| - * @param string $name |
45 |
| - * @param string $type |
46 |
| - * @param string $id |
47 |
| - * @param string $localKey |
48 |
| - * @return \Illuminate\Database\Eloquent\Relations\MorphOne |
49 |
| - */ |
50 |
| - public function morphOne($related, $name, $type = null, $id = null, $localKey = null) |
51 |
| - { |
52 |
| - // Check if it is a relation with an original model. |
53 |
| - if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model')) |
54 |
| - { |
55 |
| - return parent::morphOne($related, $name, $type, $id, $localKey ); |
56 |
| - } |
57 |
| - |
58 |
| - $instance = new $related; |
59 |
| - |
60 |
| - list($type, $id) = $this->getMorphs($name, $type, $id); |
61 |
| - |
62 |
| - $table = $instance->getTable(); |
63 |
| - |
64 |
| - $localKey = $localKey ?: $this->getKeyName(); |
65 |
| - |
66 |
| - return new MorphOne($instance->newQuery(), $this, $type, $id, $localKey); |
67 |
| - } |
68 |
| - |
69 |
| - /** |
70 |
| - * Define a one-to-many relationship. |
71 |
| - * |
72 |
| - * @param string $related |
73 |
| - * @param string $foreignKey |
74 |
| - * @param string $localKey |
75 |
| - * @return \Illuminate\Database\Eloquent\Relations\HasMany |
76 |
| - */ |
77 |
| - public function hasMany($related, $foreignKey = null, $localKey = null) |
78 |
| - { |
79 |
| - // Check if it is a relation with an original model. |
80 |
| - if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model')) |
81 |
| - { |
82 |
| - return parent::hasMany($related, $foreignKey, $localKey); |
83 |
| - } |
84 |
| - |
85 |
| - $foreignKey = $foreignKey ?: $this->getForeignKey(); |
86 |
| - |
87 |
| - $instance = new $related; |
88 |
| - |
89 |
| - $localKey = $localKey ?: $this->getKeyName(); |
90 |
| - |
91 |
| - return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey); |
92 |
| - } |
93 |
| - |
94 |
| - /** |
95 |
| - * Define a polymorphic one-to-many relationship. |
96 |
| - * |
97 |
| - * @param string $related |
98 |
| - * @param string $name |
99 |
| - * @param string $type |
100 |
| - * @param string $id |
101 |
| - * @param string $localKey |
102 |
| - * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
103 |
| - */ |
104 |
| - public function morphMany($related, $name, $type = null, $id = null, $localKey = null) |
105 |
| - { |
106 |
| - // Check if it is a relation with an original model. |
107 |
| - if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model')) |
108 |
| - { |
109 |
| - return parent::morphMany($related, $name, $type, $id, $localKey); |
110 |
| - } |
111 |
| - |
112 |
| - $instance = new $related; |
113 |
| - |
114 |
| - // Here we will gather up the morph type and ID for the relationship so that we |
115 |
| - // can properly query the intermediate table of a relation. Finally, we will |
116 |
| - // get the table and create the relationship instances for the developers. |
117 |
| - list($type, $id) = $this->getMorphs($name, $type, $id); |
118 |
| - |
119 |
| - $table = $instance->getTable(); |
120 |
| - |
121 |
| - $localKey = $localKey ?: $this->getKeyName(); |
122 |
| - |
123 |
| - return new MorphMany($instance->newQuery(), $this, $type, $id, $localKey); |
124 |
| - } |
125 |
| - |
126 |
| - /** |
127 |
| - * Define an inverse one-to-one or many relationship. |
128 |
| - * |
129 |
| - * @param string $related |
130 |
| - * @param string $foreignKey |
131 |
| - * @param string $otherKey |
132 |
| - * @param string $relation |
133 |
| - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
134 |
| - */ |
135 |
| - public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null) |
136 |
| - { |
137 |
| - // If no relation name was given, we will use this debug backtrace to extract |
138 |
| - // the calling method's name and use that as the relationship name as most |
139 |
| - // of the time this will be what we desire to use for the relatinoships. |
140 |
| - if (is_null($relation)) |
141 |
| - { |
142 |
| - list(, $caller) = debug_backtrace(false); |
143 |
| - |
144 |
| - $relation = $caller['function']; |
145 |
| - } |
146 |
| - |
147 |
| - // Check if it is a relation with an original model. |
148 |
| - if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model')) |
149 |
| - { |
150 |
| - return parent::belongsTo($related, $foreignKey, $otherKey, $relation); |
151 |
| - } |
152 |
| - |
153 |
| - // If no foreign key was supplied, we can use a backtrace to guess the proper |
154 |
| - // foreign key name by using the name of the relationship function, which |
155 |
| - // when combined with an "_id" should conventionally match the columns. |
156 |
| - if (is_null($foreignKey)) |
157 |
| - { |
158 |
| - $foreignKey = snake_case($relation).'_id'; |
159 |
| - } |
160 |
| - |
161 |
| - $instance = new $related; |
162 |
| - |
163 |
| - // Once we have the foreign key names, we'll just create a new Eloquent query |
164 |
| - // for the related models and returns the relationship instance which will |
165 |
| - // actually be responsible for retrieving and hydrating every relations. |
166 |
| - $query = $instance->newQuery(); |
167 |
| - |
168 |
| - $otherKey = $otherKey ?: $instance->getKeyName(); |
169 |
| - |
170 |
| - return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation); |
171 |
| - } |
172 |
| - |
173 |
| - /** |
174 |
| - * Define a polymorphic, inverse one-to-one or many relationship. |
175 |
| - * |
176 |
| - * @param string $name |
177 |
| - * @param string $type |
178 |
| - * @param string $id |
179 |
| - * @return \Illuminate\Database\Eloquent\Relations\MorphTo |
180 |
| - */ |
181 |
| - public function morphTo($name = null, $type = null, $id = null) |
182 |
| - { |
183 |
| - // If no name is provided, we will use the backtrace to get the function name |
184 |
| - // since that is most likely the name of the polymorphic interface. We can |
185 |
| - // use that to get both the class and foreign key that will be utilized. |
186 |
| - if (is_null($name)) |
187 |
| - { |
188 |
| - list(, $caller) = debug_backtrace(false); |
189 |
| - |
190 |
| - $name = snake_case($caller['function']); |
191 |
| - } |
192 |
| - |
193 |
| - list($type, $id) = $this->getMorphs($name, $type, $id); |
194 |
| - |
195 |
| - // If the type value is null it is probably safe to assume we're eager loading |
196 |
| - // the relationship. When that is the case we will pass in a dummy query as |
197 |
| - // there are multiple types in the morph and we can't use single queries. |
198 |
| - if (is_null($class = $this->$type)) |
199 |
| - { |
200 |
| - return new MorphTo( |
201 |
| - $this->newQuery(), $this, $id, null, $type, $name |
202 |
| - ); |
203 |
| - } |
204 |
| - |
205 |
| - // If we are not eager loading the relatinship, we will essentially treat this |
206 |
| - // as a belongs-to style relationship since morph-to extends that class and |
207 |
| - // we will pass in the appropriate values so that it behaves as expected. |
208 |
| - else |
209 |
| - { |
210 |
| - $instance = new $class; |
211 |
| - |
212 |
| - return new MorphTo( |
213 |
| - with($instance)->newQuery(), $this, $id, $instance->getKeyName(), $type, $name |
214 |
| - ); |
215 |
| - } |
216 |
| - } |
217 |
| - |
218 |
| - /** |
219 |
| - * Define a many-to-many relationship. |
220 |
| - * |
221 |
| - * @param string $related |
222 |
| - * @param string $collection |
223 |
| - * @param string $foreignKey |
224 |
| - * @param string $otherKey |
225 |
| - * @param string $relation |
226 |
| - * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
227 |
| - */ |
228 |
| - public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null, $relation = null) |
229 |
| - { |
230 |
| - // If no relationship name was passed, we will pull backtraces to get the |
231 |
| - // name of the calling function. We will use that function name as the |
232 |
| - // title of this relation since that is a great convention to apply. |
233 |
| - if (is_null($relation)) |
234 |
| - { |
235 |
| - $relation = $this->getBelongsToManyCaller(); |
236 |
| - } |
237 |
| - |
238 |
| - // Check if it is a relation with an original model. |
239 |
| - if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model')) |
240 |
| - { |
241 |
| - return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation); |
242 |
| - } |
243 |
| - |
244 |
| - // First, we'll need to determine the foreign key and "other key" for the |
245 |
| - // relationship. Once we have determined the keys we'll make the query |
246 |
| - // instances as well as the relationship instances we need for this. |
247 |
| - $foreignKey = $foreignKey ?: $this->getForeignKey() . 's'; |
248 |
| - |
249 |
| - $instance = new $related; |
250 |
| - |
251 |
| - $otherKey = $otherKey ?: $instance->getForeignKey() . 's'; |
252 |
| - |
253 |
| - // If no table name was provided, we can guess it by concatenating the two |
254 |
| - // models using underscores in alphabetical order. The two model names |
255 |
| - // are transformed to snake case from their default CamelCase also. |
256 |
| - if (is_null($collection)) |
257 |
| - { |
258 |
| - $collection = $instance->getTable(); |
259 |
| - } |
260 |
| - |
261 |
| - // Now we're ready to create a new query builder for the related model and |
262 |
| - // the relationship instances for the relation. The relations will set |
263 |
| - // appropriate query constraint and entirely manages the hydrations. |
264 |
| - $query = $instance->newQuery(); |
265 |
| - |
266 |
| - return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation); |
267 |
| - } |
268 |
| - |
269 |
| - /** |
270 |
| - * Get a new query builder instance for the connection. |
271 |
| - * |
272 |
| - * @return Builder |
273 |
| - */ |
274 |
| - protected function newBaseQueryBuilder() |
275 |
| - { |
276 |
| - $connection = $this->getConnection(); |
277 |
| - |
278 |
| - // Check the connection type |
279 |
| - if ($connection instanceof \Jenssegers\Mongodb\Connection) |
280 |
| - { |
281 |
| - return new QueryBuilder($connection, $connection->getPostProcessor()); |
282 |
| - } |
283 |
| - |
284 |
| - return parent::newBaseQueryBuilder(); |
285 |
| - } |
| 7 | + use HybridRelations; |
286 | 8 |
|
287 | 9 | }
|
0 commit comments