【问题标题】:Yii Extension - nestedsetbehavior not savingYii 扩展 - 嵌套集行为不保存
【发布时间】:2013-06-28 22:26:37
【问题描述】:

我正在尝试保存扩展名为“NestedSetBehvaior”的节点:http://www.yiiframework.com/extension/nestedsetbehavior/

但它没有在数据库中保存任何内容..

我尝试使用扩展附带的架构 (extensions/yiiext/behaviors/trees/schema.sql)..

我还添加了未包含的“标题”列。

然后我用 Gii 生成了控制器、模型和 CRUD,并将其添加到新创建的模型中:Category.php

    public function behaviors()
    {
        return array(
            'nestedSetBehavior'=>array(
                'class'=>'ext.yiiext.behaviors.model.trees.NestedSetBehavior',
                'leftAttribute'=>'lft',
                'rightAttribute'=>'rgt',
                'levelAttribute'=>'level',
            ),
        );
    }

我还将 NestedSetBehavior.php 放在 protected/extensions/yiiext/behaviors/model/trees/ 中

然后我将它添加到控制器 indexAction:

$root=new Category;
$root->title='Mobile Phones';
$root->saveNode();

可能出了什么问题?

另外,您会推荐哪种方法为多个用户(3000+)存储多棵树?想象一棵深度无限的树..

【问题讨论】:

    标签: php frameworks yii nested-sets nested-set-model


    【解决方案1】:

    我自己设法找到了解决方案。 问题出在“类别”模型中。 我更改了验证规则,因此不需要“lft”、“rgt”和“level”,因为它们是由 NestedSetBehavior 自动添加的。

    更改前:

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('lft, rgt, level', 'required'),
            array('level', 'numerical', 'integerOnly'=>true),
            array('root, lft, rgt', 'length', 'max'=>10),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, root, lft, rgt, level', 'safe', 'on'=>'search'),
        );
    }
    

    改动后:

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            //array('lft, rgt, level', 'required'),
            array('level', 'numerical', 'integerOnly'=>true),
            array('root, lft, rgt', 'length', 'max'=>10),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, root, lft, rgt, level', 'safe', 'on'=>'search'),
        );
    }
    

    现在运行良好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      相关资源
      最近更新 更多