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

  • Home
  • SEARCH
  • 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 6190169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:32:22+00:00 2026-05-24T02:32:22+00:00

For Example I have tables – Orders1, Orders2, Orders3, Users. I need to make

  • 0

For Example I have tables – Orders1, Orders2, Orders3, Users.

I need to make monthly and quartaly reports for each user. For example I am creating a function in Orders1 model quartal_sum_by_users($users, $quarter). then I make something like this:

$cost = 0;
foreach($users as $user) {
  while($month <= 3) { // first three months
    $orders = $this->Order1->find('all', array(
      'recursive' => -1,
      'fields' => array('DISTINCT id', 'COALESCE(cost+work_cost+work_installation_cost, 0) AS amount'),
      'conditions' => array(
        'MONTH(date_ordered)' => $month,
        'YEAR(date_ordered)' => $year,
        'current_status' => 3
      )
    ));

    foreach($orders as $order) {
      $cost[$month] += $order[0]['amount'];
    }

    $month++;
  } // WHILE
} // FOREACH

So, this is working but very slowly (system from I am migrating this is working on plain PHP and SQL queries and doing fine) and I have 400+ users, and more than 1000 for each Order table.

What is the best way to make it happen in CakePHP style?

  • 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-05-24T02:32:22+00:00Added an answer on May 24, 2026 at 2:32 am

    The solution should be put as answer, not as comment. Otherwise this question will be marked as unanswered forever.

    Anyway… this is what you need to do to group by:

    $this->Order1->find('all', array(
      'recursive' => -1,
      'fields' => 'YOUR_FIELDS',
      'conditions' => 'YOUR_CONDITIONS',
      'group' => 'THE_FIELD_YOU_WANT_TO_USE_AS_GROUP'
    ));
    

    The option group accepts an array so you can group by multiple fields.

    For more complex queries: book.cakephp.org/view/1030/Complex-Find-Conditions

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

Sidebar

Related Questions

For example I have 2 tables, Users and UserRelations , and it is a
Example: I have 2 tables: country and users COUNTRY: ID (integer) | name (text)
for example i have 2 tables: 1 . users : id Name 1 Mike
I need to fetch data from two tables For example: I have tables test1,
In my database I have tables that define types for example Table: Publication Types
I have two tables, for example: Table A Table B ======= ======= Name |
I have some tables that benefit from many-to-many tables. For example the team table.
Simplified for example, I have two tables, groups and items . items ( id,
I have an example database, it contains tables for Movies, People and Credits. The
If I have a collection of database tables (in an Access file, for example)

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.