0

Currently I have a table called as 'Users'. With columns like

Id, name, image, is_active, etc, etc, created, modified

I am setting up a pagination, where it will display all Users one by one, on click of next button(in the view)

here not all Users will have images. So my pagination order will be: to show users who have images first. Followed by Users who don't have images.

So far I have this

    $this->paginate = array(
      'limit' => 1,
      'conditions' => array(
        'User.is_active' => 0
        ),
      'order' => array(
        'User.image' => 'DESC'
        ),
      'group' => array(
        'User.id'
        )    
      ); 

What I want is now, the view to FIRST display a random User, who has image, every time the page refreshes OR on the first load of the page. Followed by other Users who have images ,and then followed by Users who don't have images.

this query works fine in phpmyadmin sql panel. But I am not sure how do I write a cakephp paginate query for it

select * User order by image<>' ' desc,rand()

If you think I am missing any more data, please ask. I'll write it in comments

sandeep
  • 109
  • 1
  • 12

2 Answers2

0

you can look at this answer SQL how to make null values come last when sorting ascending and try to change it with CakePHP conditions.

Community
  • 1
  • 1
Miheretab Alemu
  • 956
  • 2
  • 20
  • 43
0

In your model you can set. I believe this works for the pagination order as well.

    public $order = array(
        'ISNULL(Users.image) ASC',
        'RAND() * User.id'
    );

But this makes all the users randomized. I don't know how you may do it to show only in the first record a random user with the order field. I would suggest using a custom function in which you may choose a random row and put it in the top of the array.

gmponos
  • 2,157
  • 4
  • 22
  • 33