CakePhp 中带有元素的 generateTreeList()
generateTreeList() in CakePhp with Element
我的蛋糕 2.3 TreeBehaviour::generateTreeList() 遇到了一些问题;
我想在一个Element中使用这个函数生成的List。
所以元素使用 $this->requestAction();和函数 returns $treelist。
但是我在嵌入元素的地方出现以下错误:
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()
我已经验证了 100 次,在功能和所有方面都非常接近手册。我检查了 $actsAs beeing 拼写是否正确。
如果我不使用 generateTreeList() 而只是 find('list') 我会得到预期的输出。
我错在哪里? TreeBehaviour 不能与 Elements 一起使用吗?
感谢您的帮助。
请求后更新"Show your related code as well":
Posts/view.ctp:
...
echo '<hr><br><h3>Other Categories</h3>'; echo
$this->element('categorieselement');
...
Elements/categorieselement.ctp:
(我只是现在没有把更多的精力放在打印结果上而不是调试功能上。首先结果应该可以工作,而不是我会关心显示。)
<?php
$trees = $this->requestAction('/categories/tree');
debug($trees);
?>
类别控制器:
只是补充一下:即使我向 generateTreeList-Function 添加了更多选项,错误仍然存在。
如果我用 "find();" 替换 "generateTreeList();" 我得到的结果显示在 posts/view 而不是错误。所以元素和返回的 $data 正在工作。
class CategoriesController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
}
public function tree() {
$data = $this->Category->generateTreeList();
return $data;
}
}
类别模型:
class Category extends AppModel {
public $actsAs = array('Tree');
}
试试下面提到的这些 link
@Burzum 解决了(见评论)
他诊断,Tree-Behaviour 不会加载,我应该检查一下。
我将这段代码添加到控制器的功能树中,100% 确定:
if($this->Category->Behaviors->loaded('Tree') != true){
$this->Category->Behaviors->load('Tree');
}
我的蛋糕 2.3 TreeBehaviour::generateTreeList() 遇到了一些问题;
我想在一个Element中使用这个函数生成的List。 所以元素使用 $this->requestAction();和函数 returns $treelist。 但是我在嵌入元素的地方出现以下错误:
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()
我已经验证了 100 次,在功能和所有方面都非常接近手册。我检查了 $actsAs beeing 拼写是否正确。 如果我不使用 generateTreeList() 而只是 find('list') 我会得到预期的输出。
我错在哪里? TreeBehaviour 不能与 Elements 一起使用吗?
感谢您的帮助。
请求后更新"Show your related code as well":
Posts/view.ctp:
...
echo '<hr><br><h3>Other Categories</h3>'; echo
$this->element('categorieselement');
...
Elements/categorieselement.ctp: (我只是现在没有把更多的精力放在打印结果上而不是调试功能上。首先结果应该可以工作,而不是我会关心显示。)
<?php
$trees = $this->requestAction('/categories/tree');
debug($trees);
?>
类别控制器: 只是补充一下:即使我向 generateTreeList-Function 添加了更多选项,错误仍然存在。 如果我用 "find();" 替换 "generateTreeList();" 我得到的结果显示在 posts/view 而不是错误。所以元素和返回的 $data 正在工作。
class CategoriesController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
}
public function tree() {
$data = $this->Category->generateTreeList();
return $data;
}
}
类别模型:
class Category extends AppModel {
public $actsAs = array('Tree');
}
试试下面提到的这些 link
@Burzum 解决了(见评论) 他诊断,Tree-Behaviour 不会加载,我应该检查一下。
我将这段代码添加到控制器的功能树中,100% 确定:
if($this->Category->Behaviors->loaded('Tree') != true){
$this->Category->Behaviors->load('Tree');
}