我是yii的新人。我想从我的模型中搜索属性(字段名称),并且需要通过zii.widgets.grid.CGridView在视图页面或其他页面中查看。
这是我的模型函数,但它不起作用.. ::错误未定义的变量:pp_requisitionno
public function searchView()
{
$criteria=new CDbCriteria();
$criteria->select= 'pp_requisitionno';
$criteria->addSearchCondition('pp_requisitionno',$pp_requisitionno);
$criteria->compare('pp_requisitionno',$this->pp_requisitionno);
$criteria->condition = 'pp_requisitionno=:pp_requisitionno';
$criteria->params = array(':pp_requisitionno'=>Yii::app()->Request->Getpost('pp_requisitionno'));
$model = Requisitiondt::model()->find();
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
任何帮助请...
答案 0 :(得分:2)
定义一个可以重复用于不同搜索的常规搜索功能可能会更有用。这可以通过以下方式完成:
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search() {
$criteria = new CDbCriteria;
//Define all your searchable fields here
$criteria->compare('t.title', $this->title, true);
$criteria->compare('t.type', $this->type, true);
$criteria->compare('t.published', $this->published, true);
$criteria->compare('category.id', $this->category_id, true);
//Add any other criteria, like the default sort order etc.
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
然后在您的控制器中,您可以使用这样的搜索;
pubic function actionSearch(){
$model = new Requisitiondt;
if (isset($_POST)){
$model->attributes = $_POST;
$dataProvider = $model->search();
$this->render('searchView', array('dataProvider' => $dataProvider));
}
}
视图'searchView'看起来像这样;
<?php
$this->widget('CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
//Add in whatever columns you want the grid to show
)
));
?>
显然,您需要替换自己的模型名称和字段名称,但这是一般的想法。这种方式将搜索您在POST请求中包含的任何属性,并且更具可重用性。
答案 1 :(得分:0)
您需要在模型中使用关系,请阅读here。
然后在GridView Widget选项数组中添加'filter' => $model,