Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple delete for MANY_MANY #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions WithRelatedBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,15 @@ public function save($runValidation=true,$data=null,$owner=null)
foreach($relatedMap as $pk=>$fk)
$joinTableAttributes[$fk]=$model->$pk;

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

$insertAttributes[]=$joinTableAttributes;
}

foreach($ownerMap as $pk=>$fk)
$deleteAttributes[$pk]=$owner->$pk;

if($deleteAttributes!==array())
{
$condition=$builder->createInCondition($joinTable,array_merge(array_values($ownerMap),array_values($relatedMap)),$deleteAttributes);
$condition=$builder->createInCondition($joinTable,array_values($ownerMap),$deleteAttributes);
$criteria=$builder->createCriteria($condition);
$builder->createDeleteCommand($joinTable,$criteria)->execute();
}
Expand Down
55 changes: 55 additions & 0 deletions tests/unit/WithRelatedBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,61 @@ public function testSave()
$this->assertEquals('tag4',$article->tags[3]->name);
}

/**
* @author Anton Tyutin @AntonTyutin
*/
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