forked from illuminate/database
/
BelongsToMany.php
421 lines (355 loc) · 9.92 KB
/
BelongsToMany.php
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php namespace Illuminate\Database\Eloquent\Relations;
use DateTime;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
class BelongsToMany extends Relation {
/**
* The intermediate table for the relation.
*
* @var string
*/
protected $table;
/**
* The foreign key of the parent model.
*
* @var string
*/
protected $foreignKey;
/**
* The associated key of the relation.
*
* @var string
*/
protected $otherKey;
/**
* The pivot table columns to retrieve.
*
* @var array
*/
protected $pivotColumns = array();
/**
* Create a new has many relationship instance.
*
* @param Illuminate\Database\Eloquent\Builder $query
* @param Illuminate\Database\Eloquent\Model $parent
* @param string $table
* @param string $foreignKey
* @param string $otherKey
* @return void
*/
public function __construct(Builder $query, Model $parent, $table, $foreignKey, $otherKey)
{
$this->table = $table;
$this->otherKey = $otherKey;
$this->foreignKey = $foreignKey;
parent::__construct($query, $parent);
}
/**
* Attach a model to the parent.
*
* @param mixed $id
* @param array $attributes
* @return void
*/
public function attach($id, array $attributes = array())
{
$foreign = $this->foreignKey;
// When attaching models in a many to many relationship, we need to set the
// keys on the pivot table, including both the foreign key and the other
// associated keys before saving so it will automatically link models.
$query = $this->query->newQuery()->from($this->table);
$attributes[$foreign] = $this->parent->getKey();
$attributes[$this->otherKey] = $id;
// Since all Eloquent models are timestamped, we will set the creation and
// update timestamps on the pivot record so they will be initialized on
// the record like all of the other pivot table records on the table.
$attributes['created_at'] = new DateTime;
$attributes['updated_at'] = new DateTime;
return $query->insert($attributes);
}
/**
* Detach models from the relationship.
*
* @param int|array $ids
* @return int
*/
public function detach($ids = array())
{
$query = $this->query->newQuery()->from($this->table);
$query->where($this->foreignKey, '=', $this->parent->getKey());
// If associated IDs were passed to the method we will onyl delete those
// associations, otherwise all of the association ties will be broken.
// We'll return the numbers of affected rows when we do the deletes.
$ids = (array) $ids;
if (count($ids) > 0)
{
$query->whereIn($this->otherKey, $ids);
}
return $query->delete();
}
/**
* Set the base constraints on the relation query.
*
* @return void
*/
public function addConstraints()
{
$this->setSelect()->setJoin()->setWhere();
}
/**
* Get the results of the relationship.
*
* @return mixed
*/
public function getResults()
{
return $this->get();
}
/**
* Execute the query as a "select" statement.
*
* @param array $columns
* @return Illuminate\Database\Eloquent\Collection
*/
public function get($columns = array('*'))
{
$models = $this->query->getModels($columns);
$this->hydratePivotRelation($models);
// If we actually found models we will also eager load any relationships that
// have been specified as needing to be eager loaded. This will solve the
// n+1 query problem for the developers and also increase performance.
if (count($models) > 0)
{
$models = $this->query->eagerLoadRelations($models);
}
return new Collection($models);
}
/**
* Set the select clause for the relation query.
*
* @return Illuminate\Database\Eloquent\Relation\BelongsToMany
*/
protected function setSelect()
{
$columns = array($this->query->getModel()->getTable().'.*');
$this->query->select(array_merge($columns, $this->getPivotColumns()));
return $this;
}
/**
* Get the pivot columns for the relation.
*
* @return array
*/
protected function getPivotColumns()
{
$defaults = array('id', 'created_at', 'updated_at');
$pivot = array_merge($defaults, $this->pivotColumns);
// We need to alias all of the pivot columns with the "pivot_" prefix so we
// can easily extract them out of the models and put them into the pivot
// relationships when they are retrieved and hydrated into the models.
foreach ($pivot as $column)
{
$pivot[] = $this->table.'.'.$column.' as pivot_'.$column;
}
return $pivot;
}
/**
* Set the join clause for the relation query.
*
* @return Illuminate\Database\Eloquent\Relation\BelongsToMany
*/
protected function setJoin()
{
// We need to join to the intermediate table on the related model's primary
// key column with the intermediate tables foreign key for the related
// model instance. Then we can set the where for the parent model.
$baseTable = $this->query->getModel()->getTable();
$key = $baseTable.'.'.$this->query->getModel()->getKeyName();
$this->query->join($this->table, $key, '=', $this->getOtherKey());
return $this;
}
/**
* Set the where clause for the relation query.
*
* @return Illuminate\Database\Eloquent\Relation\BelongsToMany
*/
protected function setWhere()
{
$foreign = $this->getForeignKey();
$this->query->where($foreign, '=', $this->parent->getKey());
return $this;
}
/**
* Set the constraints for an eager load of the relation.
*
* @param array $models
* @return void
*/
public function addEagerConstraints(array $models)
{
$this->query->whereIn($this->getForeignKey(), $this->getKeys($models));
}
/**
* Initialize the relation on a set of models.
*
* @param array $models
* @param string $relation
* @return void
*/
public function initRelation(array $models, $relation)
{
foreach ($models as $model)
{
$model->setRelation($relation, new Collection);
}
return $models;
}
/**
* Match the eagerly loaded results to their parents.
*
* @param array $models
* @param Illuminate\Database\Eloquent\Collection $results
* @param string $relation
* @return array
*/
public function match(array $models, Collection $results, $relation)
{
// First we'll build a dictionary of child models keyed by the foreign key
// of the relation so that we can easily and quickly match them to the
// parents without having a possibly slow inner loops for each one.
$foreign = $this->foreignKey;
$dictionary = array();
foreach ($results as $result)
{
$dictionary[$result->pivot->$foreign][] = $result;
}
// Once we have a nice dictionary of child objects we can easily match the
// children back to their parents using the dictionary and the keys on
// the parent models. Then we will return the hydrated models back.
foreach ($models as $model)
{;
if (isset($dictionary[$key = $model->getKey()]))
{
$collection = new Collection($dictionary[$key]);
$model->setRelation($relation, $collection);
}
}
// Now that we should have the children models totally matched up to their
// parents, we can just return the array of models. Eager loading them
// helps the developer avoid tons of queries (n + 1 query problems).
return $models;
}
/**
* Hydrate the pivot table relationship on the models.
*
* @param array $models
* @return void
*/
protected function hydratePivotRelation(array $models)
{
// To hydrate the pivot relationship, we will just gather the pivot attributes
// and create a new Pivot model, which is basically a dynamic model that we
// will set the attributes, table, and connections on so it they be used.
foreach ($models as $model)
{
$values = $this->cleanPivotAttributes($model);
$pivot = $this->newExistingPivot($values);
$model->setRelation('pivot', $pivot);
}
}
/**
* Get the pivot attributes from a model.
*
* @param Illuminate\Database\Eloquent\Model $model
* @return array
*/
protected function cleanPivotAttributes(Model $model)
{
$values = array();
// To get the pivots attributes we will just take any of the attributes that
// begin with "pivot_" and add those to our arrays, as well as unsetting
// them from the parents models since they exist in a different table.
foreach ($model->getAttributes() as $key => $value)
{
if (strpos($key, 'pivot_') === 0)
{
$values[substr($key, 6)] = $value;
unset($model->$key);
}
}
return $values;
}
/**
* Create a new pivot model instance.
*
* @param array $attributes
* @param bool $exists
* @return Illuminate\Database\Eloquent\Relation\Pivot
*/
protected function newPivot(array $attributes = array(), $exists = false)
{
$connection = $this->parent->getConnectionName();
return new Pivot($attributes, $this->table, $connection, $exists);
}
/**
* Create a new existing pivot model instance.
*
* @param array $attributes
* @return Illuminate\Database\Eloquent\Relations\Pivot
*/
protected function newExistingPivot(array $attributes = array())
{
return $this->newPivot($attributes, true);
}
/**
* Set the columns on the pivot table to retrieve.
*
* @param array $columns
* @return Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function withPivot($columns)
{
$this->pivotColumns = $columns;
return $this->setSelect();
}
/**
* Get the fully qualified foreign key for the relation.
*
* @return string
*/
public function getForeignKey()
{
return $this->table.'.'.$this->foreignKey;
}
/**
* Get the fully qualified "other key" for the relation.
*
* @return string
*/
public function getOtherKey()
{
return $this->table.'.'.$this->otherKey;
}
/**
* Get both of the relation keys in an array.
*
* @return array
*/
protected function getBothKeys()
{
return array_map(function($value)
{
return $value.' as pivot_'.$value;
}, array($this->foreignKey, $this->otherKey));
}
/**
* Get the intermediate table for the relationship.
*
* @return string
*/
public function getTable()
{
return $this->table;
}
}