dtoaillwk759656786 2014-07-22 07:33
浏览 33
已采纳

yii查询构建器默认排序

I created a query:

<?php

$conditions = array();
$params = array();

$ids = explode(',', $_GET['ids']);

for($i = 0; $i < sizeof($ids); $i++)
{
    $conditions[] = 'ID=:id'.$i;
    $params[':id'.$i] = $ids[$i];
}            

if (!empty($conditions)) $conditions=implode(' OR ', $conditions);

$query = Yii::app()->db->createCommand()
               ->select()
               ->from('ABC')
               ->where($conditions, $params)
               ->limit(sizeof($ids))
               ->queryAll();

print_r($query);

problem is, by default it sorts the results by my table's primary key

my url looks like this, localhost/view?ids=6,5,1,4

and the results are sorted 1,4,5,6 i don't want that. is there a way not to sort?

  • 写回答

1条回答 默认 最新

  • dongmubi4375 2014-07-22 07:50
    关注

    you can always set order to DESC to make order descending or it will sort by defaults

    ->order('col_name desc')
    

    also you can add a where with in condition to avoid that loop for creating where part

    ->where(array('in' , 'col_name' , $id_array))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?