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 9126211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:59:12+00:00 2026-06-17T06:59:12+00:00

I like how CakePHP automatically loops through the results of MySQL queries and formats

  • 0

I like how CakePHP automatically loops through the results of MySQL queries and formats them in a nice map for you.

Here’s a sample query that I’m using:

# Inside some model
return $this->query("

  SELECT 
    Profile.id,
    SUM( IF( HOUR(Log.event_one) > 3, 1, 0 ) ) as EventOne

  FROM profiles Profile
  JOIN logs Log ON Log.id = Profile.log_id

  WHERE Profile.id = {$pUserId}
");

CakePHP would return a map like the following as the result:

array
  0
    array
      'Profile'
          array
            'id' => 23
      '0'
          array
            'EventOne' => 108
  1
    array
      'Profile'
          array
            'id' => 23
      '0'
          array
            'EventOne' => 42
  2
    ...

What I’m trying to do is have the result be something like this:

array
  'Profile'
      array
        'id' => 23

  'Events'
  #   ^   I want to be able to specify this key
      array
        'EventOne' => 108

Any ideas?

  • 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-17T06:59:13+00:00Added an answer on June 17, 2026 at 6:59 am

    You can’t do that directly

    The top level array keys are derived from the table name which mysql says the field relates to – in your case it’s a calculated field and therefore (according to mysql) belongs to no table – hence the 0 array key.

    Post processing

    What you can do however, is post process the result so that it is the format you want:

    public function getStuff() {
        // The query call in the question can very easily be a normal find call
        $return = $this->query("don't use query unless you have no choice");
    
        foreach($return as &$row) {
            $row['Events'] = $row[0];
            unset($row[0]);
        }
    
        return $return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've heard of frameworks like Ruby and CakePHP. Should I proceed to learn them,
i am trying to create a Radio button using Cakephp like the one the
I have CakePHP app in which I'd like to attach gallery to multiple resources.
I have a project in CakePHP and I would like to know how to
i am using cakephp 1.3 ...it was working like charm in server ,i copied
I'm using the new CakePHP 2.1 and would like to use the JsonView to
I'm working with cakephp 2.0 and would like to use different css for each
I would like to learn how setup prefix routing like there is in cakephp
I have problem with cakephp's Session->write method. If I set a value like $_SESSION['..']
I'm extremely new to CakePHP. From what I've gathered, it seems like I can

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.