Closed fasi1208 closed 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.
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
The above nodes are added using the interface provided by the widget. Now I am trying to add new nodes under
Floor-1
andFloor-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.
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 errorMethod MdcNode::insert is not supported for inserting new nodes.
Inserting into table via query. This approach works and new nodes are added under my
floor-1
andfloor-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')); }`
As you can see in above Image I have encircled the new node. The node
Ali
should be likeAccurate->Floor-1->Ali
but it'sAccurate->Ali
. I have tried to manage theleft
,right
, andlevel
but it's getting complicated with adding more and more new nodes.