CakePHP - 在 HABTM 中调用模型函数
我有两个表,Pages 和 Posts,它们位于 HABTM 中,并使用 Pages_posts 连接表连接。
我的页面模型以及 HABTM 定义包含一个函数。
class Page extends AppModel
{
var $name = "Page";
......
......
function callthis()
{
return $this->find('all');;
}
}
从我的 Posts 控制器,我尝试调用该函数。
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')));
当Tag模型的find()函数可以从关联的Recipes控制器调用时,为什么我不能从关联的Posts控制器调用Page模型的callthis()函数?
I have two tables, Pages and Posts that are in HABTM and are joined using pages_posts join table.
My Page model along with the HABTM definition contains a function..
class Page extends AppModel
{
var $name = "Page";
......
......
function callthis()
{
return $this->find('all');;
}
}
From my Posts controller, I'm trying to call that function..
class PostsController extends AppController
{
....
....
function index()
{
$returned = $this->Post->Page->callthis();
}
}
This results in
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]
Further Debug output:
Code
$out = null;
if ($error) {
trigger_error('<span style="color:Red;text-align:left"><b>' . __('SQL Error:', true) . "</b> {$this->error}</span>", E_USER_WARNING);
Context
$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
Calls
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
Calling a associated model function should be possible in CakePHP right? If we checkout http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM, We can see that
$this->Recipe->Tag->find('all', array('conditions'=>array('Tag.name'=>'Dessert')));
When find() function of Tag model can be called from associated Recipes controller, Why can't I call callthis() function of Page model from associated Posts controller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题很可能出在你的人际关系上。我敢打赌,如果你这样做,
你仍然会遇到错误。
The problem is most likely in your relationships. I betch if you do
you'll still get errors.
正如斯特凡和莫雷诺指出的那样,我的人际关系确实存在问题。
根据 http://book.cakephp.org/view/1114/Plugin-Models< /a>,如果我们需要在插件中引用模型,我们需要包含插件名称和模型名称,并用点分隔。
我直接引用了模型,没有使用 Plugin.Model 作为 className。谢谢大家的宝贵时间..
As Stefan and Moreno pointed, there was indeed a problem with my relationships.
According to http://book.cakephp.org/view/1114/Plugin-Models, If we need to reference a model within your plugin, we need to include the plugin name with the model name, separated with a dot.
I referenced the models directly without using Plugin.Model as className. Thanks everyone for your time..
确保页面模型已加载到 PostsController 中(应该位于控制器文件的顶部附近,在任何函数之前):
然后您应该能够调用
Make sure the Page model is loaded in the PostsController (should be near the top of the controller file, before any functions):
Then you should be able to just call
嘿我也有同样的问题。
我添加了PluginName.ModelName,然后就解决了。
因此,在插件模型前面添加 PluginName 是非常好的做法,
谢谢,
维杰
Hey I had the same problem.
I have added the PluginName.ModelName, then it is resolved.
So adding a PluginName in front of the plugin model is very good practice,
Thanks,
Vijay