列出cakephp中的树结构

时间:2014-05-25 07:21:11

标签: cakephp

我需要将类别控制器中生成的类别树列出到用户添加视图以添加它们以查看用户所属的类别。这是我的类别代码有人请帮我找到我在用户add.ctp中列出它们的方法或在用户模型中实现它

public function admin_add() {
        if ($this->request->is('post')) {
            $this->ServiceCategory->create();
            if ($this->ServiceCategory->save($this->request->data)) {
                $this->Session->setFlash(__('The service category has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The service category could not be saved. Please, try again.'));
            }
        }
        $parents[0] = "[ No Parent]";
        $categoryList = $this->ServiceCategory->generateTreeList(null,null,null," -> ");
        if($categoryList){
            foreach($categoryList as $key=>$value):
                $parents[$key] = $value;
            endforeach; 
            $this->set(compact('parents'));
        }

    }

我需要与用户一起实现这一点,以便plz帮助,如果有人知道如何做到这一点

1 个答案:

答案 0 :(得分:0)

我得到了答案,以下是我需要添加到代码中的内容

//controller
    $user_Id = $this->User->getLastInsertID();
                for($i=1;$i<=$this->request->data['User']['category_count'];$i++){
                $categories = $this->request->data['User']['category_id_'.$i];

                        if($categories>0){
                            $this->User->query("insert into provider_service_categories
 (user_id,service_categories_id)  values ('$user_Id','$categories')");

                        }   
                    }
$this->loadModel('ServiceCategory');
        $serviceCategories = $this->ServiceCategory->generateTreeList(null,null,null,'&nbsp;&nbsp;&nbsp;');
        //debug($serviceCategories);
        $this->set(compact('categories','serviceCategories'));

//view.ctp
$i =0;
        foreach($serviceCategories as $key=>$value){
            $i++;
            $cb = $this->Form->checkbox('category_id_'.$i, array('value'=>$key,'id'=>'category','class'=>'default'));
            echo  "<li>$cb $value &nbsp;</li>";
            }
        echo $this->Form->hidden('category_count',array('value'=>count($serviceCategories)));

thanx反正