Search code examples
phpyiicriteria

How to pass Criteria to an Array in Yii


I have the following queries..

$Provider1 = new CSqlDataProvider($Query1, array('pagination'=>false));
$Result1 = $bProvider->getData();

$Provider2 = new CSqlDataProvider($Query2, array('pagination'=>false));
$Result2 = $bProvider->getData();

Then im merging both array results

$Result = array_merge($Result1, $Result2);

As this array $Result contains multiple records like 100+

Here is what i have tried for pagination

// Counting array result for pagination
    $item_count = count($timelineResult);
    $page_size = 10;
    $pages = new CPagination($item_count);
    $pages->setPageSize($page_size);

    $end = ($pages->offset + $pages->limit <= $item_count ? $pages->offset + $pages->limit : $item_count);

    $criteria = new CDbCriteria();
    $criteria->limit = $end;
    $criteria->offset = $pages->offset;

Now the problem is how do i apply this $criteria to my $Result


Solution

  • I recommend you should use CArrayDataProvider. You already have a result in an array called $Result and remove the CDbCriteria from your pagination code, so it will be like this.

    $item_count = count($Result);
    $page_size = 10;
    $pages = new CPagination($item_count);
    $pages->setPageSize($page_size);
    

    The $page_size = 10 and we have to use the same page size 10 for CArrayDataProvider, Here is how you will use CArrayDataProvider:

    $dataProvider=new CArrayDataProvider($Result, array(
            'pagination'=>array(
                'pageSize'=>$page_size,
            ),
        ));
    

    and then get the result in an array like this..

    $MyResult = $dataProvider->getData();
    

    I hope it will work for you.