Commit 48ac180d authored by Jens Segers's avatar Jens Segers

Tweaked embedsMany with more push/pull

parent a8feeb78
...@@ -640,7 +640,7 @@ class Builder extends \Illuminate\Database\Query\Builder { ...@@ -640,7 +640,7 @@ class Builder extends \Illuminate\Database\Query\Builder {
* @param mixed $id * @param mixed $id
* @return mixed * @return mixed
*/ */
protected function convertKey($id) public function convertKey($id)
{ {
if (is_string($id) && strlen($id) === 24 && ctype_xdigit($id)) if (is_string($id) && strlen($id) === 24 && ctype_xdigit($id))
{ {
......
...@@ -55,6 +55,10 @@ class EmbedsMany extends Relation { ...@@ -55,6 +55,10 @@ class EmbedsMany extends Relation {
*/ */
public function addConstraints() public function addConstraints()
{ {
if (static::$constraints)
{
$this->query->where($this->parent->getKeyName(), '=', $this->parent->getKey());
}
} }
/** /**
...@@ -180,7 +184,7 @@ class EmbedsMany extends Relation { ...@@ -180,7 +184,7 @@ class EmbedsMany extends Relation {
$model->exists = true; $model->exists = true;
// Push the document to the database. // Push the document to the database.
$result = $this->parent->push($this->localKey, $model->getAttributes(), true); $result = $this->query->push($this->localKey, $model->getAttributes(), true);
// Get existing embedded documents. // Get existing embedded documents.
$documents = $this->getEmbedded(); $documents = $this->getEmbedded();
...@@ -209,13 +213,20 @@ class EmbedsMany extends Relation { ...@@ -209,13 +213,20 @@ class EmbedsMany extends Relation {
$model->setUpdatedAt($time); $model->setUpdatedAt($time);
} }
$key = $model->getKey(); // Convert the id to MongoId if necessary.
$id = $this->query->getQuery()->convertKey($model->getKey());
$primaryKey = $model->getKeyName(); // Update document in database.
$result = $this->query->where($this->localKey . '.' . $model->getKeyName(), $id)
->update(array($this->localKey . '.$' => $model->getAttributes()));
// Get existing embedded documents. // Get existing embedded documents.
$documents = $this->getEmbedded(); $documents = $this->getEmbedded();
$primaryKey = $this->related->getKeyName();
$key = $model->getKey();
// Replace the document in the parent model. // Replace the document in the parent model.
foreach ($documents as $i => $document) foreach ($documents as $i => $document)
{ {
...@@ -228,7 +239,7 @@ class EmbedsMany extends Relation { ...@@ -228,7 +239,7 @@ class EmbedsMany extends Relation {
$this->setEmbedded($documents); $this->setEmbedded($documents);
return $this->parent->save() ? $model : false; return $result ? $model : false;
} }
/** /**
...@@ -300,25 +311,33 @@ class EmbedsMany extends Relation { ...@@ -300,25 +311,33 @@ class EmbedsMany extends Relation {
// We'll return the numbers of affected rows when we do the deletes. // We'll return the numbers of affected rows when we do the deletes.
$ids = (array) $ids; $ids = (array) $ids;
$primaryKey = $this->related->getKeyName();
// Pull the documents from the database.
foreach ($ids as $id)
{
// Convert the id to MongoId if necessary.
$id = $this->query->getQuery()->convertKey($id);
$this->query->pull($this->localKey, array($primaryKey => $id));
$count++;
}
// Get existing embedded documents. // Get existing embedded documents.
$documents = $this->getEmbedded(); $documents = $this->getEmbedded();
$primaryKey = $this->related->getKeyName();
// Remove the document from the parent model. // Remove the document from the parent model.
foreach ($documents as $i => $document) foreach ($documents as $i => $document)
{ {
if (in_array($document[$primaryKey], $ids)) if (in_array($document[$primaryKey], $ids))
{ {
unset($documents[$i]); unset($documents[$i]);
$count++;
} }
} }
$this->setEmbedded($documents); $this->setEmbedded($documents);
$this->parent->save();
return $count; return $count;
} }
......
...@@ -18,7 +18,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -18,7 +18,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
Photo::truncate(); Photo::truncate();
} }
/*public function testHasMany() public function testHasMany()
{ {
$author = User::create(array('name' => 'George R. R. Martin')); $author = User::create(array('name' => 'George R. R. Martin'));
Book::create(array('title' => 'A Game of Thrones', 'author_id' => $author->_id)); Book::create(array('title' => 'A Game of Thrones', 'author_id' => $author->_id));
...@@ -281,7 +281,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -281,7 +281,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$photo = Photo::first(); $photo = Photo::first();
$this->assertEquals($photo->imageable->name, $user->name); $this->assertEquals($photo->imageable->name, $user->name);
}*/ }
public function testEmbedsManySave() public function testEmbedsManySave()
{ {
...@@ -296,18 +296,21 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -296,18 +296,21 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$address = $user->addresses()->save(new Address(array('city' => 'Paris'))); $address = $user->addresses()->save(new Address(array('city' => 'Paris')));
$freshUser = User::find($user->_id); $user = User::find($user->_id);
$this->assertEquals(array('London', 'Paris'), $freshUser->addresses->lists('city')); $this->assertEquals(array('London', 'Paris'), $user->addresses->lists('city'));
$address->city = 'New York'; $address->city = 'New York';
$freshUser->addresses()->save($address); $user->addresses()->save($address);
$this->assertEquals(2, count($freshUser->addresses)); $this->assertEquals(2, count($user->addresses));
$this->assertEquals(2, count($freshUser->addresses()->get())); $this->assertEquals(2, count($user->addresses()->get()));
$this->assertEquals(2, $freshUser->addresses->count()); $this->assertEquals(2, $user->addresses->count());
$this->assertEquals(array('London', 'New York'), $user->addresses->lists('city'));
$freshUser = User::find($user->_id);
$this->assertEquals(array('London', 'New York'), $freshUser->addresses->lists('city')); $this->assertEquals(array('London', 'New York'), $freshUser->addresses->lists('city'));
$address = $freshUser->addresses->first(); $address = $user->addresses->first();
$this->assertEquals('London', $address->city); $this->assertEquals('London', $address->city);
$this->assertInstanceOf('DateTime', $address->created_at); $this->assertInstanceOf('DateTime', $address->created_at);
$this->assertInstanceOf('DateTime', $address->updated_at); $this->assertInstanceOf('DateTime', $address->updated_at);
......
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