CakePHP order not working

Hi, I am using CakePHP version 2.5.5.

I have a table name. chat_ategory_magesI want to get the average number of orders of frequency in descending order. Know about the frequency, please check - How to get the average number of views of the current date in MySQL?

chat_ategory_mages

id        chat_category_id    hits       created
------------------------------------------------
1         5                  10       2014-11-07 11:07:57
2         5                  8        2014-11-10 05:10:20
3         5                  70       2014-10-04 08:04:22

the code

$order=array('Frequency' => 'DESC');
$fields=array(
    'ChatCategoryImage.id',
    'ChatCategoryImage.chat_category_id',
    'ChatCategoryImage.created',
    'ChatCategoryImage.hits',
    'hits/(DATEDIFF(NOW(),created)) AS Frequency',
);

QUERY-1

$rndQry=$this->ChatCategoryImage->find('all',array('conditions'=>array('ChatCategoryImage.chat_category_id'=>$cetegory_id), 'fields'=>$fields, 'order'=>$order, 'limit'=>10));
pr($rndQry); //WORKING FINE

QUERY-2

//THIS IS NOT WORKING
$this->Paginator->settings = array(
        'conditions'=>array('ChatCategoryImage.chat_category_id'=>$cetegory_id),
        'fields'=>$fields,
        'limit' => 10,
        'order' => $order,
);
$getCategoryImages = $this->Paginator->paginate('ChatCategoryImage');
pr($getCategoryImages); //NOT WORKING

Above the table, if I write a simple cakephp query, it orderworks fine, but when I use pagination cakephp it doesn't work. If I use $order=array('hits' => 'DESC');, this will be its ideal. Showing the result of 70.10.8 sequentially, but when I add Frequency, the result will not arrive in descending order.

Mysql query

QUERY-1:

SELECT ChatCategoryImage. id, ChatCategoryImage. chat_category_id, ChatCategoryImage. hits, ChatCategoryImage. created, hits/(DATEDIFF (NOW(), created)) AS Frequency, FROM myshowcam. chat_category_images AS ChatCategoryImage WHERE ChatCategoryImage. chat_category_id= 5 ORDER BY Frequency DESC LIMIT 10

QUERY-2:

SELECT ChatCategoryImage. id, ChatCategoryImage. chat_category_id, ChatCategoryImage. hits, ChatCategoryImage. created, hits/(DATEDIFF (NOW(), created)) AS Frequency, FROM myshowcam. chat_category_images AS ChatCategoryImage WHERE ChatCategoryImage. chat_category_id= 5 LIMIT 10

ORDER BY Frequency ?

+4
3

virtualFields

$this->ChatCategoryImage->virtualFields = array('Frequency' => 'hits/(DATEDIFF(NOW(),created))');

$order = array('Frequency' => 'desc');
+1

    $this->paginate = array(
                   'conditions' => array('ChatCategoryImage.chat_category_id'=>$cetegory_id), 
                   'limit' => 10, 'order' => 'Frequency' => 'DESC');
    $getAllCourses = $this->paginate('ChatCategoryImage');
0

It happened to me. You must add the third parameter $ whitelist to the paginate function. For instance.

$this->Paginator->settings = array(
        'conditions'=>array('ChatCategoryImage.chat_category_id'=>$cetegory_id),
        'fields'=>$fields,
        'limit' => 10,
        'order' => $order,
);
$scope =  array();
$whitelist = array('ChatCategoryImage.id', ...); //The fields you want to allow ordering.

$getCategoryImages = $this->Paginator->paginate('ChatCategoryImage', $scope, $whitelist);
pr($getCategoryImages);

I do not know why this is happening. I tried to see the code inside the paginate function, but I could not understand.

0
source

All Articles