diff --git a/src/SaveRelationsBehavior.php b/src/SaveRelationsBehavior.php index 32405d2..0b8a476 100644 --- a/src/SaveRelationsBehavior.php +++ b/src/SaveRelationsBehavior.php @@ -279,11 +279,9 @@ class SaveRelationsBehavior extends Behavior if (!empty($model->{$relationName})) { if ($relation->multiple === false) { $relationModel = $model->{$relationName}; - if (!($model::isPrimaryKey(array_values($relation->link)) - && $relationModel::isPrimaryKey(array_keys($relation->link)) - && $model->getIsNewRecord() - && $relationModel->getIsNewRecord() - )) { + $p1 = $model->isPrimaryKey(array_keys($relation->link)); + $p2 = $relationModel::isPrimaryKey(array_values($relation->link)); + if ($relationModel->getIsNewRecord() && $p1 && !$p2) { // Save Has one relation new record $pettyRelationName = Inflector::camel2words($relationName, true); $this->saveModelRecord($model->{$relationName}, $event, $pettyRelationName, $relationName); diff --git a/tests/SaveRelationsBehaviorTest.php b/tests/SaveRelationsBehaviorTest.php index 079137d..6c45793 100644 --- a/tests/SaveRelationsBehaviorTest.php +++ b/tests/SaveRelationsBehaviorTest.php @@ -60,8 +60,9 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase // User $db->createCommand()->createTable('user', [ - 'id' => $migration->primaryKey(), - 'username' => $migration->string()->notNull()->unique() + 'id' => $migration->primaryKey(), + 'company_id' => $migration->integer()->notNull(), + 'username' => $migration->string()->notNull()->unique() ])->execute(); // User profile @@ -133,11 +134,11 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase [3, 'Google'], ])->execute(); - $db->createCommand()->batchInsert('user', ['id', 'username'], [ - [1, 'Steve Jobs'], - [2, 'Bill Gates'], - [3, 'Tim Cook'], - [4, 'Jonathan Ive'] + $db->createCommand()->batchInsert('user', ['id', 'username', 'company_id'], [ + [1, 'Steve Jobs', 1], + [2, 'Bill Gates', 2], + [3, 'Tim Cook', 1], + [4, 'Jonathan Ive', 1] ])->execute(); $db->createCommand()->batchInsert('project', ['id', 'name', 'company_id'], [ @@ -312,6 +313,7 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase $this->assertCount(1, $project->users, 'Project should have 1 user before save'); $user = new User(); $user->username = "Steve Balmer"; + $user->company_id = 2; $project->users = array_merge($project->users, [$user]); // Add a fresh new user $this->assertCount(2, $project->users, 'Project should have 2 users after assignment'); $this->assertTrue($project->save(), 'Project could not be saved'); @@ -322,7 +324,7 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase { $project = Project::findOne(2); $this->assertCount(1, $project->users, 'Project should have 1 user before save'); - $user = ['username' => "Steve Balmer"]; + $user = ['username' => "Steve Balmer", 'company_id' => 2]; $project->users = array_merge($project->users, [$user]); // Add a fresh new user $this->assertCount(2, $project->users, 'Project should have 2 users after assignment'); $this->assertTrue($project->save(), 'Project could not be saved'); @@ -643,6 +645,9 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase { $user = new User(); $user->username = 'Dummy More'; + $user->company = [ + 'name' => 'ACME' + ]; $user->userProfile = [ 'bio' => "Some great bio" ]; @@ -651,4 +656,16 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase $this->assertEquals($user->id, $user->userProfile->user_id); } + public function testSaveCompanyWithUser() + { + $project = new Project(); + $project->name = "Cartoon"; + $company = new Company(); + $company->name = 'ACME'; + $user = new User(); + $user->username = "Bugs Bunny"; + $company->users = $user; + $project->company = $company; + $this->assertTrue($project->save(), 'Project could not be saved'); + } } diff --git a/tests/models/Company.php b/tests/models/Company.php index 4aece91..420d38a 100644 --- a/tests/models/Company.php +++ b/tests/models/Company.php @@ -2,6 +2,8 @@ namespace tests\models; +use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior; + class Company extends \yii\db\ActiveRecord { @@ -16,6 +18,19 @@ class Company extends \yii\db\ActiveRecord /** * @inheritdoc */ + public function behaviors() + { + return [ + 'saveRelations' => [ + 'class' => SaveRelationsBehavior::className(), + 'relations' => ['users'] + ], + ]; + } + + /** + * @inheritdoc + */ public function rules() { return [ @@ -24,4 +39,12 @@ class Company extends \yii\db\ActiveRecord ]; } + /** + * @return \yii\db\ActiveQuery + */ + public function getUsers() + { + return $this->hasMany(User::className(), ['company_id' => 'id']); + } + } diff --git a/tests/models/User.php b/tests/models/User.php index 0f7c176..1759942 100644 --- a/tests/models/User.php +++ b/tests/models/User.php @@ -22,7 +22,7 @@ class User extends \yii\db\ActiveRecord return [ 'saveRelations' => [ 'class' => SaveRelationsBehavior::className(), - 'relations' => ['userProfile'] + 'relations' => ['userProfile', 'company'] ], ]; } @@ -33,8 +33,10 @@ class User extends \yii\db\ActiveRecord public function rules() { return [ + ['company_id', 'integer'], ['username', 'required'], ['username', 'unique', 'targetClass' => '\tests\models\User'], + [['company_id'], 'exist', 'skipOnError' => true, 'targetClass' => Company::className(), 'targetAttribute' => ['company_id' => 'id']], ]; } @@ -46,4 +48,12 @@ class User extends \yii\db\ActiveRecord return $this->hasOne(UserProfile::className(), ['user_id' => 'id']); } + /** + * @return \yii\db\ActiveQuery + */ + public function getCompany() + { + return $this->hasOne(Company::className(), ['id' => 'company_id']); + } + }