Generate CTreeview from database table and update a model

Suppose we want to set a category to product (or an item in general term), Also suppose categories has sub-categories (multi-level)

How to set it ?

Acorrding to http://www.yiiframework.com/wiki/523/generate-a-multi-lever-array-sub-category-for-a-menu/ we could modify the code to fit it in CTreeView, so that we needs are the below models and some method that will be called directly from the viewer

Model Category: (id,name,parent_category_Id)

public function relations() {
    return array(
        'subcategories' => array(self::HAS_MANY, 'category', 'parent_category_id'),
    );
}
 

Model Item: (id,name,category_id)

public function relations() {
    return array(
        'category' => array(self::BELONGS_TO, 'category', 'category_Id'),
    );
}
 
 
 public static function getTree() {
        if (empty(self::$asTree)) {
            $rows = self::model()->findAll('parent_category_id IS NULL');
            foreach ($rows as $item) {
                self::$asTree[] = self::getTreeItems($item);
            }
        }

        return self::$asTree;
    }

    private static function getTreeItems($modelRow) {

        if (!$modelRow)
            return;

        if (isset($modelRow->subcategories)) {
            $chump = self::getTreeItems($modelRow->subcategories);
            if ($chump != null)
                $res = array('children' => $chump, 'text' => CHtml::link($modelRow->Name, '#', array('id' => $modelRow->id)));
            else
                $res = array('text' => CHtml::link($modelRow->Name, '#', array('id' => $modelRow->id)));
            return $res;
        } else {
            if (is_array($modelRow)) {
                $arr = array();
                foreach ($modelRow as $leaves) {
                    $arr[] = self::getTreeItems($leaves);
                }
                return $arr;
            } else {
                return array('text' => CHtml::link($modelRow->Name, '#', array('id' => $modelRow->id)));
            }
        }
    }

    

Viewer: (views/item/_form.php)

<div class="row">
        <?php echo $form->labelEx($model, 'Category_id'); ?>
        <div id='parent_category_name' style='color:red;'><?php echo ($model->category) ? $model->category->name : '-'; ?></div>
        <?php
        $dataTree = Category::getTree();

        $this->widget('CTreeView', array(
            'id' => 'category-treeview',
            'data' => array(array('text' => CHtml::link('Αρχική', '#', array('id' => null)), 'children' => $dataTree)),
            'collapsed' => false,
            'htmlOptions' => array(
                'class' => 'treeview-famfamfam',
            ),
        ));
        ?>
        <?php echo $form->hiddenField($model, 'parent_category_Id'); ?>
        <?php echo $form->error($model, 'parent_category_Id'); ?>
    </div>
    
    
    
    <script>
        $('#category-treeview a').live('click', function() {
            $('#parent_category_name').html($(this).html());
            $('#item_category_id').val($(this).attr('id'));
            return false;
        });
    </script>

Now you can select the product category from the treeview :)