Difference Between Activerecord And Model()

1.

$data=Vehicles::model()->findAll();

$this->render(‘index’,array(‘data’=>$data));

2.

$data=new CActiveDataProvider(‘vehicles’);

$this->render(‘index’,array(‘data’=>$data));

both are same? or these have any difference? & in which situation i have to use these? please help.

Hello ferozfirru, welcome to Yii Forum!

The findAll() is a method from the CActiveRecord.findAll() class that returns an array with all active records satisfying an specified condition or an empty array if none is found.

Normally you use this method when you want to show a list of records to the user, but since the method returns an array, in your view you need way to iterate over the value from the array, so you can simple use in your view:




...

foreach ($data as $row)

{

      echo $row->fieldName;

}

...



The CActiveDataProvider implements a data provider based on ActiveRecord and returns an ActiveRecord object. Normally you use this class when you want to use the data into a widgets (example: check the Post::search() method and the admin view that is using the widget CGridView from the Blog example/tutorial). But can you also use the CActiveDataProvider to show a list of record based on an specified condition? Yes You Can!! The CActiveDataProvider has some method that you can use, ie ‘getData()’, ‘getItemCount()’, etc…! How to use? Simple:




...

$tmp = $data->getData(); // return an array

foreach ($tmp as $t)

{

    echo $t->fieldName;

}

$total = $data->getItemCount();

echo $total;       

...