11<?php namespace Jenssegers \Mongodb \Relations ;
2+
23use Illuminate \Database \Eloquent \Collection ;
34use Illuminate \Database \Eloquent \Builder ;
45use Illuminate \Database \Eloquent \Model ;
78class BelongsToMany extends EloquentBelongsToMany {
89
910 /**
10- * Execute the query as a "select" statement .
11+ * Hydrate the pivot table relationship on the models .
1112 *
12- * @param array $columns
13- * @return \Illuminate\Database\Eloquent\Collection
13+ * @param array $models
14+ * @return void
1415 */
15- public function get ( $ columns = array ( ' * ' ) )
16+ protected function hydratePivotRelation ( array $ models )
1617 {
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
3319 }
34-
20+
3521 /**
3622 * Set the select clause for the relation query.
3723 *
@@ -42,90 +28,21 @@ protected function getSelectColumns(array $columns = array('*'))
4228 return $ columns ;
4329 }
4430
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-
6531 /**
6632 * Set the base constraints on the relation query.
6733 *
6834 * @return void
6935 */
7036 public function addConstraints ()
7137 {
72- if (static ::$ constraints )
38+ if (static ::$ constraints )
7339 {
7440 // Make sure that the primary key of the parent
7541 // is in the relationship array of keys
7642 $ this ->query ->whereIn ($ this ->foreignKey , array ($ this ->parent ->getKey ()));
7743 }
7844 }
7945
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-
12946 /**
13047 * Sync the intermediate tables with a list of IDs.
13148 *
@@ -139,13 +56,13 @@ public function sync(array $ids, $detaching = true)
13956 // in this joining table. We'll spin through the given IDs, checking to see
14057 // if they exist in the array of current ones, and if not we will insert.
14158 $ current = $ this ->parent ->{$ this ->otherKey };
142-
59+
14360 // Check if the current array exists or not on the parent model and create it
14461 // if it does not exist
14562 if (is_null ($ current )) $ current = array ();
14663
14764 $ records = $ this ->formatSyncList ($ ids );
148-
65+
14966 $ detach = array_diff ($ current , array_keys ($ records ));
15067
15168 // 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)
16481 $ this ->touchIfTouching ();
16582 }
16683
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-
19084 /**
19185 * Attach all of the IDs that aren't in the current array.
19286 *
@@ -220,25 +114,25 @@ protected function attachNew(array $records, array $current, $touch = true)
220114 public function attach ($ id , array $ attributes = array (), $ touch = true )
221115 {
222116 if ($ id instanceof Model) $ id = $ id ->getKey ();
223-
117+
224118 // Generate a new parent query instance
225119 $ parent = $ this ->newParentQuery ();
226-
120+
227121 // Generate a new related query instance
228122 $ related = $ this ->related ->newInstance ();
229-
123+
230124 // Set contraints on the related query
231125 $ related = $ related ->where ($ this ->related ->getKeyName (), $ id );
232126
233127 $ records = $ this ->createAttachRecords ((array ) $ id , $ attributes );
234-
128+
235129 // Get the ID's to attach to the two documents
236130 $ otherIds = array_pluck ($ records , $ this ->otherKey );
237131 $ foreignIds = array_pluck ($ records , $ this ->foreignKey );
238132
239133 // Attach to the parent model
240134 $ parent ->push ($ this ->otherKey , $ otherIds [0 ])->update (array ());
241-
135+
242136 // Attach to the related model
243137 $ related ->push ($ this ->foreignKey , $ foreignIds [0 ])->update (array ());
244138 }
@@ -296,54 +190,22 @@ public function detach($ids = array(), $touch = true)
296190 {
297191 $ query ->pull ($ this ->otherKey , $ id );
298192 }
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 ();
311193
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 );
333195 }
334196
335197 /**
336198 * Create a new query builder for the parent
337- *
199+ *
338200 * @return Jenssegers\Mongodb\Builder
339201 */
340202 protected function newParentQuery ()
341203 {
342204 $ query = $ this ->parent ->newQuery ();
343-
205+
344206 return $ query ->where ($ this ->parent ->getKeyName (), '= ' , $ this ->parent ->getKey ());
345207 }
346-
208+
347209 /**
348210 * Build model dictionary keyed by the relation's foreign key.
349211 *
@@ -370,16 +232,6 @@ protected function buildDictionary(Collection $results)
370232 return $ dictionary ;
371233 }
372234
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-
383235 /**
384236 * Get the fully qualified foreign key for the relation.
385237 *
@@ -399,4 +251,4 @@ public function getOtherKey()
399251 {
400252 return $ this ->otherKey ;
401253 }
402- }
254+ }
0 commit comments