0
votes

I'm trying to create small application on Yii framework but can't understand one moment. I have Order model that extends from CActiveRecord and in index action I trying to use Order model twice but having some troubles.

The reason is to show paginated records in table and in last row I wish to show average totals for some fields, and this totals must be calculated for all records filtered by sort/filter criteria

Here is code of action:

public function actionIndex()
{
    $model = new Order('search');
    $model->unsetAttributes();
    if (isset($_GET['Order']))
        $model->attributes = $_GET['Order'];

    // Get average totals by current filters
    $totals = array();
    $tm = clone $model;
    $tmdp = $tm->search();
    $tmdp->criteria->select = array('avg(price) as avgPrice', 'avg(cost) as avgCost');
    $data = $tmdp->getData(true);
    $totals['cost'] = number_format($data[0]->avgCost, 0, '.', ' ');
    $totals['price'] = number_format($data[0]->avgPrice, 0, '.', ' ');
    $totals['marja'] = Order::getMarjaVal(round($data[0]->avgCost, 0), round($data[0]->avgPrice, 0));

    $this->render('index', array(
        'model'         => $model,
        'totals'        => $totals
    ));
}

In my view I'm getting data in the same way $data = $model->search()->getData();

So if I view first page - all working good, but if I change page then $data = $tmdp->getData(true); getting empty array. I also tried to set page for $tmdp like:

    $tmdp->criteria->select = array('avg(price) as avgPrice', 'avg(cost) as avgCost');
    $tmdp->pagination->setCurrentPage(0);
    $data = $tmdp->getData(true);

but after this I getting data in my view only from first page.

Please can someone tell me how to do this correctly???

1
You will need to debug the SQL queries that are executed in order to detect if the search() generates a query that returns no results. - Pentium10

1 Answers

0
votes

When we change current page in pagination Yii automatically updates $_GET['Order_page'] value (if this value wasn't setted - it will create it), and pagination automatically gets this value when we getting data from data provider. So for this code i made such changes:

public function actionIndex()
{
    $model = new Order('search');
    $model->unsetAttributes();
    if (isset($_GET['Order']))
        $model->attributes = $_GET['Order'];

    // Get average totals by current filters
    $totals = array();

    $tm = clone $model;
    $tdp = $tm->search();
    $tdp->criteria->select = array('avg(price) as avgPrice', 'avg(cost) as avgCost');
    if (isset($_GET['Order_page'])) {
        $curPage = $_GET['Order_page'] - 1;
    } else {
        $curPage = 0;
    }
    $tdp->pagination->currentPage = 0;
    $data = $tdp->getData(true);
    $totals['cost'] = number_format($data[0]->avgCost, 0, '.', ' ');
    $totals['price'] = number_format($data[0]->avgPrice, 0, '.', ' ');
    $totals['marja'] = Order::getMarjaVal(round($data[0]->avgCost, 0), round($data[0]->avgPrice, 0));
    $tdp->pagination->currentPage = $curPage;

    $this->render('index', array(
        'model'         => $model,
        'totals'        => $totals
    ));
}

So we trying to get page from $_GET global array and after owr operation we just update this value back