问题描述
您好,我使用CakePHP版本 - 2.5.5。
Hi i am using CakePHP version - 2.5.5.
我有一个表名 chat_ategory_mages 以按降序获取频率顺序的平均数。了解频率请检查 -
I have a table name chat_ategory_mages I want to get Average number of Frequency Order by Descending. Know about the Frequency please check - How to get Average hits between current date to posted 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
代码
$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
上面的表如果我写简单的cakephp查询 order 工作正常,但当我使用cakephp分页它不工作。如果我使用 $ order = array('hits'=>'DESC'); 显示结果70,10,8一致,但当我添加频率时,结果不会按降序排列。
Above table if i write simple cakephp query the order is working fine but when i am using cakephp pagination it is not working. If i am using $order=array('hits' => 'DESC'); this its woring perfect. Showing result 70,10,8 consistently but when i am adding Frequency it the result not coming the descending order.
Mysql Query
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 code> = 5 ORDER BY 频率 DESC LIMIT 10
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 。。。 , ChatCategoryImage 。 created ,hits /(DATEDIFF(NOW(),created))AS Frequency,FROM myshowcam 。 chat_category_images AS ChatCategoryImage WHERE ChatCategoryImage chat_category_id = 5 LIMIT 10
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
问题是什么, code> ORDER BY Frequency 在第二个查询中
What is the problem and why its not coming ORDER BY Frequency in the second query?
感谢
chinu
Thankschinu
推荐答案
您可以使用virtualFields
You can use virtualFields
$this->ChatCategoryImage->virtualFields = array('Frequency' => 'hits/(DATEDIFF(NOW(),created))');
更改订单方式
$order = array('Frequency' => 'desc');
这篇关于CakePHP顺序不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!