使用CGRIDVIEW显示另一个模型的属性


Displaying attributes from another model using CGRIDVIEW

我有两个表/模型:

Tmp1

  • 收割台
  • 问题文本

Tmp2

  • 收割台
  • 零件
  • OutOf

我正在尝试显示列:标题、问题文本、部分、超出
在单个CGRIDVIEW中。

在Tmp1模型中:

public function relations()  
{  
  return array(  
               'tmp2s' => array(self::HAS_MANY, 'Tmp2', 'Header'),  
              );  
}  

在Tmp2型号中:

public function relations()
{  
    return array(  
        'header' => array(self::BELONGS_TO, 'Tmp1', 'Header'),  
    );  
}  

控制器:

 public function actionReviewAll()  
    {  
        $tmp1 = new Tmp1('search');  
                $tmp1->unsetAttributes();  
        if(isset($_GET['Tmp1'])){  
            $model->attributes=$_GET['Tmp1'];  
        }  
        $this->render('ReviewAll',array(  
                'tmp1'=>$tmp1,  
        ));  
    }  

视图:

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'tmp-grid',
    'dataProvider'=>$tmp1->search(),
    'filter'=>$tmp1,
    'columns'=>array(
        'Header',
        'QuestionText',
        array(
            'name' => 'tmp2.OutOf',
            'value' => '$data->tmp2[0].OutOf',
            'type' => 'raw'
        ),
    ),
)); ?>

错误:

未定义属性"Tmp1.tmp2"。

非常感谢您的任何帮助

您有一个sintax错误:

Tmp1.tmp2=Tmp1.mp2s

关系别名与"s"有关。"tmp2s"。

    $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'tmp-grid',
    'dataProvider'=>$tmp1->search(),
    'filter'=>$tmp1,
    'columns'=>array(
        'Header',
        'QuestionText',
        'tmp2s.Part',
        'tmp2s.OutOf',
    ),
)); ?>

但你试图显示一种"self::HAS_MANY"关系,你不能在普通的CGridView小部件上显示。。。

您的代码可能有效(我还没有尝试过),但在您的tmp1模型没有tmp2的情况下不行。在访问它之前,您必须确保有一个tmp2:

'value' => 'isset($data->tmp2) ? $data->tmp2[0].OutOf : Null',

您还可以将函数($data,$row)传递给value,使其看起来不那么混乱。(是的,这对我来说很重要。)