PHP code example of mustang / nestedsets

1. Go to this page and download the library: Download mustang/nestedsets library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mustang / nestedsets example snippets


class User extends Model
{
    public $nestedConfig = [
        'leftKey' => 'lft',
        'rightKey' => 'rgt'
    ];

}

$userModel = new User();
$nestedObj = new Mptt($userModel);

$userModel = new User();
$nested = new Mptt($userModel, "lftkey", "rgtkey");

$userModel = new User();
$nested = new Mptt("user");

$data = ['name' => "Rose"];
$parentId = 6;
$nestedObj = new Mptt("user");
$nestedObj->insert($parentId, $data);

/**
* $data必须是数组,就算只有一个字段都必须写成数组形式
* $position是位置,支持在id为parentId的元素的子元素最前和最后插入有top和bottom两个值供选择
*/
public function insert($parentId, array $data = [], $position = "top")

/**
* 传入一个参数为要删除节点的id值
*/
$nested = new Mptt("user");
$nested->delete(8);

/**
* 将id为7的节点移动到id为2的节点上
* 如果要将id为7的节点移动为父节点那么第二个参数为0即可
*/
$nested = new Mptt("user");
$nested->moveUnder(7, 2);

//需要注意的是moveUnder支持三个参数。
//第三个参数表示移动到该父节点的其他子节点之前还是之后
public function moveUnder($id, $parentId, $position = "bottom")

//将id为7的节点移到id为8的节点旁,默认是之后
$nested = new Mptt("user");
$nested->moveNear(7, 8);

//第三个参数为移动到参考节点之前或之后如果要移到之前请传入before
public function moveNear($id, $nearId, $position = 'after')

// 重建全表
$nested = new Mptt("user");
$nested->rebuildMptt();