Cakephp hasMany-through关系中的SQL错误1064

Cakephp hasMany-through关系中的SQL错误1064,cakephp,Cakephp,在过去的三天里,我一直在努力解决这个问题,但什么都没有发生。我已经研究了很多,当CakePHP找不到我的模型或者我的关系中有一些名字错误时,通常会出现这种错误。我试着看了所有的东西,但还是找不到错误在哪里。 我有一个用户模型、一个项目模型和一个UserRole模型,这是hasMany-through关系使用的联接表 文件名: user.php project.php user_role.php 型号: class UserRole extends AppModel { ... var $belo

在过去的三天里,我一直在努力解决这个问题,但什么都没有发生。我已经研究了很多,当CakePHP找不到我的模型或者我的关系中有一些名字错误时,通常会出现这种错误。我试着看了所有的东西,但还是找不到错误在哪里。 我有一个用户模型、一个项目模型和一个UserRole模型,这是hasMany-through关系使用的联接表

文件名:

user.php project.php user_role.php

型号:

class UserRole extends AppModel {
...
var $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Project' => array(
        'className' => 'Project',
        'foreignKey' => 'project_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);
class User extends AppModel {
...
var $hasMany = array(
...
    'UserRole' => array(
        'className' => 'UserRole',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),

);
class Project extends AppModel {
...
var $hasMany = array(
...
    'UserRole' => array(
        'className' => 'UserRole',
        'foreignKey' => 'project_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
);
当我尝试从UserRole从User调用任何方法时,它给出了1064sql错误。有没有关于问题出在哪里的线索

到目前为止我已经尝试过的事情:检查是否正在加载UserRole,并且它是。我可以从元素调用UserRole函数,它们工作正常

函数(与get_相关的_客户端位于Users中,另一个位于UserRole中):

错误:

 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 'get_id_users' at line 1
DboSource::showQuery() - CORE/cake/libs/model/datasources/dbo_source.php, line 684
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
UserRole::get_id_users() - [internal], line ??
UsersController::get_related_clients() - APP/controllers/users_controller.php, line 71
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 204
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171
Object::requestAction() - CORE/cake/libs/object.php, line 95
include - APP/views/elements/client_list.ctp, line 2
View::_render() - CORE/cake/libs/view/view.php, line 731
View::element() - CORE/cake/libs/view/view.php, line 392
include - APP/views/projects/index.ctp, line 45
View::_render() - CORE/cake/libs/view/view.php, line 731
更新:
如果我通过requestAction调用该函数,它会工作:(

您试图调用
UserRole
模型上的
get\u id\u users
函数,而它似乎位于
UserRoles
控制器中。因此出现错误

更新

这一行:

UserRole::get_id_users() - [internal], line ??
在您的日志中清楚地显示,PHP没有在
UserRole
类中找到函数
get\u id\u users()
(从名称来看,这应该是一个模型,但由于您从一开始就弄错了名称,因此在不查看文件的情况下很难判断)


结论:重新制作你的应用程序(至少是
用户
用户角色
模型和控制器),这次正确命名。

答案是:我不能从另一个控制器调用函数,除非它与模型一起声明。

发布你的控制器代码(在你查询模型的地方)。和错误描述,带查询。我认为调用是正确的。如果我更改$this->User->UserRole->get\u id\u users($id\u User,$relationship\u type);要$this->User->UserRoles->get\u id\u users($id\u User,$relationship\u type);它给出了“致命错误:对非对象调用成员函数get_id_users()”:(你的
get_id_users()在哪里)
函数定义?它应该在
UserRole
模型中,而不是在
UserRoles
controller中。它是在UserRole中定义的。我错误地键入了UserRole的名称,并在其末尾添加了一个“s”。它们的模型/控制器/视图是由Bake创建的。如果您的模型被称为
UserRoles
,为什么要调用
$this->User->UserRole
?在答案中查看我的更新。结论:Cake严重依赖于命名约定,像“错误添加s”这样的错误可能会让事情变得更糟。我建议你重新设置你的应用程序,然后再试一次,否则我们将永远陷入循环。我在我的答案上错误地添加了“s”,而不是在我的应用程序上。无论如何,我重新设置了用户和用户角色,我仍然我什么都没有。我将尝试从0构建另一个应用程序,看看是否可以让它工作。
UserRole::get_id_users() - [internal], line ??