Cgridview在Parentview中

时间:2014-06-02 13:02:13

标签: php gridview yii

Yii 1.1.14

我有一个员工表和一个评论表 在员工视图中,我想在网格中显示员工的所有评论(在员工字段之后) 我尝试按照示例here

我就在这里:

在模型中定义新搜索功能的新代码:

public $commentdate_param;
public $commentobservation_param;
...
public function rules()
{
    return array(
        ...
        array('public $commentdate_param, commentobservation_param, ...', 'safe', 'on'=>'search, searchIncludingComments'),
);
public function relations()
{
    return array(
        ...
        'employeecomments' => array(self::HAS_MANY, 'Employeecomments', 'employee_id'),
    );
}
public function searchIncludingComments($parentID)
    {
        $criteria=new CDbCriteria;
        $criteria->with=array('employeecomments');
        $criteria->together = true;
        $criteria->compare('t.employee_id',$parentID,false); 
        $criteria->compare('employeecomments.date', $this->commentdate_param,true);
        $criteria->compare('employeecomments.observation', $this->commentobservation_param,true);
        $sort = new CSort;
        $sort->attributes = array(
            'commentdate_param' => array(
            'asc' => 'date_desc',
            'desc' => 'date_desc DESC',
            ), '*', /* Treat all other columns normally */
        );
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            'sort'=>$sort,
        ));
    }

在控制器actionView中添加代码:

...
$child_model = new Employee("searchIncludingComments");
$child_model->unsetAttributes();
$this->render('view',array(
    'model'=>$this->loadModel($id),
    'child_model'=>$child_model,
    'parentID' => $id
));

在视图中添加代码:

...
<h3>Comments</h3>
<?php
$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'child-grid',
    'dataProvider'=>$child_model->searchIncludingComments($parentID),
    'filter'=>$child_model,
    'columns'=>array(
        'date',
        'observation',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
));
...

但我一定错过了一些东西,因为当谈到网格渲染停止时 - 但是日志中没有错误 如果有人可以帮助我会很好!

1 个答案:

答案 0 :(得分:0)

我为我找到了解决方案 - 所有我的更改和控制器都毫无价值,因为现在我在视图中执行了所有操作:

$this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'comments-grid',
        'dataProvider'=>new CActiveDataProvider('Employeecomment',array(
            'criteria'=>array(
                'condition'=>'employee_id=:eid',
                'params'=>array(':eid'=>$model->id),
            ),
            'sort'=>array(
                'defaultOrder'=>'date DESC',
            ),
        )),

当然有一些我不明白为什么它不起作用......