Commit 0caeeb9f authored by Alexandre Butynski's avatar Alexandre Butynski

Add the associate method to EmbedsMany and refactoring

parent a86d16a3
...@@ -140,6 +140,8 @@ class EmbedsMany extends Relation { ...@@ -140,6 +140,8 @@ class EmbedsMany extends Relation {
*/ */
public function save(Model $model) public function save(Model $model)
{ {
$this->updateTimestamps($model);
// Insert a new document. // Insert a new document.
if ( ! $model->exists) if ( ! $model->exists)
{ {
...@@ -154,34 +156,40 @@ class EmbedsMany extends Relation { ...@@ -154,34 +156,40 @@ class EmbedsMany extends Relation {
} }
/** /**
* Perform a model insert operation. * Attach a model instance to the parent model without persistence.
* *
* @param \Illuminate\Database\Eloquent\Model $model * @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model * @return \Illuminate\Database\Eloquent\Model
*/ */
protected function performInsert(Model $model) public function associate(Model $model)
{ {
// Create a new key. // Insert the related model in the parent instance
if ( ! $model->getAttribute('_id')) if ( ! $model->exists)
{ {
$model->setAttribute('_id', new MongoId); return $this->associateNew($model);
} }
// Update timestamps. // Update the related model in the parent instance
$this->updateTimestamps($model); else
{
// Push the document to the database. return $this->associateExisting($model);
$result = $this->query->push($this->localKey, $model->getAttributes(), true); }
$documents = $this->getEmbeddedRecords();
// Add the document to the parent model. }
$documents[] = $model->getAttributes();
$this->setEmbeddedRecords($documents); /**
* Perform a model insert operation.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model
*/
protected function performInsert(Model $model)
{
// Insert the related model in the parent instance
$this->associateNew($model);
// Mark the model as existing. // Push the document to the database.
$model->exists = true; $result = $this->query->push($this->localKey, $model->getAttributes(), true);
return $result ? $model : false; return $result ? $model : false;
} }
...@@ -194,8 +202,8 @@ class EmbedsMany extends Relation { ...@@ -194,8 +202,8 @@ class EmbedsMany extends Relation {
*/ */
protected function performUpdate(Model $model) protected function performUpdate(Model $model)
{ {
// Update timestamps. // Update the related model in the parent instance
$this->updateTimestamps($model); $this->associateExisting($model);
// Get the correct foreign key value. // Get the correct foreign key value.
$id = $this->getForeignKeyValue($model->getKey()); $id = $this->getForeignKeyValue($model->getKey());
...@@ -204,6 +212,44 @@ class EmbedsMany extends Relation { ...@@ -204,6 +212,44 @@ class EmbedsMany extends Relation {
$result = $this->query->where($this->localKey . '.' . $model->getKeyName(), $id) $result = $this->query->where($this->localKey . '.' . $model->getKeyName(), $id)
->update(array($this->localKey . '.$' => $model->getAttributes())); ->update(array($this->localKey . '.$' => $model->getAttributes()));
return $result ? $model : false;
}
/**
* Attach a new model without persistence
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model
*/
protected function associateNew($model)
{
// Create a new key.
if ( ! $model->getAttribute('_id'))
{
$model->setAttribute('_id', new MongoId);
}
$documents = $this->getEmbeddedRecords();
// Add the document to the parent model.
$documents[] = $model->getAttributes();
$this->setEmbeddedRecords($documents);
// Mark the model as existing.
$model->exists = true;
return $model;
}
/**
* Update an existing model without persistence
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Model
*/
protected function associateExisting($model)
{
// Get existing embedded documents. // Get existing embedded documents.
$documents = $this->getEmbeddedRecords(); $documents = $this->getEmbeddedRecords();
...@@ -212,18 +258,18 @@ class EmbedsMany extends Relation { ...@@ -212,18 +258,18 @@ class EmbedsMany extends Relation {
$key = $model->getKey(); $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 &$document)
{ {
if ($document[$primaryKey] == $key) if ($document[$primaryKey] == $key)
{ {
$documents[$i] = $model->getAttributes(); $document = $model->getAttributes();
break; break;
} }
} }
$this->setEmbeddedRecords($documents); $this->setEmbeddedRecords($documents);
return $result ? $model : false; return $model;
} }
/** /**
......
...@@ -329,6 +329,27 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -329,6 +329,27 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertEquals(array('London', 'Manhattan', 'Bruxelles'), $freshUser->addresses->lists('city')); $this->assertEquals(array('London', 'Manhattan', 'Bruxelles'), $freshUser->addresses->lists('city'));
} }
public function testEmbedsManyAssociate()
{
$user = User::create(array('name' => 'John Doe'));
$address = new Address(array('city' => 'London'));
$address = $user->addresses()->associate($address);
$this->assertNotNull($user->_addresses);
$this->assertEquals(array('London'), $user->addresses->lists('city'));
$this->assertNotNull($address->_id);
$freshUser = User::find($user->_id);
$this->assertEquals(array(), $freshUser->addresses->lists('city'));
$address->city = 'Londinium';
$user->addresses()->associate($address);
$this->assertEquals(array('Londinium'), $user->addresses->lists('city'));
$freshUser = User::find($user->_id);
$this->assertEquals(array(), $freshUser->addresses->lists('city'));
}
public function testEmbedsManySaveMany() public function testEmbedsManySaveMany()
{ {
$user = User::create(array('name' => 'John Doe')); $user = User::create(array('name' => 'John Doe'));
......
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