passing data to view

In my controller I have a database query. I get that data and pass it on to the view via the CActiveDataProvider:




	public function actionIndex()

	{

		// renders the view file 'protected/views/site/index.php'

		// using the default layout 'protected/views/layouts/main.php'

		$criteria=new CDbCriteria(array(

			'with'=>array('user', 'topic'),

			'together'=>true,

			'order'=>'topic.topicId desc',

			

		));

		

		$dataProvider=new CActiveDataProvider('Rant', array(

			'pagination'=>array(

				'pageSize'=>15,

			),

			'criteria'=>$criteria,

		));

		

		$this->render('index',array(

			'dataProvider'=>$dataProvider,

		));

	}



In my view I display the data in a CGridView widget, and that works fine. I would also like to extract the userId value from the first row of data and display it in a <h1> tag above the CGridView:





<?php echo "<h1>UserId is:</h1>".$data->userId[0];?>


<?php


$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider'=>$dataProvider,

	//'filter'=>$model,

    'columns'=>array(

        array(

			'name' => 'Date',

			'value'=> 'date("M j, Y g:ia", strtotime($data->createDate))',

		),

        'length', 

        'title',  

       

        array(            

            'name'=>'User',

			'type'=>'raw',

            'value'=>'($data->userId == 0) ? \'\' : "<a href=\"/site/index.php/user/$data->userId\">".$data->user->name."</a>"',

        ),

		array(            

            'name'=>'Listen',

			'type'=>'raw',

            'value'=>'"<a href=\"".Yii::app()->baseUrl."/".$data->mp3."\"><img src=\"".Yii::app()->baseUrl."/images/PlayButtonRed3.png\" title=\"Play Rant\"></a>"',

        ),

    ),

));

?>



This line fails to work:


<?php echo "<h1>UserId is:</h1>".$data->userId[0];?>

Do I need to pass in that piece of data separately into the view from the controller, or is there an easy way to get that out of the $dataProvider object that’s passed into the view?

Using the following worked:




$dataProvider->data[0]->rant->userId



Where “rant” = db table and “userId” is a field from the “rant” table. data[0] ensures it’s the first row of data pulled from the db.

Is this the proper way to do this, or is there a better way?

Yea, that seens to be the better way

Yaa it is the proper way , you are doing the right thing