在CGridView上使用Yii模型和标准

时间:2012-06-07 16:35:07

标签: php yii cgridview zii-widgets

我正在尝试使用我的模型替换搜索(自定义搜索功能):

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

结果将是模型+另一个表的属性。

我正在尝试在视图中显示它们但它没有声明这样的属性....(这很清楚,因为模型没有来自另一个表的属性)

那么我如何在模型结果上使用下面的小部件?

沮丧,但指望专家:) 丹尼


编辑:  1.控制器 -

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

模型 -

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

查看

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

1 个答案:

答案 0 :(得分:2)

在模型类中创建两个属性:public $customerpublic $money

您可以拥有任意数量的自定义属性,只需与命名保持一致即可。 (如果您没有fieldname AS something模型属性,则无法使用SQL something

编辑:您还应该告诉显示CGridView columns的{{3}}

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