I'm having a strange issue with a CakePHP 2.1.1 Project.
The issue is that if I call a find() on the Competition model (in the following code), right after it I call a custom method in another model the operation fails with the following error:
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
My CompetitionsController::view() code is as follows:
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();
}
This is what the CompetitionsPlayer::getAddedPlayersIds()
method looks like:
public function getAddedPlayersIds($competitionId = null){
if(!isset($competitionId)) {
return false;
}
$this->displayField = 'player_id';
return $this->find('list', array('conditions' => array('competition_id' => $competitionId)));
}
I initially thought that it's somehow breaking, because of the variable name I'm assigning the return of the Model::find() operation to, which is 'competition', but what's more interesting is that if I move the Competition::find() call after the CompetitionsPlayer::getAddedPlayersIds() it works!
Furthermore if I rename the variable it will sometimes work, and sometimes not...!?
I still can't figure out which is when because I currently have no time to research this further.
Notice that according to the debug information the query that gets executed over the database is:
getAddedPlayersIds
which is the name of the function I'm calling!
As I mentioned I already know the workaround for this - just swap the two calls.But what if the first was to be before the seconds and there was no other way to implement the task at hand??
All I want now is to know why this happens?