0

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

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

1 回答 1

2

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

您可以拥有任意数量的自定义属性,只要与命名保持一致即可。fieldname AS something(如果没有先有somethingmodel属性就不能使用SQL )

编辑:您还应该告诉CGridView要显示哪些,像这样

$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
     ),    
   ),
));
}
于 2012-06-07T18:12:48.377 回答