PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 21, 2022

[FIXED] CakePHP 3: Multiple Order results in pagination?

 February 21, 2022     cakephp, cakephp-3.0, mysql, orm     No comments   

Issue

Need to order (sort) content in two way.

1: field "featured" can be 1 or 0 (default), need to show first content with featured "1" value,

->order(['featured DESC']);

2: field "featured_start" can be Y-m-d DATE or NULL (default), need to show first content with date, than with NULL value.

Like this

-| featured | featured_start |             created |-
-----------------------------------------------------
-|        1 |     2015-12-24 | 2015-12-24 10:06:07 |-
-|        1 |     2015-12-23 | 2015-12-22 09:01:29 |-
-|        1 |           NULL | 2015-12-24 09:01:29 |-
-|        0 |           NULL | 2015-12-24 10:01:29 |-
-|        0 |           NULL | 2015-12-20 20:20:45 |-
-----------------------------------------------------

Thanks.


Solution

SOLVED

->order([
    'featured DESC',
    '(CASE WHEN featured_start IS NULL THEN 1 ELSE 0 END),
    'featured_start DESC',
    'created DESC
 ]);

This returns a complex sorted data. Latest featured users are always at the top, followed by other users.

When the user subscription expires, the application via cron sets the value 0 in the "featured" field, and NULL in the "featured_start" field.



Answered By - Salines
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing