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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:11:20+00:00 2026-06-15T04:11:20+00:00

Although I’m aware of the group , contain , fields and order options on

  • 0

Although I’m aware of the group, contain, fields and order options on a find(), I just cannot seem to make the following query with CakePHP 2.2.3 :

SELECT `User`.*, SUM(`Influence`.`id`) AS matches
FROM `db`.`users` AS `User` 
    LEFT JOIN `db`.`influences_users` AS `InfluencesUser` ON (`User`.`id` = `InfluencesUser`.`user_id`) 
    LEFT JOIN `db`.`influences` AS `Influence` ON (`InfluencesUser`.`influence_id` = `Influence`.`id`)
WHERE 1 = 1
AND `Influence`.`id` IN (1, 2, 3, 4)
GROUP BY `User`.`id` 
ORDER BY COUNT(`Influence`.`id`) DESC

Basically, I’m trying to retrieve an array of Users having the Influences 1, 2, 3 and 4, with the COUNT() function on the Influences tables, then order them by COUNT() DESC.

Is there a clean way (I’d rather not want to use any hacks or functions like Model::query()) to do so in CakePHP?

EDIT: It wasn’t actually a SUM() but rather a COUNT() although I doesn’t change anything to my problem.

  • 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-15T04:11:21+00:00Added an answer on June 15, 2026 at 4:11 am

    I managed to find a solution, not too hacky and unless someone has a better solution (not a lot when you see the number of answers I got), here it is:

    $this->User->virtualFields['matches'] = 'COUNT(InfluencesUser.influence_id)';
    $matches = $this->User->find(
            'all',
            array(
                'joins' => array(
                    array(
                        'alias' => 'InfluencesUser',
                        'table' => 'influences_users',
                        'type' => 'LEFT',
                        'conditions' => array(
                            'InfluencesUser.influence_id' => $userInfluences, //$userInfluences contains the list of influences I want to do the restriction on
                            '`InfluencesUser`.`user_id` = `User`.`id`'
                        )
                    )
                ),
                'group' => 'User.id',
                'order' => array('User__matches' => 'DESC'),
            ));
    

    I manually join the InfluencesUser table (I don’t actually need the Influence one, only the list of influence_id) to make a big table. I just had to do the restriction (influence_id) on the join table, then I can do my goup by, the count() and append the count() field to my results array, thanks to mark’s answer.

    Obviously I would have prefered using something like the contain parameter, but it doesn’t seem to allow me to do what I can achieve with the join in this particular situation.

    Hope this could save some time to some people.

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

Sidebar

Related Questions

Although I can group and order by on an aliased sub query, I can't
Although this is django related, it's really just a general, programming efficiency question. I
Although the question seems simple, I couldn't find the answer to it. I have
Although there seem to be very few up to date references for OpenGL 3.x
Although the actual schema is a bit more complex, the following should be a
Although I couldn't find a reference to this easily in google, I'm familiar with
Although returning a string is cake, I just can't figure out how to return
Although ServerSocket class is designed to generically listen for incoming connections, Can I just
although this C++ compiler error has been reported many times, I did not find
Although my question might seem abstract I hope it's not. Suppose I develop an

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.