1

我正在尝试使用扩展名“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+)存储多个树?想象一棵深度无限的树..

4

1 回答 1

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'),
    );
}

它现在工作得很好。

于 2013-06-29T14:34:48.083 回答