Yii2 Kartik 树管理器无法创建节点

Yii2 Kartik Tree manager could not create node

我跟进了文档,但我不知道哪里出了问题。我只需要能够使用树小部件来创建新节点。

这是型号:

<?php

namespace common\models;

use Yii;
use creocoder\nestedsets\NestedSetsBehavior;

/**
 * This is the model class for table "categories".
 *
 * @property integer $id
 * @property integer $root
 * @property integer $lft
 * @property integer $rgt
 * @property integer $lvl
 * @property string $name
 * @property string $description
 * @property string $icon
 * @property integer $icon_type
 * @property integer $active
 * @property integer $selected
 * @property integer $disabled
 * @property integer $readonly
 * @property integer $visible
 * @property integer $collapsed
 * @property integer $movable_u
 * @property integer $movable_d
 * @property integer $movable_l
 * @property integer $movable_r
 * @property integer $removable
 * @property integer $removable_all
 *
 * @property CategoryItems[] $categoryItems
 */
class Categories extends \yii\db\ActiveRecord
{
  use \kartik\tree\models\TreeTrait {
        isDisabled as parentIsDisabled; // note the alias
    }

    public static $treeQueryClass; // change if you need to set your own TreeQuery
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'categories';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['root', 'lft', 'rgt', 'lvl', 'icon_type', 'active', 'selected', 'disabled', 'readonly', 'visible', 'collapsed', 'movable_u', 'movable_d', 'movable_l', 'movable_r', 'removable', 'removable_all'], 'integer'],
            [['lft', 'rgt', 'lvl', 'name'], 'required'],
            [['description'], 'string'],
            [['name'], 'string', 'max' => 60],
            [['icon'], 'string', 'max' => 255]
        ];


    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'root' => Yii::t('app', 'Root'),
            'lft' => Yii::t('app', 'Lft'),
            'rgt' => Yii::t('app', 'Rgt'),
            'lvl' => Yii::t('app', 'Lvl'),
            'name' => Yii::t('app', 'Name'),
            'description' => Yii::t('app', 'Description'),
            'icon' => Yii::t('app', 'Icon'),
            'icon_type' => Yii::t('app', 'Icon Type'),
            'active' => Yii::t('app', 'Active'),
            'selected' => Yii::t('app', 'Selected'),
            'disabled' => Yii::t('app', 'Disabled'),
            'readonly' => Yii::t('app', 'Readonly'),
            'visible' => Yii::t('app', 'Visible'),
            'collapsed' => Yii::t('app', 'Collapsed'),
            'movable_u' => Yii::t('app', 'Movable U'),
            'movable_d' => Yii::t('app', 'Movable D'),
            'movable_l' => Yii::t('app', 'Movable L'),
            'movable_r' => Yii::t('app', 'Movable R'),
            'removable' => Yii::t('app', 'Removable'),
            'removable_all' => Yii::t('app', 'Removable All'),
        ];
    }

    public function behaviors() {
        return [
            'tree' => [
                'class' => NestedSetsBehavior::className(),
                // 'treeAttribute' => 'tree',
                // 'leftAttribute' => 'lft',
                // 'rightAttribute' => 'rgt',
                 'depthAttribute' => 'lvl',
            ],
        ];
    }
    public function transactions()
    {
        return [
            self::SCENARIO_DEFAULT => self::OP_ALL,
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCategoryItems()
    {
        return $this->hasMany(CategoryItems::className(), ['category_id' => 'id']);
    }

    /**
     * @inheritdoc
     * @return CategoriesQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new CategoriesQuery(get_called_class());
    }
     public function isDisabled()
    {
        if (Yii::$app->user->id !== 'admin') {
            return true;
        }
        return $this->parentIsDisabled();
    }
}

这是视图-index.php-

<?php

use yii\helpers\Html;
use yii\grid\GridView;
use common\models\Categories;
use kartik\tree\TreeView;

/* @var $this yii\web\View */
/* @var $searchModel common\models\CategoriesSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', 'Categories');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="categories-index">

    <h1><?= Html::encode($this->title) ?></h1>
    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>
        <?= Html::a(Yii::t('app', 'Create Categories'), ['create'], ['class' => 'btn btn-success']) ?>
    </p>

    <?php /*= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'id',
            'root',
            'lft',
            'rgt',
            'lvl',
            // 'name',
            // 'description:ntext',
            // 'icon',
            // 'icon_type',
            // 'active',
            // 'selected',
            // 'disabled',
            // 'readonly',
            // 'visible',
            // 'collapsed',
            // 'movable_u',
            // 'movable_d',
            // 'movable_l',
            // 'movable_r',
            // 'removable',
            // 'removable_all',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); */ ?>
    <?php
    echo TreeView::widget([
    // single query fetch to render the tree
    // use the Product model you have in the previous step
    'query' => Categories::find()->addOrderBy('root, lft'), 
    'headingOptions' => ['label' => 'Categories'],
    'fontAwesome' => false,     // optional
    'isAdmin' => true,         // optional (toggle to enable admin mode)
    'displayValue' => 1,        // initial display value
    'softDelete' => true,       // defaults to true
    'cacheSettings' => [        
        'enableCache' => false   // defaults to true
    ]
]);
    ?>

</div>

上面给出了以下屏幕截图:

我尝试直接从 CategoriesController 的索引操作中使用 creocoder yii2-nested-sets,但它也没有创建任何东西:

$countries = new Categories(['name' => 'bg']);
 $countries->makeRoot();

编辑

对该问题进行更详细的检查,我发现在尝试添加新根时浏览器控制台生成以下错误:

POST http://localhost/inventory/backend/web/index.php?r=treemanager%2Fnode%2Fmanage&kvtree=-385354956 500 (Internal Server Error)

此外,目标 URL 生成以下输出:

{"name":"Exception","message":"This operation is not allowed.","code":0,"type":"yii\base\InvalidCallException","file":"C:\xampp-2\htdocs\inventory\vendor\kartik-v\yii2-tree-manager\controllers\NodeController.php","line":66,"stack-trace":["#0 C:\xampp-2\htdocs\inventory\vendor\kartik-v\yii2-tree-manager\controllers\NodeController.php(143): kartik\tree\controllers\NodeController::checkValidRequest()","#1 [internal function]: kartik\tree\controllers\NodeController->actionManage()","#2 C:\xampp-2\htdocs\inventory\vendor\yiisoft\yii2\base\InlineAction.php(55): call_user_func_array(Array, Array)","#3 C:\xampp-2\htdocs\inventory\vendor\yiisoft\yii2\base\Controller.php(151): yii\base\InlineAction->runWithParams(Array)","#4 C:\xampp-2\htdocs\inventory\vendor\yiisoft\yii2\base\Module.php(455): yii\base\Controller->runAction('manage', Array)","#5 C:\xampp-2\htdocs\inventory\vendor\yiisoft\yii2\web\Application.php(84): yii\base\Module->runAction('treemanager/nod...', Array)","#6 C:\xampp-2\htdocs\inventory\vendor\yiisoft\yii2\base\Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))","#7 C:\xampp-2\htdocs\inventory\backend\web\index.php(18): yii\base\Application->run()","#8 {main}"]}

注释掉方法 isDisabled()

 public function isDisabled()
    {
        if (Yii::$app->user->id !== 'admin') {
            return true;
        }
        return $this->parentIsDisabled();
    }

http://demos.krajee.com/tree-manager#comment-2288987974

添加:

<?= Html::csrfMetaTags() ?>

到主布局的页眉(默认 main.php)。 它对我有用。

我以前遇到过这种情况。你应该在 tbl_category table 中手动插入一条 Root 记录,

eg:id=1,root=1,lft=1,rgt=1,lvl=0,name =Root

那么你可以在UI中添加节点。

我认为这是嵌套集的初始数据 table 开始工作。