Я пытался решить эту проблему последние 3 дня, но ничего не вышло.Я много исследовал, и эти ошибки обычно появляются, когда CakePHP не может найти мою модель или у меня неверное имя в моих отношениях.Ну, я пытался смотреть на все, но все еще не мог найти, где ошибка.У меня есть модель User, модель Project и модель 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, он выдаёт мне ошибку 1064 SQL.Любой намек на то, где может быть проблема?
Вещи, которые я пробовал до сих пор: проверял, загружается ли UserRole, и это так.И я могу вызывать функции UserRole из элемента, они работают нормально.
FUNCTIONS (get_related_clients в Users, а другой в 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, она работает.(