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',
            ),
        ),
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路