【问题标题】:How to get data from different table in CGridView?如何从 CGridView 中的不同表中获取数据?
【发布时间】:2014-07-30 11:49:49
【问题描述】:

我有 3 张桌子。 汽车类型:

id | main_image | title

car_type_classifiers:

id | car_type_id | classifier_id

分类器:

id | class

我想显示一个 CGridView 所以有列:标题 |班级。但是一个 car_type 可以有很多类。我尝试在网上搜索,但无法理解模型搜索功能中的那些$criteria->compare() 功能。

我希望这些显示为小列表。我怎样才能做到这一点? 我的看法:

    $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'car-type-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
        'columns' => array(
                'title'
    ),
));

我的控制器:

public function actionIndex()
{
    $model=new EeCarTypes('search');
    $model->unsetAttributes();

    $this->render('index',array('model'=>$model));
}

还有我的模特:

public function search()
{
    // @todo Please modify the following code to remove attributes that should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
    $criteria->compare('car_type',$this->car_type,true);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

【问题讨论】:

    标签: php yii cgridview


    【解决方案1】:

    我假设你的模型有很好的关系。

    在你看来:

    $this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'car-type-grid',
        'dataProvider'=>$model->search(),
        'filter'=>$model,
        'columns' => array(
            'title',
            array(            
                'name'=>'Class',
                'type' => 'raw',
                //call the method 'gridDataColumn' from the controller
                'value'=>array($this,'gridDataColumn'), 
            ), 
        ),
    ));
    

    在您的控制器中:

    //called on rendering the column for each row 
    protected function gridDataColumn($data,$row)
    {
        $cellValue = "<ul>";
        foreach($data->classifiers as $classifier) {
            $cellValue .= "<li>" . $classifier->class . '</li>';
        }
         $cellValue .= "</ul>";
        return $cellValue;    
    }  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 2015-03-11
      • 2017-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多