【问题标题】:Using Yii Model and Criteria on CGridView在 CGridView 上使用 Yii 模型和标准
【发布时间】:2012-06-07 16:35:07
【问题描述】:

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

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

【问题讨论】:

  • 没有得到你的问题。请添加更多代码以进行澄清

标签: php yii cgridview zii-widgets


【解决方案1】:

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

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

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

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

【讨论】:

  • 恐怕它不起作用....发生的事情是这些值是每个对象启动的,但是当我尝试使用小部件时没有任何显示并且分页不起作用...如果我使用 Gii Search() 方法(默认)它确实有效......我错过了一些东西!
  • 那么请说得更具体些。您期望会发生什么,而实际上发生了什么?您是否尝试在 CGridView 中显示 customermoney。他们没有出现?
  • 是的,就是这样...... CGrid 中的表格是空的,当按下分页数字时,页面只是空白.. 我想要的只是创建一个自定义函数来替换在模型中默认搜索,然后使用 CGrid 将其呈现为表格
  • 我已经编辑了我的答案,请尝试一下并查阅有关 CGridView 的 column 参数的文档:)
  • 仍然不起作用...我想我找到了解决方法,但它也不起作用,我将其作为一个新问题发布。感谢大家的帮助!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多