【问题标题】:Yii CGridView, I can't display fields form other model using HAS_MANY relationshipYii CGridView,我无法使用 HAS_MANY 关系显示其他模型的字段
【发布时间】:2015-07-08 14:30:19
【问题描述】:

我有两个模型之间的 HAS_MANY 关系,我使用 bookID 作为外键

模型 1 - Importedbooks,Importedbooks 可以有多个 CountryOfImport

public function relations()
{ 
    return array(
       'CountryOfImportObj'=>array(self::HAS_MANY, 'CountryOfImport', 'bookID')
    );
}    

模型 2 CountryOfImport,CountryOfImport 属于 Importedbooks

public function relations()
{ 
    return array(
        'ImportBooksObj'=>array(self::BELONGS_TO, 'Importedbooks', 'bookID')
    );
}

现在,对于我的 CGridView,我正在使用 Importedbooks 模型的 model->search() 作为我的 dataProvider,从这里我就卡住了。

CGridView

 $data = $model->search();
 $data->setPagination(array('pageSize'=>'5'));
 $data->criteria->addCondition('active = "yes"');  
 $this->widget('zii.widgets.grid.CGridView', array(
           'dataProvider'=>$data
           ,'filter'=>$model 
           ,'pager'=>array('header'=>'')  
           ,'columns'=>array(  
                    'id',
                    'bookYear',
                    'bookTitle',
                    'DISPLAY VALUE FROM OTHER model HERE'
                    )
           )
         );

导入书籍模型的数据提供者,我将其用作网格的数据提供者

public function search()
{ 
    $criteria=new CDbCriteria;               
    $criteria->compare('id',$this->id);
    $criteria->compare('bookID',$this->bookID);
    $criteria->compare('countryName',$this->countryName,true);
    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

这种关系有效,因为我可以在我的控制器中使用下面的代码从另一个模型中获取一个字段(国家名称)

$model = Importedbooks::model()->with('CountryOfImportObj')->findbyPK(3);
print_r($model->CountryOfImportObj[0]->countryName);

【问题讨论】:

  • 请为每个模型添加字段名称,以了解您使用的是哪个模型的数据提供者。
  • 嗨,我想访问的字段是 CountryOfImport 模型中的“id”、“countryName”,我使用的 dataProvider 来自 Importedbooks 模型。我希望这个 enuf 信息。

标签: php yii has-many cgridview


【解决方案1】:

例如 Importedbooks 有许多 CountryOfImport,您必须在一行中显示所有国家名称或具体名称,具体取决于 ID 或 countryName 或其他属性。

您可以将匿名函数作为值传递:

'columns' => array(  
    'id',
    'bookYear',
    'bookTitle',
    array(
       'header' => 'Countries',
       'type' => 'raw',
        // if you have defined counrtyName attribute in  'Importedbooks' model, if not you can remove this row
        // also CGridView 'uses' attribute 'name' for filtering or sorting
       'name' => 'countryName',
       'value' => function($data, $row) { //$data is item of DataProvider, $row is number of row (starts from 0)
           $countries = CHtml::listData((array)$data->CountryOfImportObj, 'id', 'countryName');
           return implode(', ', $countries);
       }
    )
)

如果您也想按外部属性进行过滤或排序,则必须“declare them

谢谢!

【讨论】:

  • 感谢您的回答,它解决了我的问题.. 您是否知道我可以如何使用该外国属性 CountryName 也作为网格上的搜索过滤器,目前我可以使用 'id'、'bookYear'、'bookTitle' 进行搜索
  • @AfricaMatjie 如果一本书有多个国家,你想如何按国家名称过滤?
  • 感谢您的帮助。我设法做对了,我只是在我的 Importedbooks 模型 $criteria->compare('CountryOfImportObj.bookYear',$this->bookYear, true); 的 search() 函数中添加了这些变量,依此类推
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-14
相关资源
最近更新 更多