Skip to content

Commit

Permalink
Removing MANY_MANY relations (reftable records) on save when relation…
Browse files Browse the repository at this point in the history
… array items was removed.
  • Loading branch information
AntonTyutin committed Sep 16, 2013
1 parent 7c1d125 commit 858393f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
19 changes: 8 additions & 11 deletions WithRelatedBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,15 @@ public function save($runValidation=true,$data=null,$owner=null)
throw new CDbException(Yii::t('yii','The relation "{relation}" in active record class "{class}" is specified with an incomplete foreign key. The foreign key must consist of columns referencing both joining tables.',
array('{class}'=>get_class($owner),'{relation}'=>$name)));

$condition=$builder->createInCondition(
$joinTable,
array_values($ownerMap),
count($ownerTableSchema->primaryKey) > 1 ? array($owner->$pk) : array($ownerTableSchema->primaryKey[0] => $owner->$pk)
);
$criteria=$builder->createCriteria($condition);
$builder->createDeleteCommand($joinTable,$criteria)->execute();

$insertAttributes=array();
$deleteAttributes=array();

foreach($related as $model)
{
Expand All @@ -343,19 +350,9 @@ public function save($runValidation=true,$data=null,$owner=null)
foreach($relatedMap as $pk=>$fk)
$joinTableAttributes[$fk]=$model->$pk;

if(!$newFlag)
$deleteAttributes[]=$joinTableAttributes;

$insertAttributes[]=$joinTableAttributes;
}

if($deleteAttributes!==array())
{
$condition=$builder->createInCondition($joinTable,array_merge(array_values($ownerMap),array_values($relatedMap)),$deleteAttributes);
$criteria=$builder->createCriteria($condition);
$builder->createDeleteCommand($joinTable,$criteria)->execute();
}

foreach($insertAttributes as $attributes)
$builder->createInsertCommand($joinTable,$attributes)->execute();
break;
Expand Down
52 changes: 52 additions & 0 deletions tests/unit/WithRelatedBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,58 @@ public function testSave()
$this->assertEquals('tag4',$article->tags[3]->name);
}

public function testUnsetRelations()
{
$tag1=new Tag;
$tag2=new Tag;

$tag1->name='tag1';
$tag2->name='tag2';

$group=new Group;
$group->name = 'Group';

$user=new User;
$user->name = 'User';
$user->group = $group;

$article=new Article;
$article->title = 'Article';
$article->user = $user;
$article->tags=array($tag1,$tag2);

$result=$article->withRelated->save(true,array(
'user' => array("group"),
'tags',
));
$this->assertTrue($result);

$article=Article::model()->with('tags')->find();
$this->assertEquals(2,count($article->tags));

// remove one tag
$article->tags = array_slice($article->tags, 1);

$result=$article->withRelated->save(true,array(
'tags',
));
$this->assertTrue($result);

$article=Article::model()->with('tags')->find();
$this->assertEquals(1,count($article->tags));

// clear all tags
$article->tags = array();

$result=$article->withRelated->save(true,array(
'tags',
));
$this->assertTrue($result);

$article=Article::model()->with('tags')->find();
$this->assertEquals(0,count($article->tags));
}

/**
* Tests non-database class attributes validation.
* User::$firstName and User::$lastName attributes validation will be tested.
Expand Down

0 comments on commit 858393f

Please sign in to comment.