0

如何使用 CDbCriteria 和 CActiveDataProvider 执行此查询?

'SELECT * FROM tbl_post where title LIKE %'.$title.'% ORDER BY title LIKE '.$title.' DESC , title LIKE '.$title.'% DESC'

更新: 最后我写了这个:

$criteria = new CDbCriteria;
                    $criteria->addCondition('title LIKE :title');                        
                    $criteria->params = array(':title'=>'%'.$title.'%',':t1'=>$title,':t2'=>$title.'%');;
                    $criteria->order='title LIKE :t1 DESC , title LIKE :t2 DESC';

但我得到了错误:

CDbCommand failed to execute the SQL statement: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens. The SQL statement executed was: SELECT COUNT(*) FROM `tbl_post` `t` WHERE title LIKE :title
4

2 回答 2

0

This can be achieved like this:

    $dataProvider=new CActiveDataProvider('tbl_post', array(
    'criteria'=>array(
    'condition'=>'title LIKE % '.$title.' %',
    'order'=>'title DESC',
    ),
   ));

    $dataProvider->getData();

also refer : Help

Thanks.

于 2012-12-07T09:01:27.100 回答
0

尝试使用 compare 子句,不确定您是否想要 'order by x like' :

<?php
$criteria = new CDbCriteria;
$criteria->compare('title', $title, true);
$criteria->order = 'title DESC';
$dp = new CActiveDataprovider('posts', array(
  'criteria' => $criteria
));
于 2012-12-06T21:16:25.917 回答