Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Selecting data from tables with the Yii framework

Friday, July 6th, 2012 by Servage

Yii is one of the best PHP frameworks out there, and it really does a lot of stuff pretty awesome. One of the things that is built right into the core functionality of Yii is CDbCriteria class, which helps you manage your queries whenever you want to select data from a database.

The criteria object is especially useful in combination with the various find methods of active records. This way you get powerful and customizable methods to get data rightout of Yii for free.

CActiveRecord::find()
CActiveRecord::findAll()
CActiveRecord::findByPk()
CActiveRecord::findAllByPk()
CActiveRecord::findByAttributes()
CActiveRecord::findAllByAttributes()
CActiveRecord::count()

Here is an example of how to use the criteria object to specifcy your query

$criteria = new CDbCriteria;
$criteria->condition='post_id=:post_id AND status=:status';
$criteria->params=array(':post_id'=>8, ':status'=>'active');
$post = Post::model()->find($criteria);

Check out the class reference for further info.

Selecting data from tables with the Yii framework, 4.0 out of 5 based on 9 ratings
Categories: Cool stuff, Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.