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

Commit

Permalink
Fix #1 issue: Setting null for a named relation is correctly handled now
Browse files Browse the repository at this point in the history
  • Loading branch information
Alban Jubert committed Apr 2, 2016
1 parent 67a8d06 commit b917ed1
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 28 deletions.
25 changes: 15 additions & 10 deletions src/SaveRelationsBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use yii\base\ModelEvent;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\db\Transaction;
use yii\helpers\ArrayHelper;
use yii\helpers\Inflector;

Expand All @@ -29,8 +30,8 @@ public function events()
{
return [
ActiveRecord::EVENT_BEFORE_VALIDATE => 'beforeValidate',
ActiveRecord::EVENT_AFTER_INSERT => 'afterSave',
ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave',
ActiveRecord::EVENT_AFTER_INSERT => 'afterSave',
ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave',
];
}

Expand Down Expand Up @@ -74,7 +75,6 @@ public function canSetProperty($name, $checkVars = true)
public function __set($name, $value)
{
if (in_array($name, $this->relations)) {
//echo "Setting " . $name . "\n";
/** @var ActiveRecord $model */
$model = $this->owner;
Yii::trace("Setting {$name} relation value", __METHOD__);
Expand Down Expand Up @@ -126,16 +126,16 @@ public function _processModelAsArray($data, $relation)
} else {
$fks = $data;
}
// Load existing model or create one if no key was provided
// Load existing model or create one if no key was provided and data is not empty
/** @var ActiveRecord $relationModel */
$relationModel = null;
if (!empty($fks)) {
$relationModel = $modelClass::findOne($fks);
}
if (!$relationModel) {
if (!($relationModel instanceof ActiveRecord) && !empty($data)) {
$relationModel = new $modelClass;
}
if (is_array($data)) {
if (($relationModel instanceof ActiveRecord) && is_array($data)) {
$relationModel->setAttributes($data);
}
return $relationModel;
Expand Down Expand Up @@ -248,8 +248,7 @@ private function _validateRelationModel(
$relationName,
ActiveRecord $relationModel,
ModelEvent $event
)
{
) {
/** @var ActiveRecord $model */
$model = $this->owner;
if (!is_null($relationModel) && ($relationModel->isNewRecord || count($relationModel->getDirtyAttributes()))) {
Expand Down Expand Up @@ -301,15 +300,21 @@ function (ActiveRecord $model) {
}
} else { // Has one relation
if ($this->_oldRelationValue[$relationName] != $model->{$relationName}) {
$model->link($relationName, $model->{$relationName});
if ($model->{$relationName} instanceof ActiveRecord) {
$model->link($relationName, $model->{$relationName});
} else {
if ($this->_oldRelationValue[$relationName] instanceof ActiveRecord) {
$model->unlink($relationName, $this->_oldRelationValue[$relationName]);
}
}
}
}
unset($this->_oldRelationValue[$relationName]);
}
}
$model->refresh();
$this->_relationsSaveStarted = false;
if ($this->_transaction->isActive) {
if (($this->_transaction instanceof Transaction) && $this->_transaction->isActive) {
$this->_transaction->commit();
}
}
Expand Down
60 changes: 49 additions & 11 deletions tests/SaveRelationsBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected function tearDown()
$db->createCommand()->dropTable('project')->execute();
$db->createCommand()->dropTable('user')->execute();
$db->createCommand()->dropTable('company')->execute();
$db->createCommand()->dropTable('link_type')->execute();
$db->createCommand()->dropTable('link')->execute();
$db->createCommand()->dropTable('project_link')->execute();
parent::tearDown();
Expand Down Expand Up @@ -66,12 +67,18 @@ protected function setupDbData()
$db->createCommand()->createIndex('company_id-name', 'project', 'company_id,name', true)->execute();

$db->createCommand()->createTable('link', [
'language' => $migration->string(5)->notNull(),
'name' => $migration->string()->notNull(),
'link' => $migration->string()->notNull(),
'language' => $migration->string(5)->notNull(),
'name' => $migration->string()->notNull(),
'link' => $migration->string()->notNull(),
'link_type_id' => $migration->integer(),
'PRIMARY KEY(language, name)'
])->execute();

$db->createCommand()->createTable('link_type', [
'id' => $migration->primaryKey(),
'name' => $migration->string()->notNull()->unique()
])->execute();

$db->createCommand()->createTable('project_link', [
'language' => $migration->string(5)->notNull(),
'name' => $migration->string()->notNull(),
Expand Down Expand Up @@ -108,9 +115,14 @@ protected function setupDbData()
[2, 'Windows 10', 2]
])->execute();

$db->createCommand()->batchInsert('link', ['language', 'name', 'link'], [
['fr', 'mac_os_x', 'http://www.apple.com/fr/osx/'],
['en', 'mac_os_x', 'http://www.apple.com/osx/']
$db->createCommand()->batchInsert('link_type', ['id', 'name'], [
[1, 'public'],
[2, 'private']
])->execute();

$db->createCommand()->batchInsert('link', ['language', 'name', 'link', 'link_type_id'], [
['fr', 'mac_os_x', 'http://www.apple.com/fr/osx/', 1],
['en', 'mac_os_x', 'http://www.apple.com/osx/', 1]
])->execute();

$db->createCommand()->batchInsert('project_link', ['language', 'name', 'project_id'], [
Expand Down Expand Up @@ -278,7 +290,8 @@ public function testSaveNewHasManyRelationWithCompositeFksShouldSucceed()
$this->assertEquals(3, count($project->links), 'Project should have 3 links after assignment');
$this->assertTrue($project->save(), 'Project could not be saved');
$this->assertEquals(3, count($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()
Expand All @@ -293,8 +306,10 @@ public function testSaveNewHasManyRelationWithCompositeFksAsArrayShouldSucceed()
$this->assertEquals(4, count($project->links), 'Project should have 4 links after assignment');
$this->assertTrue($project->save(), 'Project could not be saved');
$this->assertEquals(4, count($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()
Expand All @@ -305,15 +320,16 @@ public function testSaveUpdatedHasManyRelationWithCompositeFksAsArrayShouldSucce
$links[1]->link = "http://www.otherlink.com/";
$project->links = $links;
$this->assertTrue($project->save(), 'Project could not be saved');
$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()
{
$project = new Project();
$project->name = "New project";
$project->company = Company::findOne(2);
$users = User::findAll([1,3]);
$users = User::findAll([1, 3]);
$this->assertEquals(0, count($project->users), 'Project should have 0 users before save');
$project->users = $users; // Add users
$this->assertEquals(2, count($project->users), 'Project should have 2 users after assignment');
Expand All @@ -322,5 +338,27 @@ public function testSaveMixedRelationsShouldSucceed()
$this->assertEquals(2, $project->company_id, 'Company ID is not the one expected');
}

public function testSettingANullRelationShouldSucceed()
{
$link = new Link();
$link->language = 'en';
$link->name = 'yii';
$link->link = 'http://www.yiiframework.com';
$link->linkType = null;
$this->assertTrue($link->save(), 'Link could not be saved');
$this->assertNull($link->linkType, "Link type should be null");
$this->assertNull($link->link_type_id, "Link type id should be null");
}

public function testUnsettingARelationShouldSucceed()
{
$link = Link::findOne(['language' => 'fr', 'name' => 'mac_os_x']);
$this->assertEquals(1, $link->link_type_id, 'Link type id should be 1');
$link->linkType = null;
$this->assertTrue($link->save(), 'Link could not be saved');
$this->assertNull($link->linkType, "Link type should be null");
$this->assertNull($link->link_type_id, "Link type id should be null");
}


}
23 changes: 23 additions & 0 deletions tests/models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace tests\models;

use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior;

class Link extends \yii\db\ActiveRecord
{
/**
Expand All @@ -12,6 +14,19 @@ public static function tableName()
return 'link';
}

/**
* @inheritdoc
*/
public function behaviors()
{
return [
'saveRelations' => [
'class' => SaveRelationsBehavior::className(),
'relations' => ['linkType']
],
];
}

/**
* @inheritdoc
*/
Expand All @@ -20,7 +35,15 @@ public function rules()
return [
[['language', 'name', 'link'], 'required'],
[['name'], 'unique', 'targetAttribute' => ['language', 'name']],
[['link_type_id'], 'safe']
];
}

/**
* @return \yii\db\ActiveQuery
*/
public function getLinkType()
{
return $this->hasOne(LinkType::className(), ['id' => 'link_type_id']);
}
}
25 changes: 25 additions & 0 deletions tests/models/LinkType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace tests\models;

class LinkType extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'link_type';
}

/**
* @inheritdoc
*/
public function rules()
{
return [
[['name'], 'required'],
[['name'], 'unique']
];
}
}
13 changes: 6 additions & 7 deletions tests/models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace tests\models;


use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior;

class Project extends \yii\db\ActiveRecord
Expand All @@ -21,7 +20,7 @@ public static function tableName()
public function behaviors()
{
return [
'translateable' => [
'saveRelations' => [
'class' => SaveRelationsBehavior::className(),
'relations' => ['company', 'users', 'links']
],
Expand Down Expand Up @@ -50,39 +49,39 @@ public function transactions()
}

/**
* @return ActiveQuery
* @return \yii\db\ActiveQuery
*/
public function getCompany()
{
return $this->hasOne(Company::className(), ['id' => 'company_id']);
}

/**
* @return ActiveQuery
* @return \yii\db\ActiveQuery
*/
public function getProjectUsers()
{
return $this->hasMany(ProjectUser::className(), ['project_id' => 'id']);
}

/**
* @return ActiveQuery
* @return \yii\db\ActiveQuery
*/
public function getUsers()
{
return $this->hasMany(User::className(), ['id' => 'user_id'])->via('projectUsers');
}

/**
* @return ActiveQuery
* @return \yii\db\ActiveQuery
*/
public function getProjectLinks()
{
return $this->hasMany(ProjectLink::className(), ['project_id' => 'id']);
}

/**
* @return ActiveQuery
* @return \yii\db\ActiveQuery
*/
public function getLinks()
{
Expand Down

0 comments on commit b917ed1

Please sign in to comment.