1
1
<?php namespace Jenssegers \Mongodb \Relations ;
2
+
2
3
use Illuminate \Database \Eloquent \Collection ;
3
4
use Illuminate \Database \Eloquent \Builder ;
4
5
use Illuminate \Database \Eloquent \Model ;
7
8
class BelongsToMany extends EloquentBelongsToMany {
8
9
9
10
/**
10
- * Execute the query as a "select" statement .
11
+ * Hydrate the pivot table relationship on the models .
11
12
*
12
- * @param array $columns
13
- * @return \Illuminate\Database\Eloquent\Collection
13
+ * @param array $models
14
+ * @return void
14
15
*/
15
- public function get ( $ columns = array ( ' * ' ) )
16
+ protected function hydratePivotRelation ( array $ models )
16
17
{
17
- // First we'll add the proper select columns onto the query so it is run with
18
- // the proper columns. Then, we will get the results and hydrate out pivot
19
- // models with the result of those columns as a separate model relation.
20
- $ select = $ this ->getSelectColumns ($ columns );
21
-
22
- $ models = $ this ->query ->addSelect ($ select )->getModels ();
23
-
24
- // If we actually found models we will also eager load any relationships that
25
- // have been specified as needing to be eager loaded. This will solve the
26
- // n + 1 query problem for the developer and also increase performance.
27
- if (count ($ models ) > 0 )
28
- {
29
- $ models = $ this ->query ->eagerLoadRelations ($ models );
30
- }
31
-
32
- return $ this ->related ->newCollection ($ models );
18
+ // Do nothing
33
19
}
34
-
20
+
35
21
/**
36
22
* Set the select clause for the relation query.
37
23
*
@@ -42,90 +28,21 @@ protected function getSelectColumns(array $columns = array('*'))
42
28
return $ columns ;
43
29
}
44
30
45
- /**
46
- * Get a paginator for the "select" statement.
47
- *
48
- * @param int $perPage
49
- * @param array $columns
50
- * @return \Illuminate\Pagination\Paginator
51
- */
52
- public function paginate ($ perPage = null , $ columns = array ('* ' ))
53
- {
54
- $ this ->query ->addSelect ($ this ->getSelectColumns ($ columns ));
55
-
56
- // When paginating results, we need to add the pivot columns to the query and
57
- // then hydrate into the pivot objects once the results have been gathered
58
- // from the database since this isn't performed by the Eloquent builder.
59
- $ pager = $ this ->query ->paginate ($ perPage , $ columns );
60
-
61
- return $ pager ;
62
- }
63
-
64
-
65
31
/**
66
32
* Set the base constraints on the relation query.
67
33
*
68
34
* @return void
69
35
*/
70
36
public function addConstraints ()
71
37
{
72
- if (static ::$ constraints )
38
+ if (static ::$ constraints )
73
39
{
74
40
// Make sure that the primary key of the parent
75
41
// is in the relationship array of keys
76
42
$ this ->query ->whereIn ($ this ->foreignKey , array ($ this ->parent ->getKey ()));
77
43
}
78
44
}
79
45
80
- /**
81
- * Set the constraints for an eager load of the relation.
82
- *
83
- * @param array $models
84
- * @return void
85
- */
86
- public function addEagerConstraints (array $ models )
87
- {
88
- $ this ->query ->whereIn ($ this ->getForeignKey (), $ this ->getKeys ($ models ));
89
- }
90
-
91
- /**
92
- * Save a new model and attach it to the parent model.
93
- *
94
- * @param \Illuminate\Database\Eloquent\Model $model
95
- * @param array $joining
96
- * @param bool $touch
97
- * @return \Illuminate\Database\Eloquent\Model
98
- */
99
- public function save (Model $ model , array $ joining = array (), $ touch = true )
100
- {
101
- $ model ->save (array ('touch ' => false ));
102
-
103
- $ this ->attach ($ model ->getKey (), $ joining , $ touch );
104
-
105
- return $ model ;
106
- }
107
-
108
- /**
109
- * Create a new instance of the related model.
110
- *
111
- * @param array $attributes
112
- * @param array $joining
113
- * @param bool $touch
114
- * @return \Illuminate\Database\Eloquent\Model
115
- */
116
- public function create (array $ attributes , array $ joining = array (), $ touch = true )
117
- {
118
- $ instance = $ this ->related ->newInstance ($ attributes );
119
-
120
- // Save the new instance before we attach it to other models
121
- $ instance ->save (array ('touch ' => false ));
122
-
123
- // Attach to the parent instance
124
- $ this ->attach ($ instance ->_id , $ attributes , $ touch );
125
-
126
- return $ instance ;
127
- }
128
-
129
46
/**
130
47
* Sync the intermediate tables with a list of IDs.
131
48
*
@@ -139,13 +56,13 @@ public function sync(array $ids, $detaching = true)
139
56
// in this joining table. We'll spin through the given IDs, checking to see
140
57
// if they exist in the array of current ones, and if not we will insert.
141
58
$ current = $ this ->parent ->{$ this ->otherKey };
142
-
59
+
143
60
// Check if the current array exists or not on the parent model and create it
144
61
// if it does not exist
145
62
if (is_null ($ current )) $ current = array ();
146
63
147
64
$ records = $ this ->formatSyncList ($ ids );
148
-
65
+
149
66
$ detach = array_diff ($ current , array_keys ($ records ));
150
67
151
68
// Next, we will take the differences of the currents and given IDs and detach
@@ -164,29 +81,6 @@ public function sync(array $ids, $detaching = true)
164
81
$ this ->touchIfTouching ();
165
82
}
166
83
167
- /**
168
- * Format the sync list so that it is keyed by ID.
169
- *
170
- * @param array $records
171
- * @return array
172
- */
173
- protected function formatSyncList (array $ records )
174
- {
175
- $ results = array ();
176
-
177
- foreach ($ records as $ id => $ attributes )
178
- {
179
- if ( ! is_array ($ attributes ))
180
- {
181
- list ($ id , $ attributes ) = array ($ attributes , array ());
182
- }
183
-
184
- $ results [$ id ] = $ attributes ;
185
- }
186
-
187
- return $ results ;
188
- }
189
-
190
84
/**
191
85
* Attach all of the IDs that aren't in the current array.
192
86
*
@@ -220,25 +114,25 @@ protected function attachNew(array $records, array $current, $touch = true)
220
114
public function attach ($ id , array $ attributes = array (), $ touch = true )
221
115
{
222
116
if ($ id instanceof Model) $ id = $ id ->getKey ();
223
-
117
+
224
118
// Generate a new parent query instance
225
119
$ parent = $ this ->newParentQuery ();
226
-
120
+
227
121
// Generate a new related query instance
228
122
$ related = $ this ->related ->newInstance ();
229
-
123
+
230
124
// Set contraints on the related query
231
125
$ related = $ related ->where ($ this ->related ->getKeyName (), $ id );
232
126
233
127
$ records = $ this ->createAttachRecords ((array ) $ id , $ attributes );
234
-
128
+
235
129
// Get the ID's to attach to the two documents
236
130
$ otherIds = array_pluck ($ records , $ this ->otherKey );
237
131
$ foreignIds = array_pluck ($ records , $ this ->foreignKey );
238
132
239
133
// Attach to the parent model
240
134
$ parent ->push ($ this ->otherKey , $ otherIds [0 ])->update (array ());
241
-
135
+
242
136
// Attach to the related model
243
137
$ related ->push ($ this ->foreignKey , $ foreignIds [0 ])->update (array ());
244
138
}
@@ -296,54 +190,22 @@ public function detach($ids = array(), $touch = true)
296
190
{
297
191
$ query ->pull ($ this ->otherKey , $ id );
298
192
}
299
-
300
- return count ($ ids );
301
- }
302
-
303
- /**
304
- * If we're touching the parent model, touch.
305
- *
306
- * @return void
307
- */
308
- public function touchIfTouching ()
309
- {
310
- if ($ this ->touchingParent ()) $ this ->getParent ()->touch ();
311
193
312
- if ($ this ->getParent ()->touches ($ this ->relationName )) $ this ->touch ();
313
- }
314
-
315
- /**
316
- * Determine if we should touch the parent on sync.
317
- *
318
- * @return bool
319
- */
320
- protected function touchingParent ()
321
- {
322
- return $ this ->getRelated ()->touches ($ this ->guessInverseRelation ());
323
- }
324
-
325
- /**
326
- * Attempt to guess the name of the inverse of the relation.
327
- *
328
- * @return string
329
- */
330
- protected function guessInverseRelation ()
331
- {
332
- return strtolower (str_plural (class_basename ($ this ->getParent ())));
194
+ return count ($ ids );
333
195
}
334
196
335
197
/**
336
198
* Create a new query builder for the parent
337
- *
199
+ *
338
200
* @return Jenssegers\Mongodb\Builder
339
201
*/
340
202
protected function newParentQuery ()
341
203
{
342
204
$ query = $ this ->parent ->newQuery ();
343
-
205
+
344
206
return $ query ->where ($ this ->parent ->getKeyName (), '= ' , $ this ->parent ->getKey ());
345
207
}
346
-
208
+
347
209
/**
348
210
* Build model dictionary keyed by the relation's foreign key.
349
211
*
@@ -370,16 +232,6 @@ protected function buildDictionary(Collection $results)
370
232
return $ dictionary ;
371
233
}
372
234
373
- /**
374
- * Get the related model's updated at column name.
375
- *
376
- * @return string
377
- */
378
- public function getRelatedFreshUpdate ()
379
- {
380
- return array ($ this ->related ->getUpdatedAtColumn () => $ this ->related ->freshTimestamp ());
381
- }
382
-
383
235
/**
384
236
* Get the fully qualified foreign key for the relation.
385
237
*
@@ -399,4 +251,4 @@ public function getOtherKey()
399
251
{
400
252
return $ this ->otherKey ;
401
253
}
402
- }
254
+ }
0 commit comments