0

ここで回答を検索しましたが、私の問題に固有の回答は見つかりませんでした。

モデル間の関係を正しく設定していないと思います... モデルを使用する CGridView があります。これらのフィールドの 1 つが外部 ID です。その外部 ID を使用して、モデル内の別のフィールドを取得したいと考えています。(たとえば、質問モデルには外部キー「tag1」が含まれています。「tag1」を使用して、テーブルTagの「name」フィールドを見つけたいです)。

意見

    <?php
$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$questions->search(),
    'filter' => $questions,
    'columns' => array(
        array('class'=>'CCheckBoxColumn'),
        'text',
        'tag1',
        array('header' => 'Tag 1', 'value' => '$questions->tag1->text'),
        'na',
        'cca',
    ),
));
?>

モデル

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(
        'tag1' => array(self::HAS_ONE, 'Tags', 'id'),
    );
}
4

2 に答える 2

0

この関係を与えてみてください

 public function relations() {
        return array(
            'tags1'=>array(self::BELONGS_TO, 'Tags', 'id'),    
        );
    }
于 2013-07-30T02:05:03.517 に答える
0

変数が間違っているだけです。各行はデータプロバイダーを使用して変数に格納される$dataため、次のようにする必要があります。

<?php
$this->widget('zii.widgets.grid.CGridView', array(
    'dataProvider'=>$questions->search(),
    'filter' => $questions,
    'columns' => array(
        array('class'=>'CCheckBoxColumn'),
        'text',
        'tag1',
        array('header' => 'Tag 1', 'value' => '$data->tag1->text'),
        'tag1.text', //if you relation is setup correct you can also do this
        'na',
        'cca',
    ),
));
?>
于 2013-07-30T16:17:54.010 に答える