Note that there are some explanatory texts on larger screens.

plurals
  1. POModel (reference) behaving strange in Controller
    primarykey
    data
    text
    <p>I'm having a strange issue with a CakePHP 2.1.1 Project.<br> 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:</p> <pre><code>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 </code></pre> <p>My CompetitionsController::view() code is as follows:</p> <pre><code>public function view($id = null) { $this-&gt;layout = 'competition'; $this-&gt;Competition-&gt;id = $id; if (!$this-&gt;Competition-&gt;exists()) { throw new NotFoundException(__('Invalid competition')); } $active = $this-&gt;Competition-&gt;field('active', array('id' =&gt; $id)); if (!$active) { $this-&gt;redirect(array('controller' =&gt; 'pages', 'action' =&gt; 'display', 'competition_inactive')); } //THIS IS WHERE IT BECOMES STRANGE: $competition = $this-&gt;Competition-&gt;find('first', array('conditions' =&gt; array('Competition.id' =&gt; $id))); $addedPlayersIds = $this-&gt;CompetitionsPlayer-&gt;getAddedPlayersIds($id); //SOME CODE INTENTIONALLY REMOVED HERE!!! $this-&gt;set('playerShops', $playerShops); $this-&gt;set('messages', $messages); $this-&gt;set('competition', $this-&gt;Competition-&gt;read(null, $id)); //render() IS CALLED FOR A SPECIFIC REASON $this-&gt;render(); } </code></pre> <p>This is what the <code>CompetitionsPlayer::getAddedPlayersIds()</code> method looks like:</p> <pre><code>public function getAddedPlayersIds($competitionId = null){ if(!isset($competitionId)) { return false; } $this-&gt;displayField = 'player_id'; return $this-&gt;find('list', array('conditions' =&gt; array('competition_id' =&gt; $competitionId))); } </code></pre> <p>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!<br> Furthermore if I rename the variable it will sometimes work, and sometimes not...!?<br> 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:</p> <pre><code>getAddedPlayersIds </code></pre> <p>which is the name of the function I'm calling!</p> <p>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??<br> All I want now is to know why this happens?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload