Cactivedataprovider With Custom Query

Hello!

I’m trying to use CActiveDataProvider to display the overall score (based on the average of three categories of the table “rating”) in my CListView.

The following code shows no errors, and also can not access the score in CListView.

The command print_r($data), prints no information on the rating section: Array ( [rating] => Array ( ) )

Please help me!

Thanks!

ModelController.php




public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('Model', array(

			'criteria'=>array(

				'order'=>'t.id DESC',

				'with'=>array('rating'=>array('select'=>array('AVG(cat1+cat2+cat3) AS score'))),

			)));


		$this->render('index',array(

			'dataProvider'=>$dataProvider

		));

	}



index.php




<?php $this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$dataProvider,

	'itemView'=>'_view'

)); ?>



I used below code in my project.

Add this code in components




<?php

class MyActiveDataProvider extends CActiveDataProvider {


    protected function calculateTotalItemCount() {

        $baseCriteria=$this->model->getDbCriteria(false);

        if ($baseCriteria!==null)

                $baseCriteria=clone $baseCriteria;

        //You can get real records count only in this way (when you use JOIN and GROUP BY)

        $count=count($this->model->findAll($this->getCriteria()));

        $this->model->setDbCriteria($baseCriteria);

        return $count;

    }

}

?>



model search function




<?php

    public function searchbygroup()

	{

		$criteria=new CDbCriteria;       

		//custom query

        $criteria->select=array('*,concat(documentid,"_",userid) as groupid,count(documentid) as downloadcount');

		//Add Different Condition

		$criteria->compare('userid',$this->userid,true);

        $criteria->addCondition("documentid IN ('$this->documentid')");

		$criteria->addBetweenCondition('createdon',$this->datefrom,$this->dateto);

        // group by , if need

        $criteria->group='groupid';

		//return new CActiveDataProvider($this, array(			'criteria'=>$criteria,		));

        return new MyActiveDataProvider($this,array('criteria'=>$criteria));

	}

?>




Hey mbala! Thanks for your time.

I’m a newbie, my apologies, but where I have to change your code so I can calculate the score based on the average of three columns from another table?

Thanks!

Try this




public function actionIndex()

{

	$criteria=new CDbCriteria;       

	$criteria->select=array('*,AVG(cat1+cat2+cat3) AS score');

        $criteria->order='id desc';

		

	$dataProvider=new MyActiveDataProvider($this,array('criteria'=>$criteria));

	$this->render('index',array(

	'dataProvider'=>$dataProvider

	));

}



add your model name instead of $this




        $dataProvider=new MyActiveDataProvider('modelname',array('criteria'=>$criteria));

        $this->render('index',array(

        'dataProvider'=>$dataProvider

        ));

Hello,
Sorry to say this,
But it could not resolve my issue related to speedup.