0
votes

I am facing some problems with TreeBehaviour::generateTreeList() with my cake 2.3;

I want to use the List generated by this function in an Element. So the Element uses $this->requestAction(); and the function returns $treelist. But I get following Error where I embedded the Element:

Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'generateTreeList' at line 1

SQL Query: generateTreeList

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

Stack Trace

CORE\Cake\Model\Datasource\DboSource.php line 460 → PDOStatement->execute(array) CORE\Cake\Model\Datasource\DboSource.php line 426 → DboSource->_execute(string, array) CORE\Cake\Model\Datasource\DboSource.php line 666 → DboSource->execute(string, array, array) CORE\Cake\Model\Datasource\DboSource.php line 611 → DboSource->fetchAll(string, array, array) CORE\Cake\Model\Model.php line 799 → DboSource->query(string, array, AppModel) APP\Controller\CategoriesController.php line 14 → Model->__call(string, array) APP\Controller\CategoriesController.php line 14 → AppModel->generateTreeList() [internal function] → CategoriesController->tree() CORE\Cake\Controller\Controller.php line 486 → ReflectionMethod->invokeArgs(CategoriesController, array) CORE\Cake\Routing\Dispatcher.php line 187 → Controller->invokeAction(CakeRequest) CORE\Cake\Routing\Dispatcher.php line 162 → Dispatcher->_invoke(CategoriesController, CakeRequest, CakeResponse) CORE\Cake\Core\Object.php line 104 → Dispatcher->dispatch(CakeRequest, CakeResponse, array) APP\View\Elements\categorieselement.ctp line 4 → Object->requestAction(string) CORE\Cake\View\View.php line 945 → include(string) CORE\Cake\View\View.php line 907 → View->_evaluate(string, array) CORE\Cake\View\View.php line 1208 → View->_render(string, array) CORE\Cake\View\View.php line 414 → View->_renderElement(string, array, array) APP\View\Posts\view.ctp line 197 → View->element(string) CORE\Cake\View\View.php line 945 → include(string) CORE\Cake\View\View.php line 907 → View->_evaluate(string, array) CORE\Cake\View\View.php line 471 → View->_render(string) CORE\Cake\Controller\Controller.php line 948 → View->render(null, null) CORE\Cake\Routing\Dispatcher.php line 194 → Controller->render()

I have verfied 100 times beeing very close to the manual regarding the function and everything. I have checked $actsAs beeing spelled correctly. If I don't use generateTreeList() but just find('list') I get output as expected.

Where is my fault? Is TreeBehaviour not usable with with Elements?

Thanks for any Help.

Update after request "Show your related code as well":

Posts/view.ctp:

... 
echo '<hr><br><h3>Other Categories</h3>'; echo
$this->element('categorieselement'); 
...

Elements/categorieselement.ctp: (I just didn't put more on printing the result than the debug function right now. First the result should work, than I will care about displaying.)

<?php 
$trees = $this->requestAction('/categories/tree');
debug($trees); 
?>

CategoriesController: Just to add: Even if I add more Options to the generateTreeList-Function, the Error remains the same. If I replace "generateTreeList();" by "find();" I get a result displayed in posts/view and not an Error. So the element and returning $data is working.

class CategoriesController extends AppController {

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

    public function tree() {
        $data = $this->Category->generateTreeList();
        return $data;
    }
}

Category Model:

class Category extends AppModel {

    public $actsAs = array('Tree');

}
2
Show your related code as well. - floriank
The requested Code is provided in the question. - Andreas Witte
Try not to use requestAction() and pass the data down from the controller instead, e.g. via component. - mark
The code seems to be correct, however "SQL Query: generateTreeList" means that the behavior is not loaded for that model instance. The error is the default that happens with a behavior can't be accessed. Try debug($this->Category-Behaviors->loaded('Tree')); before calling generateTreeList(), if it return false try calling Behaviors->load('Tree'); - floriank
How? Is there somewhere an tutorial for component? - Andreas Witte

2 Answers

-1
votes

@Burzum solved it (see Comment) He diagnosed, the Tree-Behaviour would not be loaded and I should check for it.

I added this code into the function tree in the controller, to be 100% shure:

if($this->Category->Behaviors->loaded('Tree') != true){
    $this->Category->Behaviors->load('Tree');
}