File tree Expand file tree Collapse file tree 2 files changed +45
-2
lines changed
src/Jenssegers/Mongodb/Relations Expand file tree Collapse file tree 2 files changed +45
-2
lines changed Original file line number Diff line number Diff line change @@ -290,14 +290,19 @@ public function createMany(array $records)
290
290
* @param array|int $ids
291
291
* @return int
292
292
*/
293
- public function destroy ($ ids )
293
+ public function destroy ($ ids = array () )
294
294
{
295
295
// We'll initialize a count here so we will return the total number of deletes
296
296
// for the operation. The developers can then check this number as a boolean
297
297
// type value or get this total count of records deleted for logging, etc.
298
298
$ count = 0 ;
299
299
300
- $ ids = is_array ($ ids ) ? $ ids : func_get_args ();
300
+ if ($ ids instanceof Model) $ ids = (array ) $ ids ->getKey ();
301
+
302
+ // If associated IDs were passed to the method we will only delete those
303
+ // associations, otherwise all of the association ties will be broken.
304
+ // We'll return the numbers of affected rows when we do the deletes.
305
+ $ ids = (array ) $ ids ;
301
306
302
307
// Get existing embedded documents.
303
308
$ documents = $ this ->getEmbedded ();
@@ -320,6 +325,28 @@ public function destroy($ids)
320
325
return $ count ;
321
326
}
322
327
328
+ /**
329
+ * Delete alias.
330
+ *
331
+ * @param int|array $ids
332
+ * @return int
333
+ */
334
+ public function detach ($ ids = array ())
335
+ {
336
+ return $ this ->destroy ($ ids );
337
+ }
338
+
339
+ /**
340
+ * Save alias.
341
+ *
342
+ * @param \Illuminate\Database\Eloquent\Model $model
343
+ * @return \Illuminate\Database\Eloquent\Model
344
+ */
345
+ public function attach (Model $ model )
346
+ {
347
+ return $ this ->save ($ model );
348
+ }
349
+
323
350
/**
324
351
* Get the embedded documents array
325
352
*
Original file line number Diff line number Diff line change @@ -342,6 +342,22 @@ public function testEmbedsManyDestroy()
342
342
$ address = $ user ->addresses ->first ();
343
343
$ user ->addresses ()->destroy ($ address ->_id );
344
344
$ this ->assertEquals (array ('Bristol ' , 'Bruxelles ' ), $ user ->addresses ->lists ('city ' ));
345
+
346
+ $ address = $ user ->addresses ->first ();
347
+ $ user ->addresses ()->destroy ($ address );
348
+ $ this ->assertEquals (array ('Bruxelles ' ), $ user ->addresses ->lists ('city ' ));
349
+ }
350
+
351
+ public function testEmbedsManyAliases ()
352
+ {
353
+ $ user = User::create (array ('name ' => 'John Doe ' ));
354
+ $ address = new Address (array ('city ' => 'London ' ));
355
+
356
+ $ address = $ user ->addresses ()->attach ($ address );
357
+ $ this ->assertEquals (array ('London ' ), $ user ->addresses ->lists ('city ' ));
358
+
359
+ $ user ->addresses ()->detach ($ address );
360
+ $ this ->assertEquals (array (), $ user ->addresses ->lists ('city ' ));
345
361
}
346
362
347
363
}
You can’t perform that action at this time.
0 commit comments