Commit b2e0fd72 authored by Jens Segers's avatar Jens Segers

Adding some alias methods for embedsMany

parent de3ffe0d
......@@ -290,14 +290,19 @@ class EmbedsMany extends Relation {
* @param array|int $ids
* @return int
*/
public function destroy($ids)
public function destroy($ids = array())
{
// We'll initialize a count here so we will return the total number of deletes
// for the operation. The developers can then check this number as a boolean
// type value or get this total count of records deleted for logging, etc.
$count = 0;
$ids = is_array($ids) ? $ids : func_get_args();
if ($ids instanceof Model) $ids = (array) $ids->getKey();
// If associated IDs were passed to the method we will only delete those
// associations, otherwise all of the association ties will be broken.
// We'll return the numbers of affected rows when we do the deletes.
$ids = (array) $ids;
// Get existing embedded documents.
$documents = $this->getEmbedded();
......@@ -320,6 +325,28 @@ class EmbedsMany extends Relation {
return $count;
}
/**
* Delete alias.
*
* @param int|array $ids
* @return int
*/
public function detach($ids = array())
{
return $this->destroy($ids);
}
/**
* Save alias.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model
*/
public function attach(Model $model)
{
return $this->save($model);
}
/**
* Get the embedded documents array
*
......
......@@ -342,6 +342,22 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$address = $user->addresses->first();
$user->addresses()->destroy($address->_id);
$this->assertEquals(array('Bristol', 'Bruxelles'), $user->addresses->lists('city'));
$address = $user->addresses->first();
$user->addresses()->destroy($address);
$this->assertEquals(array('Bruxelles'), $user->addresses->lists('city'));
}
public function testEmbedsManyAliases()
{
$user = User::create(array('name' => 'John Doe'));
$address = new Address(array('city' => 'London'));
$address = $user->addresses()->attach($address);
$this->assertEquals(array('London'), $user->addresses->lists('city'));
$user->addresses()->detach($address);
$this->assertEquals(array(), $user->addresses->lists('city'));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment