From 6137a94102778a8dd211d51fc8b83476e357aace Mon Sep 17 00:00:00 2001 From: Scott Dawson Date: Mon, 16 Feb 2015 21:42:23 +1100 Subject: [PATCH] Add recursive save and set relations --- src/Sjdaws/Vocal/Vocal.php | 42 +++++++++++++++++++++++++++++--------- tests/VocalTest.php | 12 +++++++---- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/Sjdaws/Vocal/Vocal.php b/src/Sjdaws/Vocal/Vocal.php index 4cd5206..a6eadcd 100644 --- a/src/Sjdaws/Vocal/Vocal.php +++ b/src/Sjdaws/Vocal/Vocal.php @@ -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; @@ -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 * @@ -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); } @@ -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); } diff --git a/tests/VocalTest.php b/tests/VocalTest.php index 2ecf5e8..bea3436 100755 --- a/tests/VocalTest.php +++ b/tests/VocalTest.php @@ -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( @@ -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'); } }