Skip to content

Commit

Permalink
Add in fix from Cameron Spear (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjdaws committed Dec 8, 2014
1 parent 61cd329 commit a94d0ef
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sjdaws/vocal",
"description": "Extended functionality for Eloquent in Laravel 4.1+",
"version": "0.2.4",
"version": "0.2.5",
"type": "library",
"license": "MIT",
"keywords": ["laravel", "eloquent", "orm", "activerecord", "recursive", "hydration", "validation", "angular", "angularjs"],
Expand Down
7 changes: 6 additions & 1 deletion src/Sjdaws/Vocal/Vocal.php
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,12 @@ private function saveRelations($conditions, $rules, $messages, $relationships, $
// Save record on success, log errors on fail
if ($result)
{
if (method_exists($this->$modelClass(), 'associate')) $this->$modelClass()->associate($record)->forceSave();
if (method_exists($this->$modelClass(), 'associate')) {

// we must save the record first before associating it
$record->forceSave();
$this->$modelClass()->associate($record)->forceSave();
}
else
{
$result = $this->$modelClass()->saveRelation($record);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function up()
Schema::create($this->table, function($t)
{
$t->increments('id')->unsigned();
$t->integer('test_id')->unsigned();
$t->integer('test_id')->unsigned()->nullable();
$t->string('description', 100);
$t->timestamps();
});
Expand Down
17 changes: 17 additions & 0 deletions tests/VocalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,22 @@ public function testVocal()
$this->assertTrue($result, $this->errorResponse('Record was not saved', $test));

$this->assertTrue($test->password && $test->password != $input->get('password'), $this->errorResponse('Record was not saved', $test));

// Test that the inverse relationship (belongsTo) works
$input->replace(array(
'description' => 'Child 3',
'parent' => array(
'description' => 'Parent 2',
)
));

$child = new TestChild;
$result = $child->saveRecursive();

// Make sure validation passed
$this->assertTrue($result, $this->errorResponse('Record was not saved', $child));

// Make sure the parent has an ID (that means it was actually saved)
$this->assertTrue(!!$child->parent->id, true);
}
}

0 comments on commit a94d0ef

Please sign in to comment.