kartik-v / yii2-tree-manager

An advanced tree management module using nested sets for Yii 2.
http://demos.krajee.com/tree-manager
Other
150 stars 107 forks source link

How to add node from another model #268

Closed fasi1208 closed 4 years ago

fasi1208 commented 4 years ago

Hi, I have added some nodes into my tree. Now I want to add some more nodes into it. This addition of new nodes will be performed via another model. Below is my hierarchy

image

The above nodes are added using the interface provided by the widget. Now I am trying to add new nodes under Floor-1 and Floor-2 but I am using another model. i.e. I want to add customers under my floors. I have a customer-node module through which I am trying to add new nodes under it.

For adding them I have tried 2 approaches.

  1. Calling model and then saving data into it $n = new MdcNode; $n->root = $root; $n->lft = $lft; $n->rgt = $rgt; $n->lvl = $lvl; $n->name = $name; $n->save(); With this I am facing error Method MdcNode::insert is not supported for inserting new nodes.

  2. Inserting into table via query. This approach works and new nodes are added under my floor-1 and floor-2. But in actual there parent node is different.

`$root = MdcNode::idToid($m->node_id); $lft = MdcNode::idTolft($m->node_id)+1; $rgt = Mdcnode::idTorgt($m->node_id)+1; $lvl = MdcNode::idTolvl($m->node_id)+1; $name = $m->customer_name; $icon = "user"; $customer_id = $m->customer_id; $insert = "INSERT INTO mdc_db.mdc_node ( root, lft, rgt, lvl, name, icon, customer_id ) VALUES ( '$root', '$lft', '$rgt', '$lvl', '$name', '$icon', '$customer_id' )"; $root_rgt = MdcNode::idTorgt($root)+2 $update_node = "UPDATE mdc_db.mdc_node SET
rgt = $root_rgt WHERE id = $root";

if ($m->save() && Yii::$app->db->createCommand($insert)->execute() && Yii::$app->db->createCommand($update_node)->execute()) { return $this->redirect(Url::toRoute('/mdcnodecustomer')); }`

image

As you can see in above Image I have encircled the new node. The node Ali should be like Accurate->Floor-1->Ali but it's Accurate->Ali. I have tried to manage the left, right, and level but it's getting complicated with adding more and more new nodes.

kartik-v commented 4 years ago

The tree node manager works based on just one tree table in the background (it is dependent on the yii2-nested-sets extension). If you need to use this extension you need to configure your TREE MODEL DB TABLE correctly and filter it via appropriate db columns to achieve your use case. Summary - you can use only one tree model for the nested set to function.