Click here for EduSec Demo EduSec Screenshots

Dataprovider in Yii

On 2013-06-10 - By Ravi Thanki

There are 3 Types of Dataprovider in Yii.

1. CActiveDataProvider: To asociate data with a model.

2. CArrayDataProvider: To asociate data with any Array object.

3. CSqlDataProvider: To associate data with a custom SQL query.

Example:

We want to retrieve all users whose status field is equal to 1.

1) With CActiveDataProvider:

public function getActiveUsers()
{
$criteria=new CDbCriteria;
$criteria->condition = “status=1″;
return new CActiveDataProvider($this, array(
‘criteria’=>$criteria,
));
}

2) With CArrayDataProvider:

$usersArray = User::model()->findAllByAttributes(array(“status”=>1));

new CArrayDataProvider($usersArray, array(‘keyField’=>’userId’));

3) With CSqlDataProvider:

$sql=’SELECT * FROM user where status=1′;
$dataProvider=new CSqlDataProvider($sql);