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