Php YII模型:::CGridView使用条件按特定属性搜索
我是新来的。我想从我的模型中按属性(字段名)搜索,并需要在视图页面或zii.widgets.grid.CGridView的其他页面中查看Php YII模型:::CGridView使用条件按特定属性搜索,php,yii-extensions,yii,Php,Yii Extensions,Yii,我是新来的。我想从我的模型中按属性(字段名)搜索,并需要在视图页面或zii.widgets.grid.CGridView的其他页面中查看 如何通过findByAttribute()在model中创建search()函数 这样我们就可以按属性显示结果,而无需任何搜索 这是我的模型函数,但它不起作用错误未定义变量:pp_requisionno public function searchView() { $criteria=new CDbCriteria(); $crite
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,
));
}
请提供任何帮助…您需要在模型中使用关系,请阅读
然后在GridView小部件选项数组中添加
'filter'=>$model,
,定义可用于不同搜索的通用搜索函数可能会更有用。这可以通过以下方式完成:
/**
* 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));
}
}
<?php
$this->widget('CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
//Add in whatever columns you want the grid to show
)
));
?>
“searchView”视图看起来是这样的
pubic function actionSearch(){
$model = new Requisitiondt;
if (isset($_POST)){
$model->attributes = $_POST;
$dataProvider = $model->search();
$this->render('searchView', array('dataProvider' => $dataProvider));
}
}
<?php
$this->widget('CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
//Add in whatever columns you want the grid to show
)
));
?>
显然,您需要替换自己的模型名和字段名,但这是一般的想法。这种方法将搜索POST请求中包含的任何属性,并且更易于重用。如何根据您的答案显示特定属性“类型”的结果??请帮助我,这是一个从我正在从事的现有项目复制的通用示例。您需要替换您的WIN文件名,例如,在我放置t.type、t.PUBLISTED和t.title的地方,您需要输入自己的字段名,例如t.pp_requisionNo。@SelimReza编辑了我的答案以添加视图文件。您可以像使用普通CGridView一样使用它,我将在其中使用控制器??请确定是否有控制器来渲染视图文件?把它放在那里@SelimReza