这是我的控制器:
public function actionIndex() {
$model = new Article('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['Article']))
$model->attributes = $_GET['Article'];
$this->render('index', array(
'model' => $model,
));
}
这是我的观点:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'article-grid',
'dataProvider' => $model->search(),
'columns' => array(
'id',
'alias',
'title',
'date',
'created',
'user.name',
array(
'class' => 'CButtonColumn',
'htmlOptions' => array('style' => "text-align: right;"),
'headerHtmlOptions' => '',
'template' => '{ask} {del}',
'buttons' => array(
'ask' => array(
'label' => 'Edit',
'url' => 'Yii::app()->createUrl("/mgmt/blog/update/", array("id"=>$data->id))',
'options' => array()
),
'del' => array(
'label' => 'Delete',
'url' => 'Yii::app()->createUrl("/mgmt/blog/delete/", array("id"=>$data->id))',
'options' => array("class" => "delete", 'onclick' => 'openDialog($(this))', "data-width" => 400, 'data-height' => 200, "title" => "Delete article")
),
),
),
),
));
,这是我的模特内容的一部分:
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'user' => array(self::BELONGS_TO, 'User', 'createdby'),
'slideshowImages' => array(self::HAS_ONE, 'SlideshowImage', 'article_id'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels() {
return array(
'id' => 'ID',
'alias' => 'Alias',
'title' => 'Title',
'content' => 'Content',
'date' => 'Date',
'created' => 'Created',
'createdby' => 'Createdby',
'user.name'=> "Created By"
);
}
/**
* 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() {
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id);
$criteria->compare('alias', $this->alias, true);
$criteria->compare('title', $this->title, true);
$criteria->compare('content', $this->content, true);
$criteria->compare('date', $this->date, true);
$criteria->compare('created', $this->created, true);
$criteria->compare('createdby', $this->createdby);
$criteria->order = 'id desc';
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
此代码的结果是包含db数据的表。我的问题是什么我不明白搜索功能的数据是如何传递给视图的?如果我在 $ criteria 上的var_dump,或 $ this 或在 $ model 的视图中,所有这些都是空的,这两者都不是从db返回数据。为什么?有人可以解释我的周期吗?我不会在这里问,但Yii论坛上的文档很少。谢谢你的帮助
答案 0 :(得分:0)
视图中有$model
。
$model
有一个方法search()
。 search()
仅返回数据提供者。小组件使用数据提供程序从数据库查询数据。