Why these AR statements doesn't work?


$criteria=new CDbCriteria;

$cirteria->limit=5;

$criteria->offset=6;

$cirteria->condition='id=:book_id';

$criteria->params=array(':book_id'=>10000);

$comments = BookComment::model()->findAll($criteria);

The error log said it Failed to execute SQL statement, and The SQL statement executed was: SELECT * FROM tbl_book_comment t OFFSET 6.

If findAll method doesn’t support these conditions how can i select several rows with a limit and an offset?

You’re wrong with syntaxis of condition and params. I think you mean:


$criteria->condition='id = :book_id';

Sorry, but that was just my mistake when i post it, the original code is just like you said…I tried again and still getting the same problem…

It’s funny that the error complains exactly of the fields you’ve misspelled, don’t you think ?

The produced query is: SELECT * FROM tbl_book_comment t OFFSET 6 ;

Whereas if we take a look at what you wrote and remove the misspelled data we get:




$criteria=new CDbCriteria;

$criteria->offset=6;

$comments = BookComment::model()->findAll($criteria);



As you see, it’s too much to be just a simple coincidence.

Try this code just as i write it:




$c=new CDbCriteria;

$c->condition='id=:book_id';

$c->limit=5;

$c->offset=6;

$c->params[':book_id']=10000;

$comments = BookComment::model()->findAll($c);



If that doesn’t work, though there is no reason not to, try this:




$comments = BookComment::model()->findAllBySql('SELECT * FROM {{tbl_book_comment}} WHERE book_id=:book_id LIMIT 6,5',array(':book_id'=>10000));



I bet there are 2 variables, i.e. $criteria and $cirteria. :)

ha ha, it’s unbelievable this is the reason.

Really sorry my misspelling wasted your time, thanks to everyone.

And I do think Yii should build a stronger error log system.

Just set error reporting level to E_ALL in your development environment, and errors like this will be logged properly.

Yii has an awesome error logging system, but if you turn off error reporting from PHP it is useless, that’ why it’s a good idea to code with error_reporting(E_ALL);