doutou1922 2014-11-07 17:53
浏览 28
已采纳

迭代CActiveDataProvider以从不同模型获取值

I'm currently learning Yii. I'm trying to iterate over CActiveDataProvider results and get the value of one index from every result and use that to pull data from another model. I'm not sure exactly if I'm going about it correctly.

public function actionIndex()
{
    // This next line works and return data, but I want it to index with each record in $dataProvder
    // $name = Artist::model()->findByPk(1);
    $name = Artist::model();
    $dataProvider=new CActiveDataProvider('Album');
    foreach($dataProvider->getData() as $record){
        $name->findByPk($record->artist_Id); 
        $this->name[] = $name->firstName . " " . $name->lastName;
    }
    $this->render('index',array(
        'dataProvider'=>$dataProvider,
        'name' =>$this->name,
    ));
}

What is causing my results to be blank when I use dataProvider for index?

  • 写回答

1条回答 默认 最新

  • doufu9947 2014-11-08 18:22
    关注

    You should be using Active Record relations for a situation like this. Data providers can take into account relations and join data automagically. This is a vital concept to learn in order to effectively use Active Record in Yii, so I suggest reading and practicing examples. Here's a link to the official guide:

    http://www.yiiframework.com/doc/guide/1.1/en/database.arr

    For your specific case, the relations would look something like this:

    Album Class relations function:

    class Album extends CActiveRecord{
        //beginning of model
        public function relations(){
            return array(
                'artist' => array(self::BELONGS_TO, 'Artist', 'artist_Id'),
            );
        }
        //rest of model
    }
    

    Artist Class relations function:

    class Artist extends CActiveRecord{
        //beginning of model
        public function relations(){
            return array(
                'albums' => array(self::HAS_MANY, 'Album', 'artist_Id'),
            );
        }
        //rest of model
    }
    

    Controller Action:

    public function actionIndex()
    {
        $names = array();
        $dataProvider=new CActiveDataProvider('Album', array(
            'criteria' => array(
                'with' => 'artist',
            )
        ));
        foreach($dataProvider->getData() as $record){
            $names[] = $record->artist->firstName . " " . $record->artist->lastName;
        }
        $this->render('index',array(
            'dataProvider'=>$dataProvider,
            'name' => $names,
        ));
    }
    

    By passing a criteria to the Albums data provider telling it to join "with" the artists, all artists and albums will be fetched in 1 SQL query. You can access the Artist information from the data provider, for example, in a Grid View in your view file:

    $this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider'=>$dataProvider,
        'columns'=>array(
            'name',                  // album name
            'artist.firstName',      // display the first name of the artist
            array(                   // display the full name of the artist
                'name'=>'fullName',
                'value'=>'$data->artist->firstName." ".$data->artist->lastName',
            ),
        ),
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵