Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Commit

Permalink
Prepare for 1.4.1 release
Browse files Browse the repository at this point in the history
  • Loading branch information
Alban Jubert committed Mar 4, 2018
1 parent 6e81025 commit 264731f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Yii2 Active Record Save Relations Behavior Change Log

## [1.4.1]
### Fixed
- Bug #24: Fix a regression introduced in 1.4.0 release where validation of hasOne relations was not triggered. (thx @dabkhazi)

## [1.4.0]
### Fixed
- Bug #25: Fix for Yii 2.0.14 compatibility. Has many relations were not saved. (thx @SanChes-tanker)
Expand Down
4 changes: 3 additions & 1 deletion src/SaveRelationsBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,12 @@ protected function saveRelatedRecords(BaseActiveRecord $model, ModelEvent $event
$relationModel = $model->{$relationName};
$p1 = $model->isPrimaryKey(array_keys($relation->link));
$p2 = $relationModel::isPrimaryKey(array_values($relation->link));
$pettyRelationName = Inflector::camel2words($relationName, true);
if ($relationModel->getIsNewRecord() && $p1 && !$p2) {
// Save Has one relation new record
$pettyRelationName = Inflector::camel2words($relationName, true);
$this->saveModelRecord($model->{$relationName}, $event, $pettyRelationName, $relationName);
} else {
$this->validateRelationModel($pettyRelationName, $relationName, $relationModel, $event);
}
} else {
// Save Has many relations new records
Expand Down
15 changes: 15 additions & 0 deletions tests/SaveRelationsBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use tests\models\ProjectNoTransactions;
use tests\models\Tag;
use tests\models\User;
use tests\models\UserProfile;
use Yii;
use yii\base\Model;
use yii\db\Migration;
Expand Down Expand Up @@ -710,4 +711,18 @@ public function testUpdateHasManyNestedModels()
$this->assertEquals($project->company->name, "Tutu");
$this->assertEquals($project->company->users[0]->username, "Someone Else");
}

public function testHasOneRelationShouldTriggerOnBeforeValidateEvent()
{
$user = new User();
$user->setAttributes([
'username' => 'Larry Page',
'company_id' => 3
]);
$user->userProfile = new UserProfile();
$this->assertFalse($user->save(), 'User should not be saved');
$this->assertCount(1, $user->userProfile->getErrors());
$user->userProfile->bio = 'Lawrence Edward Page (born March 26, 1973) is an American computer scientist and Internet entrepreneur who co-founded Google with Sergey Brin.';
$this->assertTrue($user->save(), 'User could not be saved');
}
}

0 comments on commit 264731f

Please sign in to comment.