Add node to Tree

727
November 23, 2016, at 9:36 PM

I decided to use nested sets model. My table has columns: id, name, lft, rgt, parent_id

My function to add new node:

public function actionAdd($parent = null)
{
    $model = Menu::find()->where(['id' => $parent])->one();
    if ( $model )
    {
        $connection = \Yii::$app->db;
        $rgt = $model->rgt;
        $connection->createCommand('UPDATE category SET rgt = rgt+2 WHERE rgt > '.$rgt)->execute();
        $connection->createCommand('UPDATE category SET lft = lft+2 WHERE lft > '.$rgt)->execute();
    }
    else 
    {
        $rgt = 1;
    }
    $m2 = new Menu;
    $m2->lft = $rgt;
    $m2->rgt = (int)$rgt + 1; 
    $m2->parent_id = $parent;
    $m2->name = 'test'.$rgt;
    $m2->save();
}

Nodes are added in various locations inconsistent with parent_id. My question is what am I doing wrong ?

Query to show:

    $sql = "SELECT node.*, (COUNT(parent.id) - 1) AS depth
    FROM category AS node, category AS parent
    WHERE (node.lft BETWEEN parent.lft AND parent.rgt)
    GROUP BY node.id
    ORDER BY node.lft";

I'll just add that as sort with jQuery plugin ( http://mjsarfatti.com/sandbox/nestedSortable ) and ajax store. Its display good.

My jQuery code: http://pastebin.com/raw.php?i=q7LALNTP

READ ALSO
javascript/jquery: on click copy relevant array items to new array

javascript/jquery: on click copy relevant array items to new array

I think I'm missing an obvious answer. I have an array of arrays, like this.

709
Convert datetime with Jquery

Convert datetime with Jquery

Well, i'm modifying a board system, and i have this datetime format:.

559
Jquery datatable features shows in delay

Jquery datatable features shows in delay

I am using datatable ver. 1.

435