doudiecai1572 2014-07-24 13:57
浏览 34
已采纳

为什么Yii QueryBuilder会忽略offset()?

My query result with offset = 0 and limit = 10 is exactly one row.

When I use offset = 1 there should be no result.

But with a query like

$query = Yii::app()->db->createCommand();
$criteria = new CDbCriteria();
$criteria->addCondition('f.deleted = 0 AND b.version = f.version');
$criteria->params = [];

if ($idUserAnswerer)
{
    $query->leftJoin('bar AS b', 'b.idF = f.id');
    $criteria->addCondition('f.idUserOwner = :idUserOwner');
    $criteria->params['idUserOwner'] = $idUserOwner;
}

$query->select('*')
    ->from('foo AS f')
    ->where($criteria->condition)
    ->bindValues($criteria->params);

$query->order('FIELD(t.type,
    ' . X::X_TYPE_ID_A . ', ' . X::X_TYPE_ID_B . ',
    ' . X::X_TYPE_ID_C . '), b.created_time, b.votes DESC');

$data = $query->limit(10, 2)->queryAll();

(Hint: I have to use $criteria, because I have to use inCondition at some point)

I still get this one row. But normally (when I do this query directly in my db) I should not get a result.

Any ideas why Yii spits out this one row?

  • 写回答

2条回答 默认 最新

  • dongliao9233 2014-07-25 11:37
    关注

    This is a problem with the query builder. You need a specific order, binding values should be done at the end of the query.

    This is because Yii does a prepare statement when binding values. It then caches this statement. Then you append your order and limit to it. When you do the query, the previous cached statement is used, without the order and limit. I think this is by design.

    So always bind the values last:

    $query = Yii::app()->db->createCommand();
    $data = $query->select('*')
        ->from('tbl AS t')
        ->where('idCategory=:catid')
        ->order('id DESC')
        ->limit(2, 2)
        ->bindValues(array(':catid' => 151))
        ->queryAll();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果