Yii Framework Forum: Using Yii Model and Criteria on CGridView - Yii Framework Forum

Jump to content

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

Using Yii Model and Criteria on CGridView How to display query results not from the model attributes Rate Topic: -----

#1 User is offline   Danny V 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 05-June 12

Posted 07 June 2012 - 11:36 AM

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
0

#2 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,165
  • Joined: 05-January 12
  • Location:Morocco

Posted 07 June 2012 - 12:06 PM

Hi there. Not an expert here :), but try with
  • 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,
    ));

This post has been edited by bennouna: 07 June 2012 - 12:11 PM

0

#3 User is offline   Danny V 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 05-June 12

Posted 07 June 2012 - 02:56 PM

View Postbennouna, on 07 June 2012 - 12:06 PM, said:

Hi there. Not an expert here :), but try with
  • 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,
    ));



I'll try it right now...post soon with results!!
thanks a million :)
0

#4 User is offline   Danny V 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 05-June 12

Posted 07 June 2012 - 03:09 PM

View Postbennouna, on 07 June 2012 - 12:06 PM, said:

Hi there. Not an expert here :), but try with
  • 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,
    ));



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!
0

#5 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,165
  • Joined: 05-January 12
  • Location:Morocco

Posted 07 June 2012 - 03:51 PM

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

#6 User is offline   Danny V 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 05-June 12

Posted 07 June 2012 - 03:59 PM

View Postbennouna, on 07 June 2012 - 03:51 PM, said:

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... :):)
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,
));
}

2. 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,
));
}

3. View -
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'bo-campaigns-grid',
'dataProvider'=>$model->combineCampInputByDate($dates['startDate'],$dates['endDate']),
'filter'=>$model,

),
));
}
?>
That's it...I think :)
0

#7 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,165
  • Joined: 05-January 12
  • Location:Morocco

Posted 07 June 2012 - 04:05 PM

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.
0

#8 User is offline   Danny V 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 05-June 12

Posted 07 June 2012 - 04:08 PM

View Postbennouna, on 07 June 2012 - 04:05 PM, said:

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