From 75450aa185b9d6f541d81793ba71e21a90aeeec9 Mon Sep 17 00:00:00 2001 From: Alban Jubert Date: Tue, 26 Apr 2016 12:11:19 +0200 Subject: [PATCH] Documentation fix. More unit tests added. --- README.md | 37 ++++++++++++++++++++++++-- src/SaveRelationsBehavior.php | 2 +- tests/SaveRelationsBehaviorTest.php | 53 +++++++++++++++++++++++++++++-------- 3 files changed, 78 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index cdce6bb..61bd025 100644 --- a/README.md +++ b/README.md @@ -131,9 +131,42 @@ Validation ---------- Every declared related models will be validated prior to be saved. If any validation fails, for each related model attribute in error, an error associated with the named relation will be added to the owner model. -For `hasMany()` relations, the index of the related model will be used to identifiy the associated error message. +For `hasMany()` relations, the index of the related model will be used to identify the associated error message. > **Tips :** -> For relations not involving a junction table by using the `via()` or `viaTable()` methods, you should remove the attributes pointing to the owner model to be able to pass the validations. +> For relations not involving a junction table by using the `via()` or `viaTable()` methods, you should remove the attributes pointing to the owner model from the 'required' validation rules to be able to pass the validations. +Populate the model and its relations with input data +---------------------------------------------------- +This behavior adds a convenient method to load relations models attributes in the same way that the load() method does. +Simply call the `loadRelations()` with the according input data. + +For instance: +```php +$project = Project::findOne(1); +/** + * $_POST could be something like: + * [ + * 'Company' => [ + * 'name' => 'YiiSoft' + * ], + * 'ProjectLink' => [ + * [ + * 'language' => 'en', + * 'name' => 'yii', + * 'link' => 'http://www.yiiframework.com' + * ], + * [ + * 'language' => 'fr', + * 'name' => 'yii', + * 'link' => 'http://www.yiiframework.fr' + * ] + * ] + * ]; + */ +$project->loadRelations(Yii::$app->request->post()); +``` + +You can even further simplify the process by adding the `SaveRelationsTrait` to your model. +In that case, a call to the `load()` method will also automatically trigger a call to the `loadRelations()` method by using the same data, so you basically won't have to change your controllers. \ No newline at end of file diff --git a/src/SaveRelationsBehavior.php b/src/SaveRelationsBehavior.php index d485c2d..1faa9b4 100644 --- a/src/SaveRelationsBehavior.php +++ b/src/SaveRelationsBehavior.php @@ -84,7 +84,7 @@ class SaveRelationsBehavior extends Behavior } if ($relation->multiple === true) { $newRelations = []; - if(!is_array($value)){ + if (!is_array($value)) { $value = []; } foreach ($value as $entry) { diff --git a/tests/SaveRelationsBehaviorTest.php b/tests/SaveRelationsBehaviorTest.php index 6ad5d1c..ae761c1 100644 --- a/tests/SaveRelationsBehaviorTest.php +++ b/tests/SaveRelationsBehaviorTest.php @@ -8,6 +8,7 @@ use SebastianBergmann\GlobalState\RuntimeException; use tests\models\Company; use tests\models\Link; use tests\models\Project; +use tests\models\ProjectNoTransactions; use tests\models\User; use Yii; use yii\base\Model; @@ -205,8 +206,27 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase $project->company = $company; $this->assertTrue($company->isNewRecord, 'Company should be a new record'); $this->assertFalse($project->save(), 'Project could be saved'); - $this->assertArrayHasKey('company', $project->getErrors(), - 'Validation errors do not contain a message for company'); + $this->assertArrayHasKey( + 'company', + $project->getErrors(), + 'Validation errors do not contain a message for company' + ); + $this->assertEquals('Company: Name cannot be blank.', $project->getFirstError('company')); + } + + public function testSaveInvalidModelWithNoTransactionsSetShouldFail() + { + $project = new ProjectNoTransactions(); + $project->name = "Java"; + $company = new Company(); + $project->company = $company; + $this->assertTrue($company->isNewRecord, 'Company should be a new record'); + $this->assertFalse($project->save(), 'Project could be saved'); + $this->assertArrayHasKey( + 'company', + $project->getErrors(), + 'Validation errors do not contain a message for company' + ); $this->assertEquals('Company: Name cannot be blank.', $project->getFirstError('company')); } @@ -290,8 +310,11 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase $this->assertCount(3, $project->links, 'Project should have 3 links after assignment'); $this->assertTrue($project->save(), 'Project could not be saved'); $this->assertCount(3, $project->links, 'Project should have 3 links after save'); - $this->assertEquals("https://www.microsoft.com/fr-fr/windows/features", $project->links[2]->link, - 'Second link should be https://www.microsoft.com/fr-fr/windows/features'); + $this->assertEquals( + "https://www.microsoft.com/fr-fr/windows/features", + $project->links[2]->link, + 'Second link should be https://www.microsoft.com/fr-fr/windows/features' + ); } public function testSaveNewHasManyRelationWithCompositeFksAsArrayShouldSucceed() @@ -306,10 +329,16 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase $this->assertCount(4, $project->links, 'Project should have 4 links after assignment'); $this->assertTrue($project->save(), 'Project could not be saved'); $this->assertCount(4, $project->links, 'Project should have 4 links after save'); - $this->assertEquals("https://www.microsoft.com/fr-fr/windows/features", $project->links[2]->link, - 'Second link should be https://www.microsoft.com/fr-fr/windows/features'); - $this->assertEquals("https://www.microsoft.com/en-us/windows/features", $project->links[3]->link, - 'Third link should be https://www.microsoft.com/en-us/windows/features'); + $this->assertEquals( + "https://www.microsoft.com/fr-fr/windows/features", + $project->links[2]->link, + 'Second link should be https://www.microsoft.com/fr-fr/windows/features' + ); + $this->assertEquals( + "https://www.microsoft.com/en-us/windows/features", + $project->links[3]->link, + 'Third link should be https://www.microsoft.com/en-us/windows/features' + ); } public function testSaveUpdatedHasManyRelationWithCompositeFksAsArrayShouldSucceed() @@ -321,8 +350,11 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase $project->links = $links; $this->assertTrue($project->save(), 'Project could not be saved'); $this->assertCount(2, $project->links, 'Project should have 2 links before save'); - $this->assertEquals("http://www.otherlink.com/", $project->links[1]->link, - 'Second link "Link" attribute should be "http://www.otherlink.com/"'); + $this->assertEquals( + "http://www.otherlink.com/", + $project->links[1]->link, + 'Second link "Link" attribute should be "http://www.otherlink.com/"' + ); } public function testSaveMixedRelationsShouldSucceed() @@ -387,5 +419,4 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase $this->assertCount(2, $project->projectLinks, "Project should have 2 links"); } - }