Using Yii Model and Criteria on CGridView

6.7k views Asked by At

I'm trying to use my model with a replacement for the search (custom search function):

public function combineCampInputByDate($startDate,$endDate) {

$criteria=new CDbCriteria;
$criteria->select   = 'food.*,SUM(customer) AS customer, SUM(money) AS money';
$criteria->join     = 'JOIN foodType food ON foodtype = food.foodtype ';     
$criteria->condition = "date BETWEEN '$startDate' AND '$endDate'";
$criteria->group        = 'foodtype ';
return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
    ));
}

the result will be the attributes for the model + the other table.

i'm trying to display them in the view but it states no such attribute as .... (this is clear since the model doesnt have an attribiute that came from the other table)

So how do i use the widget below on the model result??

frustrates, but counting on the experts :) Danny


Edit: 1. Controller -

public function actionIndex()
{
if (isset($_POST['Filter']) && !empty($_POST['Filter']['date']) ) {
$GetDates = new GetDates();
$this->dates = $GetDates->getDatesFromPostWidget($_POST);
$model = new Campaigns;
}
else 
$model=NULL;
$this->render('index',array(
'model' => $model, 'dates' => $this->dates,
)); 
}

Model -

public function combineCampInputByDate($startDate,$endDate) {

$criteria=new CDbCriteria;
$criteria->select = 'food.*,SUM(customer) AS customer, SUM(money) AS money';
$criteria->join = 'JOIN foodType food ON foodtype = food.foodtype '; 
$criteria->condition = "date BETWEEN '$startDate' AND '$endDate'";
$criteria->group = 'foodtype ';
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}

View

$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'bo-campaigns-grid',
'dataProvider'=>$model->combineCampInputByDate($dates['startDate'],$dates['endDate']),
'filter'=>$model,  
),
));
}
1

There are 1 answers

5
adamors On BEST ANSWER

In your model class create two attributes: public $customer and public $money.

You can have as many custom attributes as you want, just be consistent with the naming. (You can't use the SQL fieldname AS something if you don't have a something model attribute first)

EDIT: You should also tell the CGridView which columns to display, like this

$this->widget('zii.widgets.grid.CGridView', array(
     'id'=>'bo-campaigns-grid',
     'dataProvider'=>$model->combineCampInputByDate($dates['startDate'],$dates['endDate']),
     'filter'=>$model,
     'columns'=>array(
        'customer',
        'money',
       //etc. for more detailed customization, check the links above
     ),    
   ),
));
}