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 {
*/
public function save(Model $model)
{
$this->updateTimestamps($model);
// Insert a new document.
if ( ! $model->exists)
{
......@@ -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
* @return \Illuminate\Database\Eloquent\Model
*/
protected function performInsert(Model $model)
public function associate(Model $model)
{
// Create a new key.
if ( ! $model->getAttribute('_id'))
// Insert the related model in the parent instance
if ( ! $model->exists)
{
$model->setAttribute('_id', new MongoId);
return $this->associateNew($model);
}
// Update timestamps.
$this->updateTimestamps($model);
// Push the document to the database.
$result = $this->query->push($this->localKey, $model->getAttributes(), true);
$documents = $this->getEmbeddedRecords();
// Update the related model in the parent instance
else
{
return $this->associateExisting($model);
}
// 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.
$model->exists = true;
// Push the document to the database.
$result = $this->query->push($this->localKey, $model->getAttributes(), true);
return $result ? $model : false;
}
......@@ -194,8 +202,8 @@ class EmbedsMany extends Relation {
*/
protected function performUpdate(Model $model)
{
// Update timestamps.
$this->updateTimestamps($model);
// Update the related model in the parent instance
$this->associateExisting($model);
// Get the correct foreign key value.
$id = $this->getForeignKeyValue($model->getKey());
......@@ -204,6 +212,44 @@ class EmbedsMany extends Relation {
$result = $this->query->where($this->localKey . '.' . $model->getKeyName(), $id)
->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.
$documents = $this->getEmbeddedRecords();
......@@ -212,18 +258,18 @@ class EmbedsMany extends Relation {
$key = $model->getKey();
// Replace the document in the parent model.
foreach ($documents as $i => $document)
foreach ($documents as &$document)
{
if ($document[$primaryKey] == $key)
{
$documents[$i] = $model->getAttributes();
$document = $model->getAttributes();
break;
}
}
$this->setEmbeddedRecords($documents);
return $result ? $model : false;
return $model;
}
/**
......
......@@ -329,6 +329,27 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$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()
{
$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