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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:07:12+00:00 2026-05-24T03:07:12+00:00

CAKEPHP Group by problem in paginate.. This is my table structure Friends Table `id`

  • 0

CAKEPHP Group by problem in paginate..

This is my table structure

 Friends Table
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user1_id` int(11) NOT NULL DEFAULT '0',--> UserFrom
  `user2_id` int(11) NOT NULL DEFAULT '0',---> UserTo

I need to get all the unique records of one user and his user1_id = 100

There are lot of duplicate values in user2_id. I need to get the unique values
While i trying this code it returns only first 12 values(according to limit).
If i commented the group by line then all records are displaying (including duplicate values)

$this->paginate = array(
                    'conditions' => $conditions,
        'contain'=>array(
            'UserFrom'=>array(
                'fields'=>array(
                   'UserFrom.id',
                   'UserFrom.first_name',
                   'UserFrom.last_name',
                ),
             ),
             'UserTo'=>array(
                'fields'=>array(
                    'UserTo.id',
                    'UserTo.first_name',
                    'UserTo.last_name',
                    )
             )
            ),'limit' => 12,
            'order' => array('Friend.id' => 'desc'),
            'recursive'=>0,
            'fields'=>array('DISTINCT Friend.user2_id','Friend.*'),
            'group'=>array('UserTo.id'),
            );

This is my sql query on that page

SELECT COUNT(*) AS count FROM friends AS Friend LEFT JOIN users AS UserTo ON (Friend.user2_id = UserTo.id) LEFT JOIN users AS UserFrom ON (Friend.user1_id = UserFrom.id) WHERE UserFrom.nick_name = ‘shyam’ AND Friend.status = ‘friend’ GROUP BY UserTo.id

SELECT DISTINCT Friend.user2_id, Friend.*, UserTo.id, UserTo.first_name, UserTo.last_name, UserTo.nick_name, UserTo.name, UserTo.icon_medium, UserTo.icon_big, UserFrom.id, UserFrom.first_name, UserFrom.last_name, UserFrom.nick_name, UserFrom.name FROM friends AS Friend LEFT JOIN users AS UserTo ON (Friend.user2_id = UserTo.id) LEFT JOIN users AS UserFrom ON (Friend.user1_id = UserFrom.id) WHERE UserFrom.nick_name = ‘shyam’ AND Friend.status = ‘friend’ GROUP BY UserTo.id ORDER BY Friend.id desc LIMIT 12

1.First count query returning count properly
2.Second query if i put limit it not working properly. i.e there are 144 records in my table i need to display 12 per page.. only first page coming if i use group by and limit

  • 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-24T03:07:13+00:00Added an answer on May 24, 2026 at 3:07 am

    You should have either DISTINCT or GROUP BY, not both – they’re duplicating each other and probably causing problems with your query. Remove one of them and retry.

    Note: If you want to manually request pages other than 1st with $this->paginate(), add 'page' parameter with page number to your $this->paginate array, like this:

    $this->paginate = array_merge($this->paginate, array('page' => $pageNumber);
    

    However, normally you wouldn’t want to do this, as paginator does this automatically (passing page variable via URL parameter to your action) as long as you use pagination controls in your view (see http://book.cakephp.org/view/1233/Pagination-in-Views).

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

Sidebar

Related Questions

In cakephp, I used $rNo array in WHERE clause. $rooms =$this->find('all',array( 'conditions'=>array(NOT=>array('Room.id'=>$rNo)), 'group'=>array('Room.room_type_id'))); Now,
So, I'm not quite sure how I should structure this in CakePHP to work
Asking this question here because in CakePHP google group got no answer.I have the
I am using CakePHP framework with MySQL database and I have problem in saving
I am stuck on pagination in CakePHP 1.3. I am trying to paginate feePayment
I'm building a controller to manage group based ACL in CakePHP and when I
Using CakePHP 1.2, I am trying produce a GROUP BY query: SELECT `categories`.*, COUNT(`entities`.id)
Greetings, I'm trying to use the 'group' parameter for a find using CakePHP. The
CakePHP is driving me nuts! Here is my code: if($this->Page->save($datavalue)) { $this->Session->setFlash('Page content updated
I want to have cakephp paginate data and still use containable , but for

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.