@@ -36,9 +36,7 @@ public function addConstraints()
36
36
{
37
37
if (static ::$ constraints )
38
38
{
39
- // Make sure that the primary key of the parent
40
- // is in the relationship array of keys
41
- $ this ->query ->whereIn ($ this ->foreignKey , array ($ this ->parent ->getKey ()));
39
+ $ this ->query ->where ($ this ->foreignKey , $ this ->parent ->getKey ());
42
40
}
43
41
}
44
42
@@ -114,26 +112,25 @@ public function attach($id, array $attributes = array(), $touch = true)
114
112
{
115
113
if ($ id instanceof Model) $ id = $ id ->getKey ();
116
114
117
- // Generate a new parent query instance
118
- $ parent = $ this ->newParentQuery ();
119
-
120
- // Generate a new related query instance
121
- $ related = $ this ->related ->newInstance ();
122
-
123
- // Set contraints on the related query
124
- $ related = $ related ->where ($ this ->related ->getKeyName (), $ id );
125
-
126
115
$ records = $ this ->createAttachRecords ((array ) $ id , $ attributes );
127
116
128
117
// Get the ID's to attach to the two documents
129
118
$ otherIds = array_pluck ($ records , $ this ->otherKey );
130
119
$ foreignIds = array_pluck ($ records , $ this ->foreignKey );
131
120
132
121
// Attach to the parent model
133
- $ parent ->push ($ this ->otherKey , $ otherIds [0 ]);
122
+ $ this ->parent ->push ($ this ->otherKey , $ otherIds [0 ]);
123
+
124
+ // Generate a new related query instance
125
+ $ query = $ this ->getNewRelatedQuery ();
126
+
127
+ // Set contraints on the related query
128
+ $ query ->where ($ this ->related ->getKeyName (), $ id );
134
129
135
130
// Attach to the related model
136
- $ related ->push ($ this ->foreignKey , $ foreignIds [0 ]);
131
+ $ query ->push ($ this ->foreignKey , $ foreignIds [0 ]);
132
+
133
+ if ($ touch ) $ this ->touchIfTouching ();
137
134
}
138
135
139
136
/**
@@ -168,47 +165,32 @@ public function detach($ids = array(), $touch = true)
168
165
{
169
166
if ($ ids instanceof Model) $ ids = (array ) $ ids ->getKey ();
170
167
171
- $ query = $ this ->newParentQuery ();
172
-
173
- // Generate a new related query instance
174
- $ related = $ this ->related ->newInstance ();
175
-
176
168
// If associated IDs were passed to the method we will only delete those
177
169
// associations, otherwise all of the association ties will be broken.
178
170
// We'll return the numbers of affected rows when we do the deletes.
179
171
$ ids = (array ) $ ids ;
180
172
181
- if (count ($ ids ) > 0 )
173
+ // Pull each id from the parent.
174
+ foreach ($ ids as $ id )
182
175
{
183
- $ query -> whereIn ($ this ->otherKey , $ ids );
176
+ $ this -> parent -> pull ($ this ->otherKey , $ id );
184
177
}
185
178
186
- if ($ touch ) $ this ->touchIfTouching ();
179
+ // Get a new related query.
180
+ $ query = $ this ->getNewRelatedQuery ();
187
181
188
- // Once we have all of the conditions set on the statement, we are ready
189
- // to run the delete on the pivot table. Then, if the touch parameter
190
- // is true, we will go ahead and touch all related models to sync.
191
- foreach ($ ids as $ id )
182
+ // Prepare the query to select all related objects.
183
+ if (count ($ ids ) > 0 )
192
184
{
193
- $ query ->pull ($ this ->otherKey , $ id );
185
+ $ query ->whereIn ($ this ->related -> getKeyName () , $ ids );
194
186
}
195
187
196
- // Remove the relation from the related model
197
- $ related ->pull ($ this ->foreignKey , $ this ->parent ->getKey ());
198
-
199
- return count ($ ids );
200
- }
188
+ // Remove the relation to the parent.
189
+ $ query ->pull ($ this ->foreignKey , $ this ->parent ->getKey ());
201
190
202
- /**
203
- * Create a new query builder for the parent
204
- *
205
- * @return Jenssegers\Mongodb\Builder
206
- */
207
- protected function newParentQuery ()
208
- {
209
- $ query = $ this ->parent ->newQuery ();
191
+ if ($ touch ) $ this ->touchIfTouching ();
210
192
211
- return $ query -> where ( $ this -> parent -> getKeyName (), ' = ' , $ this -> parent -> getKey () );
193
+ return count ( $ ids );
212
194
}
213
195
214
196
/**
@@ -237,6 +219,16 @@ protected function buildDictionary(Collection $results)
237
219
return $ dictionary ;
238
220
}
239
221
222
+ /**
223
+ * Get a new related query.
224
+ *
225
+ * @return \Illuminate\Database\Query\Builder
226
+ */
227
+ public function getNewRelatedQuery ()
228
+ {
229
+ return $ this ->related ->newQuery ();
230
+ }
231
+
240
232
/**
241
233
* Get the fully qualified foreign key for the relation.
242
234
*
0 commit comments