Skip to content

Commit

Permalink
Add recursive save and set relations
Browse files Browse the repository at this point in the history
  • Loading branch information
sjdaws committed Feb 16, 2015
1 parent b1dbc80 commit 6137a94
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
42 changes: 32 additions & 10 deletions src/Sjdaws/Vocal/Vocal.php
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Hashing\BcryptHasher;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Validator;
Expand Down Expand Up @@ -815,10 +816,37 @@ private function recurseRecord($method, $name, array $data, $index = null)
// Capture errors
$this->errors = $this->mergeErrors($this->errors, $record->getErrors(), $index);

// Attach relationhip to record
if ($result) $this->setRelationship($class, $record, $name, $index);

// Record failure
return $result;
}

/**
* @return \Illuminate\Database\Eloquent\Model
*/
private function setRelationship($relationship, $record, $name, $index)
{
// If we have an index we're dealing with a many relationship
if ($index !== null)
{
$collection = ($this->{$name}) ? $this->{$name} : new Collection;
$collection->put($index, $record);
return $this->setRelation($name, $collection);
}

// belongsTo and morphTo become parent records via associate
if (method_exists($this->{$relationship}(), 'associate'))
{
return $model->associate($record)->forceSave();
}
else
{
return $this->setRelation($name, $record);
}
}

/**
* Remove any fields which can't be submitted to the database
*
Expand All @@ -840,13 +868,10 @@ private function removeInvalidAttributes()
* @param array $messages
* @return bool
*/
public function save(array $data = [], array $rules = [], array $messages = [], $force = false)
public function save(array $data = [], array $rules = [], array $messages = [])
{
if ($this->validateBeforeSave)
{
// Validate, and if it fails abort save
if ( ! $this->validate($data, $rules, $messages)) return false;
}
// Validate, and if it fails abort save
if ($this->validateBeforeSave && ! $this->validate($data, $rules, $messages)) return false;

return $this->saveRecord($data, $rules, $messages);
}
Expand Down Expand Up @@ -881,10 +906,7 @@ private function saveRecord(array $data = [], array $rules = [], array $messages
public function saveRecursive(array $data = [], array $rules = [], array $messages = [])
{
// If we're validating, do the whole record first to head off any problems
if ($this->validateBeforeSave)
{
if ( ! $this->validateRecursive($data, $rules, $messages)) return false;
}
if ($this->validateBeforeSave && ! $this->validateRecursive($data, $rules, $messages)) return false;

return $this->recurse('save', $data, $rules, $messages);
}
Expand Down
12 changes: 8 additions & 4 deletions tests/VocalTest.php
Expand Up @@ -317,8 +317,8 @@ public function testSaveRecursive()
$this->assertNotNull($test3, 'Record failed to save even when it was forced');

// Make sure save was indeed recursive
//$test4 = TestChildChild::find($test1->children[1]->children[0]->id);
//$this->assertNotNull($test4, 'Child record failed to save recursively');
$test4 = TestChildChild::find(array_get($test1->toArray(), 'children.1.children.0.id'));
$this->assertNotNull($test4, 'Child record failed to save recursively on force save');

// Reset data
$data = array(
Expand All @@ -338,7 +338,11 @@ public function testSaveRecursive()
$input->replace($data);

// Test again with correct data
$test4 = new Test;
$this->assertTrue($test4->saveRecursive(), "Recursive save should pass but it didn't");
$test5 = new Test;
$this->assertTrue($test5->saveRecursive(), "Recursive save should pass but it didn't");

// Make sure save was indeed recursive
$test6 = TestChildChild::find(array_get($test5->toArray(), 'children.1.children.0.id'));
$this->assertNotNull($test5, 'Child record failed to save recursively');
}
}

0 comments on commit 6137a94

Please sign in to comment.