CakePHP的 - 在HABTM调用模型函数
-
26-09-2019 - |
题
我有两个表,页和文章是在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')));
当发现()标签模型的功能,可以从相关的食谱控制器叫,我为什么不能叫callthis()页面模型的功能,从相关的帖子控制器?
解决方案 2
由于斯特凡和莫雷诺指出,确实有问题,与我的关系。
根据 http://book.cakephp.org/view/1114/Plugin -Models ,如果我们需要你的插件中引用的模型,我们需要包括与模型名的插件名称,用点号分隔。
我所引用的模型的情况下直接使用Plugin.Model作为类名。感谢大家对你的时间..
其他提示
问题是最有可能在你的人际关系。我betch如果你
$this->Post->Page->find('all');
你还是会出现错误。
确认页面模型在PostsController加载(应靠近控制器文件的顶部,之前的任何函数):
var $uses = array('Post', 'Page');
那么你应该能够公正来电
$this->Page->callthis();
嘿我有同样的问题。
我已经添加了PluginName.ModelName,那么它被解决。 因此,在插件模型的前添加PluginName是非常好的做法,
谢谢, 维杰
不隶属于 StackOverflow