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);
QUERY-2
$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);
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 ?