Human-Readable Values From A $Dataprovider Using A Relation

I think Yii has an automagic way to do what I’m trying to do, as long as I set up my relations correctly in my models. Here’s the story:

I have a table of Destinations where the “program_id” field holds a foreign key pointing to the “id” field of the “programs” table. The “programs” table has, in addition to its “id” field, a field “name” which holds a unique human-readable name for each Program. When I display or edit a Destination, I would like the human-readable “name” of the Program to be displayed or, in the case of a form, selectable. I’m not having any trouble getting the “id” of the program to show up, but I can’t figure out how to get the “name” field to be displayed instead of the number.

In my database (innodb) I have:


Table "programs" 

     column "id" (primary key)

     column "name" (the name of the program, e.g., "America Reads")

Table "destinations"

     column "id" (primary key)

     column "destination" (the name of the destination, e.g., Detroit)

     column "program_id" (foreign key pointing to programs.id)

In my models I have the following relations:

Destination.php:


	public function relations()

	{

		return array(

		     'program_id' => array(self::BELONGS_TO, 'Program', 'program_id'),

		);

	}

Program.php:


	public function relations()

	{

		return array(

			'destinations' => array(self::HAS_MANY, 'Destination', 'program_id'),

		);

	}

In my Controller DestinationController.php I have the following for the Index action:


	public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('Destination');

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

			'dataProvider'=>$dataProvider,

		));

  }

In my Views I call CListView like this in index.php:


    <?php

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

     	'dataProvider'=>$dataProvider,

     	'itemView'=>'_view',

     	'summaryText'=>'',

     ));

    ?>

_view.php produces output like so:


  <td><?php echo CHtml::encode($data->destination); ?></td>

  <td><?php echo CHtml::encode($data->program_id); ?></td>

How do I get the second <td> to to display the program name? When a Destination is being edited, how do I get the "value" of the select list option to be the "program_id" (that is the foreign key pointing to programs.id), but the options to display the value of programs.name? So each option in the select list would look like:


<option value="1">America Reads</option>

Thanks for any assistance.

I would rename the relation in Destination to something not equal to the field name, i.e.:

Destination.php:


	public function relations()

	{

		return array(

		     'program' => array(self::BELONGS_TO, 'Program', 'program_id'),

		);

	}

The name of the program would be seen by coding:


  <td><?php echo CHtml::encode($data->destination); ?></td>

  <td><?php echo CHtml::encode($data->program->name); ?></td>

For your options in the selection you would need the Program model. The relation would not be involved.

Perfect! Thanks so much!

Actually, the index view is showing up correctly, but I’m not sure how to proceed for getting a select list populated with the names of the programs. Could you please point me in the right direction? Sorry for a newbie question.

Never mind, there is a great comment on how to do it here:

Drop down of a foreign key in crud for a table