Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8116009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:40:26+00:00 2026-06-06T03:40:26+00:00

Note: I originally asked this question about an hour ago but only recently realized

  • 0

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.

In the CakePHP framework, after I updated a Model, I dumped the SQL queries. COUNT(*) is called twice, for no apparent reason.

So I have two Models, $Foo and $Bar. For simplicity sake, I have not defined any $belongsTo or $hasMany relationships between them. The problem only involves $Foo, but just in case I’ve included $Bar’s code as well.

$data = array(
    'something' => 12,
    'something_else' => $this->Bar->field('id', $conditions),
);

$this->Foo->id = $this->Foo->field($data);
$this->Foo->save($data);

I am getting the $Bar.id field according to a set of conditions (which aren’t relevant here). I use that id to determine the $data that should INSERT or UPDATE into $Foo. If $Foo has an id that fulfills $data‘s requirements that value will be returned; if not false will be returned. According to CakePHP’s architecture, save() will UPDATE if there’s a valid id; otherwise it will INSERT. I’m pretty sure I’m not doing anything unusual.

Here’s what I see in the SQL dump:

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'

For the life of me I can’t figure out why COUNT() 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:40:29+00:00Added an answer on June 6, 2026 at 3:40 am

    I haven’t worked with CakePHP, but looking at the source code here, you’ll see in the field function that it calls the find method.

    Therefore, this call

    $data = array(
        'something' => 12,
        'something_else' => $this->Bar->field('id', $conditions),
    );
    

    Generates:

    SELECT `Foo`.`id` FROM `foos` AS `Foo` WHERE `something` = 12 AND `something_else` = 1     LIMIT 1
    

    Then the set field call

    $this->Foo->id = $this->Foo->field($data);
    

    generates this through the inner find function call:

    SELECT COUNT(*) AS `count` FROM `foos` AS `Foo` WHERE `Foo`.`id` = 1
    

    The Save function also calls find to see if the record exists before saving, which generates the second call:

    SELECT COUNT(*) AS `count` FROM `foos` AS `Foo` WHERE `Foo`.`id` = 1
    

    The final query is the obvious save itself.

    Please look for yourself to verify I’m reading it correctly, but that’s what it appears to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note This originally started as a question about 404 errors, but now it's a
Note: This question originally applied to Xapian , but due to cross-platform issues and
Note: Originally this question was asked for PostgreSQL, however, the answer applies to almost
NOTE : This question was originally asked way back in 2012. Before the decltype
Note this question was originally posted in 2009, before C++11 was ratified and before
(Note: This is not a question about what is the best way with code
Note: I had another similar question about how to GZIP data using Ruby's zlib
I need to get execution time in milliseconds. Note : I originally asked this
UPDATE (5 July 2013): I've learned much since I originally asked this question. In
I recently asked a question about structs, and optimizing some overloaded operators. The original

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.