问题描述
我使用的是 CakePHP 版本 - 2.5.5.
Hi i am using CakePHP version - 2.5.5.
我有一个表名 chat_ategory_mages
我想通过降序获得平均频率顺序数.了解频率请检查 - 如何在 MySQL 中获取当前日期到发布日期之间的平均点击量?
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 但当我添加 Frequency
时,结果不是降序.
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 查询
查询-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 LIMIT10
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
查询-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
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
?
What is the problem and why its not coming ORDER BY Frequency
in the second query?
谢谢中文
推荐答案
你可以使用 virtualFields
You can use virtualFields
$this->ChatCategoryImage->virtualFields = array('Frequency' => 'hits/(DATEDIFF(NOW(),created))');
改变订购方式
$order = array('Frequency' => 'desc');
这篇关于CakePHP 命令不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!