Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6e86f13

Browse files
committedApr 10, 2015
Issue 53: Allow to use treeAttribute as external key
1 parent ac447e9 commit 6e86f13

4 files changed

+36
-2
lines changed
 

‎src/NestedSetsBehavior.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ protected function beforeInsertRootNode()
361361
{
362362
if ($this->treeAttribute === false && $this->owner->find()->roots()->exists()) {
363363
throw new Exception('Can not create more than one root when "treeAttribute" is false.');
364+
} elseif ($this->treeAttribute && $this->owner->getAttribute($this->treeAttribute) && $this->owner->find()->roots($this->owner->getAttribute($this->treeAttribute))->exists()) {
365+
throw new Exception("Can not create more than one root with {$this->treeAttribute} ".$this->owner->getAttribute($this->treeAttribute));
364366
}
365367

366368
$this->owner->setAttribute($this->leftAttribute, 1);
@@ -399,7 +401,7 @@ protected function beforeInsertNode($value, $depth)
399401
*/
400402
public function afterInsert()
401403
{
402-
if ($this->operation === self::OPERATION_MAKE_ROOT && $this->treeAttribute !== false) {
404+
if ($this->operation === self::OPERATION_MAKE_ROOT && $this->treeAttribute !== false && !$this->owner->getAttribute($this->treeAttribute)) {
403405
$this->owner->setAttribute($this->treeAttribute, $this->owner->getPrimaryKey());
404406
$primaryKey = $this->owner->primaryKey();
405407

‎src/NestedSetsQueryBehavior.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,21 @@ class NestedSetsQueryBehavior extends Behavior
2121
{
2222
/**
2323
* Gets the root nodes.
24+
* @param mixed $id the optional tree id. Default is `null` to query for all root nodes.
2425
* @return \yii\db\ActiveQuery the owner
2526
*/
26-
public function roots()
27+
public function roots($id = null)
2728
{
2829
$model = new $this->owner->modelClass();
2930

3031
$this->owner
3132
->andWhere([$model->leftAttribute => 1])
3233
->addOrderBy([$model->primaryKey()[0] => SORT_ASC]);
3334

35+
if ($id!==null && $model->treeAttribute!==false) {
36+
$this->owner->andWhere([$model->treeAttribute => $id]);
37+
}
38+
3439
return $this->owner;
3540
}
3641

‎tests/NestedSetsBehaviorTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,29 @@ public function testIsLeaf()
641641
$this->assertFalse(Tree::findOne(1)->isLeaf());
642642
}
643643

644+
public function testMakeTreeWithTreeAttribute()
645+
{
646+
$dataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/clean.xml');
647+
$this->getDatabaseTester()->setDataSet($dataSet);
648+
$this->getDatabaseTester()->onSetUp();
649+
650+
$node = new MultipleTree(['name' => 'Root', 'tree' => 10]);
651+
$this->assertTrue($node->makeRoot());
652+
653+
$dataSet = $this->getConnection()->createDataSet(['multiple_tree']);
654+
$expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/test-make-tree-with-tree-attribute.xml');
655+
$this->assertDataSetsEqual($expectedDataSet, $dataSet);
656+
}
657+
658+
/**
659+
* @expectedException \yii\db\Exception
660+
*/
661+
public function testMakeRootNewExceptionIsRaisedWhenMultiTreeIsCreatedWithExistingTreeAttribute()
662+
{
663+
$node = new MultipleTree(['name' => 'Root 4', 'tree' => 10]);
664+
$node->makeRoot();
665+
}
666+
644667
/**
645668
* @inheritdoc
646669
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<dataset>
3+
<multiple_tree id="1" tree="10" lft="1" rgt="2" depth="0" name="Root"/>
4+
</dataset>

0 commit comments

Comments
 (0)
Please sign in to comment.