1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
<?php namespace Illuminate\Database\Eloquent\Relations;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection;
abstract class HasOneOrMany extends Relation {
/** * The foreign key of the parent model. * * @var string */ protected $foreignKey;
/** * The local key of the parent model. * * @var string */ protected $localKey;
/** * Create a new has many relationship instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $foreignKey * @return void */ public function __construct(Builder $query, Model $parent, $foreignKey, $localKey) { $this->localKey = $localKey; $this->foreignKey = $foreignKey;
parent::__construct($query, $parent); }
/** * Set the base constraints on the relation query. * * @return void */ public function addConstraints() { if (static::$constraints) { $this->query->where($this->foreignKey, '=', $this->getParentKey()); } }
/** * Set the constraints for an eager load of the relation. * * @param array $models * @return void */ public function addEagerConstraints(array $models) { $this->query->whereIn($this->foreignKey, $this->getKeys($models, $this->localKey)); }
/** * Match the eagerly loaded results to their single parents. * * @param array $models * @param \Illuminate\Database\Eloquent\Collection $results * @param string $relation * @return array */ public function matchOne(array $models, Collection $results, $relation) { return $this->matchOneOrMany($models, $results, $relation, 'one'); }
/** * Match the eagerly loaded results to their many parents. * * @param array $models * @param \Illuminate\Database\Eloquent\Collection $results * @param string $relation * @return array */ public function matchMany(array $models, Collection $results, $relation) { return $this->matchOneOrMany($models, $results, $relation, 'many'); }
/** * Match the eagerly loaded results to their many parents. * * @param array $models * @param \Illuminate\Database\Eloquent\Collection $results * @param string $relation * @param string $type * @return array */ protected function matchOneOrMany(array $models, Collection $results, $relation, $type) { $dictionary = $this->buildDictionary($results);
// Once we have the dictionary we can simply spin through the parent models to // link them up with their children using the keyed dictionary to make the // matching very convenient and easy work. Then we'll just return them. foreach ($models as $model) { $key = $model->getAttribute($this->localKey);
if (isset($dictionary[$key])) { $value = $this->getRelationValue($dictionary, $key, $type);
$model->setRelation($relation, $value); } }
return $models; }
/** * Get the value of a relationship by one or many type. * * @param array $dictionary * @param string $key * @param string $type * @return mixed */ protected function getRelationValue(array $dictionary, $key, $type) { $value = $dictionary[$key];
return $type == 'one' ? reset($value) : $this->related->newCollection($value); }
/** * Build model dictionary keyed by the relation's foreign key. * * @param \Illuminate\Database\Eloquent\Collection $results * @return array */ protected function buildDictionary(Collection $results) { $dictionary = array();
$foreign = $this->getPlainForeignKey();
// First we will create a dictionary of models keyed by the foreign key of the // relationship as this will allow us to quickly access all of the related // models without having to do nested looping which will be quite slow. foreach ($results as $result) { $dictionary[$result->{$foreign}][] = $result; }
return $dictionary; }
/** * Attach a model instance to the parent model. * * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Database\Eloquent\Model */ public function save(Model $model) { $model->setAttribute($this->getPlainForeignKey(), $this->getParentKey());
return $model->save() ? $model : false; }
/** * Attach an array of models to the parent instance. * * @param array $models * @return array */ public function saveMany(array $models) { array_walk($models, array($this, 'save'));
return $models; }
/** * Create a new instance of the related model. * * @param array $attributes * @return \Illuminate\Database\Eloquent\Model */ public function create(array $attributes) { $foreign = array( $this->getPlainForeignKey() => $this->getParentKey(), );
// Here we will set the raw attributes to avoid hitting the "fill" method so // that we do not have to worry about a mass accessor rules blocking sets // on the models. Otherwise, some of these attributes will not get set. $instance = $this->related->newInstance();
$instance->setRawAttributes(array_merge($attributes, $foreign));
$instance->save();
return $instance; }
/** * Create an array of new instances of the related model. * * @param array $records * @return array */ public function createMany(array $records) { $instances = array();
foreach ($records as $record) { $instances[] = $this->create($record); }
return $instances; }
/** * Perform an update on all the related models. * * @param array $attributes * @return int */ public function update(array $attributes) { if ($this->related->usesTimestamps()) { $attributes[$this->relatedUpdatedAt()] = $this->related->freshTimestamp(); }
return $this->query->update($attributes); }
/** * Get the key for comparing against the parent key in "has" query. * * @return string */ public function getHasCompareKey() { return $this->getForeignKey(); }
/** * Get the foreign key for the relationship. * * @return string */ public function getForeignKey() { return $this->foreignKey; }
/** * Get the plain foreign key. * * @return string */ public function getPlainForeignKey() { $segments = explode('.', $this->getForeignKey());
return $segments[count($segments) - 1]; }
/** * Get the key value of the paren's local key. * * @return mixed */ public function getParentKey() { return $this->parent->getAttribute($this->localKey); }
/** * Get the fully qualified parent key name. * * @return string */ public function getQualifiedParentKeyName() { return $this->parent->getTable().'.'.$this->localKey; }
}
|