Note that there are some explanatory texts on larger screens.

plurals
  1. POA CakePHP query calls COUNT() twice for no apparent reason
    text
    copied!<p><em>Note: I originally asked this question about an hour ago but only recently realized I had made a major copy and paste mistake. One so significant that it was easier to delete the old post and start over. Sorry about that.</em></p> <p>In the CakePHP framework, after I updated a Model, I dumped the SQL queries. <code>COUNT(*)</code> is called twice, for no apparent reason.</p> <p>So I have two Models, <code>$Foo</code> and <code>$Bar</code>. For simplicity sake, I have <em>not</em> defined any <code>$belongsTo</code> or <code>$hasMany</code> relationships between them. The problem only involves $Foo, but just in case I've included $Bar's code as well.</p> <pre><code>$data = array( 'something' =&gt; 12, 'something_else' =&gt; $this-&gt;Bar-&gt;field('id', $conditions), ); $this-&gt;Foo-&gt;id = $this-&gt;Foo-&gt;field($data); $this-&gt;Foo-&gt;save($data); </code></pre> <p>I am getting the <code>$Bar.id</code> field according to a set of conditions (which aren't relevant here). I use that id to determine the <code>$data</code> that should INSERT or UPDATE into <code>$Foo</code>. If <code>$Foo</code> has an id that fulfills <code>$data</code>'s requirements that value will be returned; if not <em>false</em> will be returned. According to CakePHP's architecture, <code>save()</code> will UPDATE if there's a valid id; otherwise it will INSERT. I'm pretty sure I'm not doing anything unusual.</p> <p>Here's what I see in the SQL dump:</p> <pre class="lang-sql prettyprint-override"><code>SELECT `Foo`.`id` FROM `foos` AS `Foo` WHERE `something` = 12 AND `something_else` = 1 LIMIT 1 SELECT COUNT(*) AS `count` FROM `foos` AS `Foo` WHERE `Foo`.`id` = 1 SELECT COUNT(*) AS `count` FROM `foos` AS `Foo` WHERE `Foo`.`id` = 1 UPDATE `foos` SET `something` = 12, something_else` = 1 WHERE `foos`.`id` = 1' </code></pre> <p>For the life of me I can't figure out why <code>COUNT()</code> is needed at all, much less why it's called twice. Does anyone have an idea of what's going on? Thanks for the help.</p>
 

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