我跟进了文件,我不知道出了什么问题。我只需要能够使用Tree小部件来创建新节点。
这是模型:
<?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();
修改
有关该问题的更详细检查,我发现浏览器控制台在尝试添加新root时会产生以下错误:
POST http://localhost/inventory/backend/web/index.php?r=treemanager%2Fnode%2Fmanage&kvtree=-385354956 500(内部服务器错误)
此外,目标网址会生成以下输出:
{"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}"]}
答案 0 :(得分:3)
我之前遇到过这个。您应该手动在tbl_category
表中插入根记录,
eg:id=1,root=1,lft=1,rgt=1,lvl=0,name =Root
然后您可以在UI中添加节点。
我认为这是嵌套集表的init数据开始工作。
答案 1 :(得分:2)
注释掉方法isDisabled()
public function isDisabled()
{
if (Yii::$app->user->id !== 'admin') {
return true;
}
return $this->parentIsDisabled();
}
答案 2 :(得分:0)
添加:
<field name="name">purchase.order.inherit</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_form"/>
<field name="arch" type="xml">
<xpath expr="//page[@string='Products']//field[@name='order_line']//field[@name='product_qty']" position="after">
<field name="squ_meter"/>
</xpath>
</field>
到主要布局的标题(默认为main.php)。 它为我工作。