Search code examples
phpyiiyii-cmodel

In Yii, why use CActiveDataProvider instead of Post::model()->findAll()?


I see CActiveDataProvider used in the controller actionIndex() function.

Is there any difference between using this instead of simply Post::model()->findAll()?


Solution

  • CActiveDataProvider is a wrapper for CActiveRecord with extended capabilities of sorting, pagination and filtering. Typically CActiveDataProvider is being used in conjunction with zii widgets like CListView or CGridView which are utilizing its sort and search capabilities. So if you want to display data in terms of list or table with (optional) sorting/filtering/pagination - most convenient way of doing it in Yii is using described zii widgets or their extensions.

    If you just need to fetch data and use it in any other way, but without smart rendering mechanisms provided by CListView or CGridView - use CActiveRecord, because it doesn't perform anything else except of fetching of data.

    reference