我对 CakePHP 2.1.1 项目有一个奇怪的问题。
问题是,如果我在 Competition 模型上调用 find()(在下面的代码中),紧随其后我在另一个模型中调用自定义方法,操作将失败并出现以下错误:
Error: SQLSTATE[42000]: Syntax error or access violation: 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 'getAddedPlayersIds' at line 1
SQL Query: getAddedPlayersIds
我的 CompetitionsController::view() 代码如下:
public function view($id = null) {
$this->layout = 'competition';
$this->Competition->id = $id;
if (!$this->Competition->exists()) {
throw new NotFoundException(__('Invalid competition'));
}
$active = $this->Competition->field('active', array('id' => $id));
if (!$active) {
$this->redirect(array('controller' => 'pages', 'action' => 'display', 'competition_inactive'));
}
//THIS IS WHERE IT BECOMES STRANGE:
$competition = $this->Competition->find('first', array('conditions' => array('Competition.id' => $id)));
$addedPlayersIds = $this->CompetitionsPlayer->getAddedPlayersIds($id);
//SOME CODE INTENTIONALLY REMOVED HERE!!!
$this->set('playerShops', $playerShops);
$this->set('messages', $messages);
$this->set('competition', $this->Competition->read(null, $id));
//render() IS CALLED FOR A SPECIFIC REASON
$this->render();
}
这是该CompetitionsPlayer::getAddedPlayersIds()
方法的样子:
public function getAddedPlayersIds($competitionId = null){
if(!isset($competitionId)) {
return false;
}
$this->displayField = 'player_id';
return $this->find('list', array('conditions' => array('competition_id' => $competitionId)));
}
我最初认为它会以某种方式破坏,因为我将 Model::find() 操作的返回分配给变量名,即“竞争”,但更有趣的是,如果我移动 Competition::find () 在 CompetitionsPlayer::getAddedPlayersIds() 之后调用它有效!
此外,如果我重命名变量,它有时会起作用,有时不会......!?
我仍然无法弄清楚这是什么时候,因为我目前没有时间进一步研究。请注意,根据调试信息,在数据库上执行的查询是:
getAddedPlayersIds
这是我正在调用的函数的名称!
正如我所提到的,我已经知道解决方法 - 只需交换两个调用。但是如果第一个是在几秒钟之前并且没有其他方法来实现手头的任务怎么办?
我现在只想知道为什么会这样?