我有两个表,分页和帖子在HABTM中,并使用pages_posts连接表连接。
我的页面模型和HABTM定义包含一个函数..
class Page extends AppModel
{
var $name = "Page";
......
......
function callthis()
{
return $this->find('all');;
}
}
从我的帖子控制器,我正试图调用该功能..
class PostsController extends AppController
{
....
....
function index()
{
$returned = $this->Post->Page->callthis();
}
}
这导致
Warning (512): SQL Error: 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 'threadedpages' at line 1 [CORE/cake/libs/model/datasources/dbo_source.php, line 681]
进一步调试输出:
代码
$out = null;
if ($error) {
trigger_error('<span style="color:Red;text-align:left"><b>' . __('SQL Error:', true) . "</b> {$this->error}</span>", E_USER_WARNING);
上下文
$sql = "threadedpages"
$error = "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 'threadedpages' at line 1"
$out = null
呼叫
DboSource::showQuery() - CORE/cake/libs/model/datasources/dbo_source.php, line 681
DboSource::execute() - CORE/cake/libs/model/datasources/dbo_source.php, line 266
DboSource::fetchAll() - CORE/cake/libs/model/datasources/dbo_source.php, line 410
DboSource::query() - CORE/cake/libs/model/datasources/dbo_source.php, line 364
Model::call__() - CORE/cake/libs/model/model.php, line 502
Overloadable::__call() - CORE/cake/libs/overloadable_php5.php, line 50
AppModel::threadedpages() - [internal], line ??
PostsController::admin_index() - CORE/plugins/cakey/controllers/posts_controller.php, line 11
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 204
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171
[main] - APP/webroot/index.php, line 83
在CakePHP中应该可以调用关联的模型函数吗?如果我们结帐http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM,我们可以看到
$this->Recipe->Tag->find('all', array('conditions'=>array('Tag.name'=>'Dessert')));
当可以从关联的Recipes控制器调用Tag模型的find()函数时,为什么我不能从关联的Posts控制器调用Page模型的callthis()函数?
答案 0 :(得分:1)
问题很可能出在你的人际关系中。我打赌,如果你做的话
$this->Post->Page->find('all');
你仍会收到错误。
答案 1 :(得分:1)
斯特凡和莫雷诺指出,我的人际关系确实存在问题。
根据http://book.cakephp.org/view/1114/Plugin-Models,如果我们需要在插件中引用模型,我们需要将插件名称包含在模型名称中,并用点分隔。
我直接引用模型而不使用Plugin.Model作为className。谢谢大家的时间..
答案 2 :(得分:0)
确保在PostsController中加载Page模型(在任何函数之前应该靠近控制器文件的顶部):
var $uses = array('Post', 'Page');
然后你应该可以打电话
$this->Page->callthis();
答案 3 :(得分:0)
我添加了PluginName.ModelName,然后解析了。 因此,在插件模型前面添加一个PluginName是非常好的做法,
谢谢, 维杰