Skip to content

Commit a86d16a

Browse files
Add method dissociate to EmbedsMany and refactor destroy
1 parent cbb5273 commit a86d16a

File tree

2 files changed

+57
-16
lines changed

2 files changed

+57
-16
lines changed

src/Jenssegers/Mongodb/Relations/EmbedsMany.php

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -273,32 +273,36 @@ public function createMany(array $records)
273273
/**
274274
* Destroy the embedded models for the given IDs.
275275
*
276-
* @param array|int $ids
276+
* @param mixed $ids
277277
* @return int
278278
*/
279279
public function destroy($ids = array())
280280
{
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);
292282

293283
$primaryKey = $this->related->getKeyName();
294284

295285
// Pull the documents from the database.
296286
foreach ($ids as $id)
297287
{
298288
$this->query->pull($this->localKey, array($primaryKey => $this->getForeignKeyValue($id)));
299-
$count++;
300289
}
301290

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+
302306
// Get existing embedded documents.
303307
$documents = $this->getEmbeddedRecords();
304308

@@ -313,7 +317,28 @@ public function destroy($ids = array())
313317

314318
$this->setEmbeddedRecords($documents);
315319

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;
317342
}
318343

319344
/**

tests/RelationsTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,15 +383,31 @@ public function testEmbedsManyDestroy()
383383
$user->addresses()->create(array('city' => 'Paris'));
384384
$user->addresses()->create(array('city' => 'San Francisco'));
385385

386-
$user = User::find($user->id);
387-
$this->assertEquals(array('Bruxelles', 'Paris', 'San Francisco'), $user->addresses->lists('city'));
386+
$freshUser = User::find($user->id);
387+
$this->assertEquals(array('Bruxelles', 'Paris', 'San Francisco'), $freshUser->addresses->lists('city'));
388388

389389
$ids = $user->addresses->lists('_id');
390390
$user->addresses()->destroy($ids);
391391
$this->assertEquals(array(), $user->addresses->lists('city'));
392392

393393
$freshUser = User::find($user->id);
394394
$this->assertEquals(array(), $freshUser->addresses->lists('city'));
395+
396+
list($london, $bristol, $bruxelles) = $user->addresses()->saveMany(array(new Address(array('city' => 'London')), new Address(array('city' => 'Bristol')), new Address(array('city' => 'Bruxelles'))));
397+
$user->addresses()->destroy(array($london, $bruxelles));
398+
$this->assertEquals(array('Bristol'), $user->addresses->lists('city'));
399+
}
400+
401+
public function testEmbedsManyDissociate()
402+
{
403+
$user = User::create(array());
404+
$cordoba = $user->addresses()->create(array('city' => 'Cordoba'));
405+
406+
$user->addresses()->dissociate($cordoba->id);
407+
408+
$freshUser = User::find($user->id);
409+
$this->assertEquals(0, $user->addresses->count());
410+
$this->assertEquals(1, $freshUser->addresses->count());
395411
}
396412

397413
public function testEmbedsManyAliases()

0 commit comments

Comments
 (0)