display external relation field with CListView

Hi all.

I have an artist-song (1:N) relation, and I’d like to show the name of the artist in the song list view.

I understood I have to use a code like this in the controller:




	public function actionIndex()

	{

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

			'pagination'=>array(

				'pageSize'=>self::PAGE_SIZE,

			),

			'criteria'=>array(

        		'with'=>array('artists'),

    		),


		));


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

			'dataProvider'=>$dataProvider,

		));

	}



but then? I don’t know what I have to write in the index.php (the list) file.

I used this code




<?php $this->widget('zii.widgets.CDetailView', array(

	'data'=>$model,

	'attributes'=>array(

		'id',

		'title',

		'path',

		'md5',

		array(

			'label'=>'Artista',

			'name'=>'artists.name',

		),

	),

)); ?>



in the view.php, and it displays correctly the artist name, but I can’t do the same with the List view.

I found something similar with CGridView, but it uses the “column” attribute, that I can’t use in CListView…

Can anyone give me a hint?

thanks :)

In the list view you can use




echo $data->artists->name



at least for me it works.

Unfortunately I have another problem, as soon as I add an order to the CActiveDataProvider criteria everything breaks for me and I get an exception.

For example if in your case I’d add an order line like this:




    public function actionIndex()

        {

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

                        'pagination'=>array(

                                'pageSize'=>self::PAGE_SIZE,

                        ),

                        'criteria'=>array(

                        'with'=>array('artists'),

                        'order'=>'title' // <---- This is the added order line

                ),


                ));


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

                        'dataProvider'=>$dataProvider,

                ));

        }



the result I’d get is:


CDbException

Description

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 207 General SQL Server error: Check messages from the SQL Server [207] (severity 16) [(null)]


I would appreciate if you could try it and report back if it worked.