Browse Source

Fix for issue #22: when a has one relation is involved and the related models are linked using there primary key, the owner attribute should not be updated in order to pass the validation

Added test cases for #22 issue
tags/1.3.2
Alban Jubert 7 years ago
parent
commit
a34b436548
  1. 2
      src/SaveRelationsBehavior.php
  2. 39
      tests/SaveRelationsBehaviorTest.php
  3. 48
      tests/models/BadConfigurationModel.php
  4. 23
      tests/models/User.php
  5. 25
      tests/models/UserProfile.php

2
src/SaveRelationsBehavior.php

@ -238,7 +238,7 @@ class SaveRelationsBehavior extends Behavior
if ($relation->multiple === false && !empty($model->{$relationName})) { if ($relation->multiple === false && !empty($model->{$relationName})) {
Yii::trace("Setting foreign keys for {$relationName}", __METHOD__); Yii::trace("Setting foreign keys for {$relationName}", __METHOD__);
foreach ($relation->link as $relatedAttribute => $modelAttribute) { foreach ($relation->link as $relatedAttribute => $modelAttribute) {
if ($model->{$modelAttribute} !== $model->{$relationName}->{$relatedAttribute}) { if ($model->{$modelAttribute} !== $model->{$relationName}->{$relatedAttribute} && !in_array($modelAttribute, $model->primaryKey())) {
$model->{$modelAttribute} = $model->{$relationName}->{$relatedAttribute}; $model->{$modelAttribute} = $model->{$relationName}->{$relatedAttribute};
} }
} }

39
tests/SaveRelationsBehaviorTest.php

@ -4,7 +4,6 @@ namespace tests;
use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior; use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior;
use RuntimeException;
use tests\models\Company; use tests\models\Company;
use tests\models\DummyModel; use tests\models\DummyModel;
use tests\models\DummyModelParent; use tests\models\DummyModelParent;
@ -32,6 +31,7 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
$db->createCommand()->dropTable('project_user')->execute(); $db->createCommand()->dropTable('project_user')->execute();
$db->createCommand()->dropTable('project')->execute(); $db->createCommand()->dropTable('project')->execute();
$db->createCommand()->dropTable('user')->execute(); $db->createCommand()->dropTable('user')->execute();
$db->createCommand()->dropTable('user_profile')->execute();
$db->createCommand()->dropTable('company')->execute(); $db->createCommand()->dropTable('company')->execute();
$db->createCommand()->dropTable('link_type')->execute(); $db->createCommand()->dropTable('link_type')->execute();
$db->createCommand()->dropTable('link')->execute(); $db->createCommand()->dropTable('link')->execute();
@ -62,6 +62,12 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
'username' => $migration->string()->notNull()->unique() 'username' => $migration->string()->notNull()->unique()
])->execute(); ])->execute();
// User profile
$db->createCommand()->createTable('user_profile', [
'user_id' => $migration->primaryKey(),
'bio' => $migration->text(),
])->execute();
// Project // Project
$db->createCommand()->createTable('project', [ $db->createCommand()->createTable('project', [
'id' => $migration->primaryKey(), 'id' => $migration->primaryKey(),
@ -148,20 +154,25 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
])->execute(); ])->execute();
} }
/**
* @expectedException RuntimeException
*/
public function testCannotAttachBehaviorToAnythingButActiveRecord() public function testCannotAttachBehaviorToAnythingButActiveRecord()
{ {
$this->setExpectedException('RuntimeException');
$model = new Model(); $model = new Model();
$model->attachBehavior('saveRelated', SaveRelationsBehavior::className()); $model->attachBehavior('saveRelated', SaveRelationsBehavior::className());
} }
/** public function testUnsupportedRelationProperty()
* @expectedException \yii\base\InvalidCallException {
*/ $this->setExpectedException('\yii\base\UnknownPropertyException');
$model = new Project();
$model->detachBehaviors();
$model->attachBehavior('saveRelated', new SaveRelationsBehavior(['relations' => ['links' => ['fakeParam' => 'Some value']]]));
}
public function testTryToSetUndeclaredRelationShouldFail() public function testTryToSetUndeclaredRelationShouldFail()
{ {
$this->setExpectedException('\yii\base\InvalidCallException');
$project = new Project(); $project = new Project();
$project->projectUsers = []; $project->projectUsers = [];
} }
@ -582,7 +593,7 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
$project->loadRelations($data); $project->loadRelations($data);
$this->assertFalse($project->save(), 'Project could be saved'); $this->assertFalse($project->save(), 'Project could be saved');
$data = [ $data = [
'Link' => [ 'Link' => [
[ [
'language' => 'en', 'language' => 'en',
'name' => 'yii', 'name' => 'yii',
@ -594,4 +605,16 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($project->save(), 'Project could not be saved'); $this->assertTrue($project->save(), 'Project could not be saved');
} }
public function testSaveHasOneWithPrimaryKeyAsForeignKey()
{
$user = new User();
$user->username = 'Dummy More';
$user->userProfile = [
'bio' => "Some great bio"
];
$this->assertTrue($user->save(), 'User could not be saved');
$this->assertEquals($user->userProfile->bio, "Some great bio");
$this->assertEquals($user->id, $user->userProfile->user_id);
}
} }

48
tests/models/BadConfigurationModel.php

@ -0,0 +1,48 @@
<?php
/**
* @link http://www.lahautesociete.com
* @copyright Copyright (c) 2016 La Haute Société
*/
namespace tests\models;
use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior;
/**
* DummyModel class
*
* @author albanjubert
**/
class BadConfigurationModel extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'dummy';
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'saveRelations' => [
'class' => SaveRelationsBehavior::className(),
'relations' => ['children']
],
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getChildren()
{
return $this->hasOne(DummyModel::className(), ['id' => 'parent_id']);
}
}

23
tests/models/User.php

@ -2,6 +2,8 @@
namespace tests\models; namespace tests\models;
use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior;
class User extends \yii\db\ActiveRecord class User extends \yii\db\ActiveRecord
{ {
/** /**
@ -15,6 +17,19 @@ class User extends \yii\db\ActiveRecord
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function behaviors()
{
return [
'saveRelations' => [
'class' => SaveRelationsBehavior::className(),
'relations' => ['userProfile']
],
];
}
/**
* @inheritdoc
*/
public function rules() public function rules()
{ {
return [ return [
@ -23,4 +38,12 @@ class User extends \yii\db\ActiveRecord
]; ];
} }
/**
* @return \yii\db\ActiveQuery
*/
public function getUserProfile()
{
return $this->hasOne(UserProfile::className(), ['user_id' => 'id']);
}
} }

25
tests/models/UserProfile.php

@ -0,0 +1,25 @@
<?php
namespace tests\models;
class UserProfile extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'user_profile';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
['bio', 'required']
];
}
}
Loading…
Cancel
Save