cakephp Tree Behaivor:在非对象上调用成员函数generateTreeList()

时间:2014-05-09 20:31:25

标签: php cakephp jstree

我按照文档为我的项目生成树,但我只得到错误。

我的SiteController.php

<?php
App::uses('AppController', 'Controller');
/**
 * Sites Controller
 *
 * @property Site $Site
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class SitesController extends AppController {

    public $name = 'sites';

/**
 * Components
 *
 * @var array
 */
    public $components = array('Paginator', 'Session', 'RequestHandler');
    public $helpers = array('Wysiwyg.Wysiwyg' => array('editor' => 'Ck'), 'Form');


    public function beforeFilter() {
        parent::beforeFilter();
    }

/**
 * index method
 *
 * @return void
 */
    public function index() {
        $data = $this->Site->generateTreeList(
          null,
          null,
          null,
          '&nbsp;&nbsp;&nbsp;'
        );
        debug($data); die;

        #$this->Site->recursive = 0;
        #$this->set('sites', $this->Paginator->paginate());
    }

    public function getSiteTree()
        {
        #$this->layout = 'ajax';
        if($this->request->is('ajax'))
            {
            $parent = $_REQUEST["parent"];

            $data = array();

            $states = array(
                "success",
                "info",
                "danger",
                "warning"
            );

            if ($parent == "#") {
                for($i = 1; $i < rand(4, 7); $i++) {
                    $data[] = array(
                        "id" => "node_" . time() . rand(1, 100000), 
                        "text" => "Node #" . $i, 
                        "icon" => "fa fa-folder icon-lg icon-" . ($states[rand(0, 3)]),
                        "children" => true, 
                        "type" => "root"
                    );
                }
            } else {
                if (rand(1, 5) === 3) {
                    $data[] = array(
                        "id" => "node_" . time() . rand(1, 100000), 
                        "icon" => "fa fa-file fa-large icon-default",
                        "text" => "No childs ", 
                        "state" => array("disabled" => true),
                        "children" => false
                    );
                } else {
                    for($i = 1; $i < rand(2, 4); $i++) {
                        $data[] = array(
                            "id" => "node_" . time() . rand(1, 100000), 
                            "icon" => ( rand(0, 3) == 2 ? "fa fa-file icon-lg" : "fa fa-folder icon-lg")." icon-" . ($states[rand(0, 3)]),
                            "text" => "Node " . time(), 
                            "children" => ( rand(0, 3) == 2 ? false : true)
                        );
                    }
                }
            }
            $this->autoRender = false;
            $this->RequestHandler->respondAs('json');
            echo json_encode($data);
            }
        }

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function view($id = null) {
        if (!$this->Site->exists($id)) {
            throw new NotFoundException(__('Invalid site'));
        }
        $options = array('conditions' => array('Site.' . $this->Site->primaryKey => $id));
        $this->set('site', $this->Site->find('first', $options));
    }

/**
 * add method
 *
 * @return void
 */
    public function add() {
        if ($this->request->is('post')) {
            $this->Site->create();
            if ($this->Site->save($this->request->data)) {
                $this->Session->setFlash(__('The site has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The site could not be saved. Please, try again.'));
            }
        }
    }

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function edit($id = null) {
        if (!$this->Site->exists($id)) {
            throw new NotFoundException(__('Invalid site'));
        }
        if ($this->request->is(array('post', 'put'))) {
            if ($this->Site->save($this->request->data)) {
                $this->Session->setFlash(__('The site has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The site could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Site.' . $this->Site->primaryKey => $id));
            $this->request->data = $this->Site->find('first', $options);
        }
    }

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function delete($id = null) {
        $this->Site->id = $id;
        if (!$this->Site->exists()) {
            throw new NotFoundException(__('Invalid site'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Site->delete()) {
            $this->Session->setFlash(__('The site has been deleted.'));
        } else {
            $this->Session->setFlash(__('The site could not be deleted. Please, try again.'));
        }
        return $this->redirect(array('action' => 'index'));
    }}

和我的模型:Site.php

<?php
App::uses('AppModel', 'Model');
/**
 * Site Model
 *
 */
class Site extends AppModel {
    public $actsAs = array('Tree');
/**
 * Primary key field
 *
 * @var string
 */
    public $primaryKey = 'id_site';

/**
 * Display field
 *
 * @var string
 */
    public $displayField = 'title';

/**
 * Validation rules
 *
 * @var array
 */
    public $validate = array(
        'id_site' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'title' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );
}

和错误:

Fatal Error

Error: Call to a member function generateTreeList() on a non-object
File: /var/www/app/Controller/SitesController.php
Line: 33

Notice: If you want to customize this error message, create app/View/Errors/fatal_error.ctp

0 个答案:

没有答案