diff --git a/src/NestedSetsBehavior.php b/src/NestedSetsBehavior.php index db1569e..d55bff3 100644 --- a/src/NestedSetsBehavior.php +++ b/src/NestedSetsBehavior.php @@ -361,6 +361,8 @@ protected function beforeInsertRootNode() { if ($this->treeAttribute === false && $this->owner->find()->roots()->exists()) { throw new Exception('Can not create more than one root when "treeAttribute" is false.'); + } elseif ($this->treeAttribute && $this->owner->getAttribute($this->treeAttribute) && $this->owner->find()->roots($this->owner->getAttribute($this->treeAttribute))->exists()) { + throw new Exception("Can not create more than one root with {$this->treeAttribute} ".$this->owner->getAttribute($this->treeAttribute)); } $this->owner->setAttribute($this->leftAttribute, 1); @@ -399,7 +401,7 @@ protected function beforeInsertNode($value, $depth) */ public function afterInsert() { - if ($this->operation === self::OPERATION_MAKE_ROOT && $this->treeAttribute !== false) { + if ($this->operation === self::OPERATION_MAKE_ROOT && $this->treeAttribute !== false && !$this->owner->getAttribute($this->treeAttribute)) { $this->owner->setAttribute($this->treeAttribute, $this->owner->getPrimaryKey()); $primaryKey = $this->owner->primaryKey(); diff --git a/src/NestedSetsQueryBehavior.php b/src/NestedSetsQueryBehavior.php index 7368856..ea4ffc2 100644 --- a/src/NestedSetsQueryBehavior.php +++ b/src/NestedSetsQueryBehavior.php @@ -21,9 +21,10 @@ class NestedSetsQueryBehavior extends Behavior { /** * Gets the root nodes. + * @param mixed $id the optional tree id. Default is `null` to query for all root nodes. * @return \yii\db\ActiveQuery the owner */ - public function roots() + public function roots($id = null) { $model = new $this->owner->modelClass(); @@ -31,6 +32,10 @@ public function roots() ->andWhere([$model->leftAttribute => 1]) ->addOrderBy([$model->primaryKey()[0] => SORT_ASC]); + if ($id!==null && $model->treeAttribute!==false) { + $this->owner->andWhere([$model->treeAttribute => $id]); + } + return $this->owner; } diff --git a/tests/NestedSetsBehaviorTest.php b/tests/NestedSetsBehaviorTest.php index 1867275..ce7c6fa 100644 --- a/tests/NestedSetsBehaviorTest.php +++ b/tests/NestedSetsBehaviorTest.php @@ -641,6 +641,29 @@ public function testIsLeaf() $this->assertFalse(Tree::findOne(1)->isLeaf()); } + public function testMakeTreeWithTreeAttribute() + { + $dataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/clean.xml'); + $this->getDatabaseTester()->setDataSet($dataSet); + $this->getDatabaseTester()->onSetUp(); + + $node = new MultipleTree(['name' => 'Root', 'tree' => 10]); + $this->assertTrue($node->makeRoot()); + + $dataSet = $this->getConnection()->createDataSet(['multiple_tree']); + $expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/test-make-tree-with-tree-attribute.xml'); + $this->assertDataSetsEqual($expectedDataSet, $dataSet); + } + + /** + * @expectedException \yii\db\Exception + */ + public function testMakeRootNewExceptionIsRaisedWhenMultiTreeIsCreatedWithExistingTreeAttribute() + { + $node = new MultipleTree(['name' => 'Root 4', 'tree' => 1]); + $node->makeRoot(); + } + /** * @inheritdoc */ diff --git a/tests/data/test-make-tree-with-tree-attribute.xml b/tests/data/test-make-tree-with-tree-attribute.xml new file mode 100644 index 0000000..c03cae5 --- /dev/null +++ b/tests/data/test-make-tree-with-tree-attribute.xml @@ -0,0 +1,4 @@ + + + +