@@ -273,32 +273,36 @@ public function createMany(array $records)
273
273
/**
274
274
* Destroy the embedded models for the given IDs.
275
275
*
276
- * @param array|int $ids
276
+ * @param mixed $ids
277
277
* @return int
278
278
*/
279
279
public function destroy ($ ids = array ())
280
280
{
281
- // We'll initialize a count here so we will return the total number of deletes
282
- // for the operation. The developers can then check this number as a boolean
283
- // type value or get this total count of records deleted for logging, etc.
284
- $ count = 0 ;
285
-
286
- if ($ ids instanceof Model) $ ids = (array ) $ ids ->getKey ();
287
-
288
- // If associated IDs were passed to the method we will only delete those
289
- // associations, otherwise all of the association ties will be broken.
290
- // We'll return the numbers of affected rows when we do the deletes.
291
- $ ids = (array ) $ ids ;
281
+ $ ids = $ this ->getIdsArrayFrom ($ ids );
292
282
293
283
$ primaryKey = $ this ->related ->getKeyName ();
294
284
295
285
// Pull the documents from the database.
296
286
foreach ($ ids as $ id )
297
287
{
298
288
$ this ->query ->pull ($ this ->localKey , array ($ primaryKey => $ this ->getForeignKeyValue ($ id )));
299
- $ count ++;
300
289
}
301
290
291
+ return $ this ->dissociate ($ ids );
292
+ }
293
+
294
+ /**
295
+ * Dissociate the embedded models for the given IDs without persistence.
296
+ *
297
+ * @param mixed $ids
298
+ * @return int
299
+ */
300
+ public function dissociate ($ ids = array ())
301
+ {
302
+ $ ids = $ this ->getIdsArrayFrom ($ ids );
303
+
304
+ $ primaryKey = $ this ->related ->getKeyName ();
305
+
302
306
// Get existing embedded documents.
303
307
$ documents = $ this ->getEmbeddedRecords ();
304
308
@@ -313,7 +317,28 @@ public function destroy($ids = array())
313
317
314
318
$ this ->setEmbeddedRecords ($ documents );
315
319
316
- return $ count ;
320
+ // We return the total number of deletes for the operation. The developers
321
+ // can then check this number as a boolean type value or get this total count
322
+ // of records deleted for logging, etc.
323
+ return count ($ ids );
324
+ }
325
+
326
+ /**
327
+ * Transform single ID, single Model or array of Models into an array of IDs
328
+ *
329
+ * @param mixed $ids
330
+ * @return int
331
+ */
332
+ protected function getIdsArrayFrom ($ ids )
333
+ {
334
+ if (! is_array ($ ids )) $ ids = array ($ ids );
335
+
336
+ foreach ($ ids as &$ id )
337
+ {
338
+ if ($ id instanceof Model) $ id = $ id ->getKey ();
339
+ }
340
+
341
+ return $ ids ;
317
342
}
318
343
319
344
/**
0 commit comments