Yii 1.1: auto-count-sql-data-provider

AutoCountSqlDataProvider has the ability to use pagination without first counting all the rows, thus increasing performance.
6 followers

AutoCountSqlDataProvider extends the CSqlDataProvider. It adds the ability to use pagination without first counting all the rows, thus increasing the performance when browsing large tables.

Requirements

None

Usage

# table_name has 15 rows.
 
$provider = new AutoCountSqlDataProvider(
    'table_name',
    new CDbCriteria(array('condition'=>'column1 is not null')),
    false,
    array(
        'sort'=>array(
            'attributes'=>array('column1','column2'),
        ),
        'pagination'=>array(
            'pageSize'=>10,
        ),
    )
);
 
$rows = $provider->fetchData();
# count($rows) == 10
# $provider->hasMore == true
# $provider->getTotalItemCount() == 10
$rows = $provider->fetchData();
# count($rows) == 5
# $provider->hasMore == false
# $provider->getTotalItemCount() == 15

Resources

Total 2 comments

#8244 report it
prekageo at 2012/05/21 11:19am
Where is the SQLDataProvider feature?

The SQL statement is created by calling CDbCommandBuilder.createFindCommand and using the $table and $criteria parameters inside the constructor. The reason for this approach is that is easy and portable to generate also the count SQL query by calling CDbCommandBuilder.createCountCommand and using the same parameters. If you find that this approach is restrictive you might consider altering the behavior as needed. Can you also send me a patch of your changes and I will happily integrate them here.

#8233 report it
Junior - df9 at 2012/05/20 02:30pm
Where is the SQLDataProvider feature?

I'm sorry if I'm wrong, but as docs say:

"CSqlDataProvider implements a data provider based on a plain SQL statement."

The first parameter of this extension asks for a "table" name not SQL statement.

:(

Leave a comment

Please to leave your comment.

Create extension