creocoder / yii2-nested-sets

The nested sets behavior for the Yii framework.
Other
446 stars 129 forks source link

makeRoot() with tree #83

Open sieulog opened 8 years ago

sieulog commented 8 years ago

Hello Everybody!

I have 2 tables below:

vocabulary: ----id ----title data example id = 1; title = news id = 2; title = products id = 3; title = gallery taxonomy: ----id ----vocabulary_id ----lft ----rgt ----depth ----title ----slug ... with vocabulary_id = tree

I config behavior tree is vocabulary_id. But when I make root with function makeRoot() with follow:

$taxonomy = new Taxonomy([ 'vocabulary_id' => 1, //news 'title' => 'News Cat A' ]); $taxonomy->makeRoot();

I seen vocabulary_id = 1; lft = 1, rgt = 2, depth = 0, title = "News Cat A' ===> OK but when I try with follow: $taxonomy = new Taxonomy([ 'vocabulary_id' => 3, //gallery 'title' => 'Gallery Cat A' ]); $taxonomy->makeRoot(); I seen vocabulary_id = 2; lft = 1, rgt = 2, depth = 0, title = "Gallery Cat A' ===> Not good. Because I want vocabulary = 3

creocoder commented 8 years ago

You can't set treeAttribute, leftAttribute, rightAttribute to custom values. They restricted by design. Due to NS nature treeAttribute should be equal to id so you can't use treeAttribute as FK for another table. I think in your case its good idea to add additional field.