过去3天我一直试图解决这个问题,但没有任何结果。我已经研究了很多,当CakePHP找不到我的模型或者我的关系中有一些名字错误时,这些错误通常会出现。好吧,我试着看一切,但仍然无法找到错误的位置。 我有一个User模型,一个Project模型和一个UserRole模型,它是hasMany通过关系使用的连接表。
文件名:
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' => ''
),
);
当我尝试从User调用UserRole中的任何方法时,它会给我1064 SQL错误。关于哪里可能是问题的暗示?
到目前为止我尝试过的事情:检查是否正在加载UserRole,它是。我可以从一个元素调用UserRole函数,它们工作正常。
功能(get_related_clients在User中,另一个在UserRole中):
function get_related_clients ($id_user, $relationship_type) {
$id_names = $this->User->UserRole->get_id_users($id_user,$relationship_type);
...
}
function get_id_users ($id_agency = null,$type = null) {
$params_1 = array(
'conditions' => array('UserRole.user_id' => $id_agency)
);
$user_projects = $this->UserRole->find('all',$params_1);
$projects = array();
for ($i = 0; !empty($user_projects[$i]); $i++) {
$projects[] = $user_projects[$i]['UserRole']['project_id'];
}
$clients = array();
foreach ($projects as $project) { //pega o id de todos os usuarios que sao clientes da lista de projetos anteriores
$params_2 = array(
'conditions' => array('UserRole.project_id' => $project, 'UserRole.role' => $type)
);
$client_project = $this->UserRole->find('first',$params_2);
if ($id_agency != $client_project['UserRole']['user_id']) { $clients[] = $client_project['UserRole']['user_id']; } // voce nao pode ser cliente de voce mesmo
}
return $clients;
}
ERROR:
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调用该函数它可以工作。 :(
答案 0 :(得分:2)
您尝试在get_id_users
型号上调用UserRole
功能,而它似乎位于UserRoles
控制器中。因此错误。
<强>更新强>:
这一行:
UserRole::get_id_users() - [internal], line ??
你的日志中的清楚地表明PHP没有在get_id_users()
类中找到函数UserRole
(应该是模型根据它的名称来判断,但是因为你从一开始就错误地将这些名称弄错了在没有实际查看文件的情况下告诉。)
结论:重新启动您的应用(至少User
和UserRole
型号和控制器),这次正确命名。
答案 1 :(得分:0)
答案是:我不能从另一个控制器调用函数,除非它是用模型声明的。 :)