Using Yii Model and Criteria on CGridView

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

$this->widget(‘zii.widgets.grid.CGridView’, array(

‘id’=>‘food-grid’,

‘dataProvider’=>$model->combineFoodInputByDate($dates[‘startDate’],$dates[‘endDate’]),

‘filter’=>$model,

)); }

frustrates, but counting on the experts :) Danny

Hi there. Not an expert here :), but try with

[list=1]

[*]In controller:


public function actionSomething()

{

    $model=new YourModel('combineCampInputByDate');

    …

}

[*]In model:


public $customer;

public $money;

public $startDate;

public $endDate;

…

public function rules()

{

    return array(

        array('…, customer, money, startDate, endDate', 'safe', 'on'=>'combineCampInputByDate'),

    );

}

…

public function combineCampInputByDate() {

    …

    if(($this->startDate != '') && ($this->endDate != '')) {

        $criteria->condition = "date BETWEEN '{$this->startDate}' AND '{$this->endDate}'";

    }

    …

}

[*]In view:


$this->widget('zii.widgets.grid.CGridView', array(

    'id'=>'food-grid',

    'dataProvider'=>$model->combineCampInputByDate(),

    'filter'=>$model,

));

[/list]

I’ll try it right now…post soon with results!!

thanks a million :)

No Good…For some reason it searches for the public properties i added:

include(customer.php) [<a href=‘function.include’>function.include</a>]: failed to open stream: No such file or directory

C:\wamp\bin\Yii\framework\YiiBase.php(418)

      }

415 }

416 }

417 else

418 include($className.’.php’);

419 }

420 else // class name with namespace in PHP 5.3

421 {

422 $namespace=str

Any suggestions?

Thanks again!

Hi again. I think you should post your running codes, including the controller and the search part of the view.

No problem here it is: (anything that would solve this… :):slight_smile:

  1. Controller -

public function actionIndex()

{


if (isset(&#036;_POST['Filter']) &amp;&amp; &#33;empty(&#036;_POST['Filter']['date']) ) {


    &#036;GetDates = new GetDates();


    &#036;this-&gt;dates = &#036;GetDates-&gt;getDatesFromPostWidget(&#036;_POST);


    &#036;model = new Campaigns;


}


else 


    &#036;model=NULL;


        &#036;this-&gt;render('index',array(


	'model' =&gt; &#036;model, 'dates' =&gt; &#036;this-&gt;dates,


	));	


}
  1. Model -

public function combineCampInputByDate($startDate,$endDate) {

&#036;criteria=new CDbCriteria;


&#036;criteria-&gt;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,

));

}

  1. View -

<?php

&#036;this-&gt;widget('zii.widgets.grid.CGridView', array(


'id'=&gt;'bo-campaigns-grid',


'dataProvider'=&gt;&#036;model-&gt;combineCampInputByDate(&#036;dates['startDate'],&#036;dates['endDate']),


'filter'=&gt;&#036;model,  





	),


));

}

?>

That’s it…I think :)

Well where have you applied the changes I gave above on model, controller, and view?

Also, I should you’d better use the standard generated codes by Gii. It gives you a beautiful admin action rendering a view that includes a CGridView, and uses the model’s search method.

Hi,

This is before your changes… just wanted to illustrate what i’m going for.

And this is the standard Gii generated Code…all i did was replace the search function with my own in the model