Browse Source

Fix for issue #12

tags/1.1.1^2
Alban Jubert 8 years ago
parent
commit
e1dfd78708
  1. 11
      src/SaveRelationsBehavior.php
  2. 27
      tests/SaveRelationsBehaviorTest.php

11
src/SaveRelationsBehavior.php

@ -228,7 +228,7 @@ class SaveRelationsBehavior extends Behavior
public function _saveModelRecord(ActiveRecord $model, ModelEvent $event, $pettyRelationName, $relationName)
{
$this->_validateRelationModel($pettyRelationName, $relationName, $model, $event);
if ($event->isValid && count($model->dirtyAttributes)) {
if ($event->isValid && (count($model->dirtyAttributes) || $model->isNewRecord)) {
Yii::trace("Saving {$pettyRelationName} relation model", __METHOD__);
$model->save(false);
}
@ -241,12 +241,7 @@ class SaveRelationsBehavior extends Behavior
* @param ActiveRecord $relationModel
* @param ModelEvent $event
*/
private function _validateRelationModel(
$pettyRelationName,
$relationName,
ActiveRecord $relationModel,
ModelEvent $event
)
private function _validateRelationModel($pettyRelationName, $relationName, ActiveRecord $relationModel, ModelEvent $event)
{
/** @var ActiveRecord $model */
$model = $this->owner;
@ -312,7 +307,7 @@ class SaveRelationsBehavior extends Behavior
$model->link($relationName, $actualModels[$key]);
}
} else { // Has one relation
if ($this->_oldRelationValue[$relationName] != $model->{$relationName}) {
if ($this->_oldRelationValue[$relationName] !== $model->{$relationName}) {
if ($model->{$relationName} instanceof ActiveRecord) {
$model->link($relationName, $model->{$relationName});
} else {

27
tests/SaveRelationsBehaviorTest.php

@ -6,8 +6,11 @@ namespace tests;
use lhs\Yii2SaveRelationsBehavior\SaveRelationsBehavior;
use RuntimeException;
use tests\models\Company;
use tests\models\DummyModel;
use tests\models\DummyModelParent;
use tests\models\Link;
use tests\models\Project;
use tests\models\ProjectLink;
use tests\models\ProjectNoTransactions;
use tests\models\User;
use Yii;
@ -33,6 +36,7 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
$db->createCommand()->dropTable('link_type')->execute();
$db->createCommand()->dropTable('link')->execute();
$db->createCommand()->dropTable('project_link')->execute();
$db->createCommand()->dropTable('dummy')->execute();
parent::tearDown();
}
@ -94,6 +98,12 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
'PRIMARY KEY(project_id, user_id)'
])->execute();
// Dummy
$db->createCommand()->createTable('dummy', [
'id' => $migration->primaryKey(),
'parent_id' => $migration->integer()
])->execute();
/**
* Insert some data
*/
@ -136,7 +146,6 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
[1, 4],
[2, 2]
])->execute();
}
/**
@ -427,4 +436,20 @@ class SaveRelationsBehaviorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(1, count($project->users), 'Project should have 1 users after assignment');
}
public function testChangeHasOneRelationWithAnotherObject()
{
$dummy_a = new DummyModel();
$dummy_b = new DummyModel();
$dummy_a->save();
$dummy_b->save();
$dummy_a->children = $dummy_b;
$dummy_b->children = $dummy_a;
$this->assertTrue($dummy_a->save(), 'Dummy A could not be saved');
$this->assertTrue($dummy_b->save(), 'Dummy B could not be saved');
$dummy_c = new DummyModel();
//$this->assertTrue($dummy_c->save(), 'Dummy C could not be saved');
$dummy_a->children = $dummy_c;
$this->assertTrue($dummy_a->save(), 'Dummy A could not be saved');
}
}

Loading…
Cancel
Save