您好我正在使用CakePHP版本 - 2.5.5。
我有一个表名chat_ategory_mages
我希望通过降序获得频率顺序的平均数。了解频率请查看 - 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
时,结果不是降序。
Mysql查询
QUERY-1:
选择ChatCategoryImage
。id
,ChatCategoryImage
。chat_category_id
,ChatCategoryImage
。hits
,ChatCategoryImage
。{{1} },命中/(DATEDIFF(NOW(),已创建))AS频率,来自created
。myshowcam
AS chat_category_images
WHERE ChatCategoryImage
。ChatCategoryImage
= 5订购BY chat_category_id
DESC LIMIT 10
QUERY-2:
选择Frequency
。ChatCategoryImage
,id
。ChatCategoryImage
,chat_category_id
。ChatCategoryImage
,hits
。{{1} },命中/(DATEDIFF(NOW(),创建))AS频率,FROM ChatCategoryImage
。created
AS myshowcam
WHERE chat_category_images
。ChatCategoryImage
= 5 LIMIT 10
问题是什么以及为什么它不会在第二个查询中出现ChatCategoryImage
?
由于 茅
答案 0 :(得分:1)
您可以使用virtualFields
$this->ChatCategoryImage->virtualFields = array('Frequency' => 'hits/(DATEDIFF(NOW(),created))');
改变订单方式
$order = array('Frequency' => 'desc');
答案 1 :(得分:0)
这件事发生在我身上。您必须在paginate函数中添加第三个参数$ whitelist。例如。
$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);
我不知道为什么会这样。我试图在paginate函数中看到代码,但我无法弄明白。
答案 2 :(得分:-1)
你的代码错了
$this->paginate = array(
'conditions' => array('ChatCategoryImage.chat_category_id'=>$cetegory_id),
'limit' => 10, 'order' => 'Frequency' => 'DESC');
$getAllCourses = $this->paginate('ChatCategoryImage');