Yii Framework Forum: How to display count of rows which satisfies a condition in CActiveDataProvider - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to display count of rows which satisfies a condition in CActiveDataProvider Need to calculate count of unrepeated rows Rate Topic: -----

#1 User is offline   DavidHayriyan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 19-December 16

Posted 19 December 2016 - 06:19 AM

Let's assume we have model with customers and car numbers.
I have the ordinary controller:
public function actionVieworders() {
     ...
    $this->render('vieworders',array('model'=>$model));
}

Search function of model which returns CActiveDataProvider:
public function search() {
    $criteria = new CDbCriteria;
    $criteria->with[] = 'tcustomersrel';
    $criteria->with[] = 'orderedproducts';
    ...
    return new CActiveDataProvider(get_class($this), array(
        'criteria'=>$criteria,
        'sort'=>array(
        ...
        'pagination' => array(
             'pageSize' => 20)
    ));
}

And gridview widget in the view:
$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'torder-grid',
    'dataProvider'=>$model->search(),
     ...
    'filter'=>$model,
    'columns'=>$columns
));

As a result I see the correct number of rows in Summary as described in renderSummary of CBaseListView.php. In case of the following result rows count is 4:
+------------------------+
| customer 1 | car 1 |
| customer 2 | car 2 |
| customer 2 | car 1 |
| customer 1 | car 1 |
+------------------------+
In addition of this result I need to display the number of unrepeated rows by customers and car numbers. In above-mentioned case I want to get 3.
I failed trying to iterate through getData() in view, because getData() is displayed only data from current page in case of using CActiveDataProvider with pagination.
I failed trying to create criteria in model and pass count to view:
$newCriteria = clone $criteria;
$newCriteria->select='DateOfSend, IdCustomers, Time1, IdCar';
$newCriteria->group='DateOfSend, IdCustomers, Time1, IdCar';
var_dump($this->count($newCriteria));

I'm new in Yii and I didn't know the correct way to organize a solution. After some research I didn't get necessary answers.
Please could help me how to accomplish this.
0

#2 User is offline   DavidHayriyan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 19-December 16

Posted 02 January 2017 - 04:49 PM

I have solved the problem in a horrible way by changing CGridView and CActiveDataProvider in framework core. I wrote function similar to the getTotalItemCount() in framework/web/CDataProvider.php:

public function getRealItemCount($refresh=false)
{
    if($this->_realTotalItemCount===null || $refresh)
        $this->_realTotalItemCount=$this->calculateRealItemCount();
    return $this->_realTotalItemCount;
}

And in framework/web/CActiveDataProvider.php wrote equivalent of the calculateTotalItemCount():

protected function calculateRealItemCount()
{
    $baseCriteria=clone $this->getCriteria();
    $baseCriteria->select='`t`.`DateOfSend`, `t`.`IdCustomers`, `t`.`Time`, `t`.`IdCar`';
    $baseCriteria->group='`t`.`DateOfSend`, `t`.`IdCustomers`, `t`.`Time`, `t`.`IdCar`';
    $count=$this->model->findAll($baseCriteria);
    return count($count);
}

So in framework/zii/widgets/CBaseListView.php I can call $this->dataProvider->getRealItemCount() (like $this->dataProvider->getTotalItemCount()).

As I understood for the correct solution I need to extend widget and data provider.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users