Skip to content

Commit

Permalink
feat: detachNode
Browse files Browse the repository at this point in the history
  • Loading branch information
AidasK committed Sep 2, 2014
1 parent 58acc0b commit adbe1c1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
27 changes: 26 additions & 1 deletion ClosureTableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public function moveTo($target, $node = null)
/**
* Deletes node and it's descendants.
* @param $primaryKey
* @return int number of rows deleted
* @return int number of rows and relations deleted (Note, number may differ in different mysql versions)
*/
public function deleteNode($primaryKey = null)
{
Expand All @@ -502,4 +502,29 @@ public function deleteNode($primaryKey = null)
);
return $cmd->execute(array($primaryKey));
}

/**
* Delete all records from closure table associated with this record
* @param $primaryKey
* @return int number of rows deleted
*/
public function detachNode($primaryKey = null)
{
/* @var $owner CActiveRecord */
$owner = $this->getOwner();
if ($primaryKey === null) {
$primaryKey = $owner->primaryKey;
}
$db = $owner->getDbConnection();
$closureTable = $db->quoteTableName($this->closureTableName);
$childAttribute = $db->quoteColumnName($this->childAttribute);
$primaryKeyName = $db->quoteColumnName($owner->tableSchema->primaryKey);
$cmd = $db->createCommand(
'DELETE t '
. 'FROM ' . $closureTable . ' t '
. 'JOIN ' . $closureTable . ' tt ON t.' . $childAttribute . '= tt.' . $childAttribute
. 'WHERE tt.' . $db->quoteColumnName($this->parentAttribute) . '=?'
);
return $cmd->execute(array($primaryKey));
}
}
1 change: 1 addition & 0 deletions tests/models/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* @method int append(CActiveRecord $target) Appends node to target as child (Only for new records).
* @method int moveTo($target, $node = null) Move node
* @method int deleteNode($primaryKey = null) Delete node
* @method int detachNode($primaryKey = null) Delete all records from closure table associated with this record
*/
class Folder extends CActiveRecord
{
Expand Down
34 changes: 33 additions & 1 deletion tests/unit/ClosureTableBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public function testAppend()
{
$folder = Folder::model()->findByPk(5);
$newFolder = new Folder();
$newFolder->id = 8;
$newFolder->name = 'Folder 1.4.5.8';
$this->assertTrue($newFolder->save());
$this->assertGreaterThan(0, $folder->append($newFolder));
Expand Down Expand Up @@ -211,10 +212,40 @@ public function testDeleteNode()

$this->assertEquals(0, Folder::model()->deleteNode(0));

$this->assertEquals(3, Folder::model()->deleteNode(1));
$this->assertTrue(Folder::model()->deleteNode(1) > 0);
$this->assertEquals(0, count(Folder::model()->findAll()));
}

public function testDetachNodeMiddle()
{
/** @var Folder $folder */
$folder = Folder::model()->findByPk(4);
$this->assertTrue($folder instanceof Folder);
$this->assertEquals(12, $folder->detachNode());// 12 relations should be deleted
$this->assertEquals(0, count($folder->ancestors()->findAll()));
$this->assertEquals(0, count($folder->children()->findAll()));
}

public function testDetachNodeTop()
{
/** @var Folder $folder */
$folder = Folder::model()->findByPk(1);
$this->assertTrue($folder instanceof Folder);
$this->assertEquals(18, $folder->detachNode());
$this->assertEquals(0, count($folder->ancestors()->findAll()));
$this->assertEquals(0, count($folder->children()->findAll()));
}

public function testDetachNodeLeaf()
{
/** @var Folder $folder */
$folder = Folder::model()->findByPk(3);
$this->assertTrue($folder instanceof Folder);
$this->assertEquals(3, $folder->detachNode());
$this->assertEquals(0, count($folder->ancestors()->findAll()));
$this->assertEquals(0, count($folder->children()->findAll()));
}

public function testSaveAsRoot()
{
$newFolder = new Folder();
Expand All @@ -241,6 +272,7 @@ public function testMixed()
{
$folder5 = Folder::model()->findByPk(5);
$newFolder = new Folder();
$newFolder->id = 8;
$newFolder->name = 'Folder 1.4.5.8';
$this->assertTrue($newFolder->save());
$this->assertGreaterThan(0, $folder5->append($newFolder));
Expand Down

0 comments on commit adbe1c1

Please sign in to comment.