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???