Populating activeListBox with data from related table

I have two tables that are related by MANY_MANY (so 3 tables really), services and timetables.

I am trying to display an activeListBox in services' _form.php view where it shows the selected timetables.

I have the following:

services model (relations):

_form.php

When I access _form.php via /create it correctly generates the following code:

I have added an afterSave function to my Services model which correctly inserts the appropriate values into the database.

The problem occurs when I try to then update the service (via _form.php) where data exists in the database for that service. I get the following error:

Quote

Object of class Timetables could not be converted to int

From what I can tell, it is trying to compare the Timetables object to the value (int) that exists in the database. I can't work out how to get it to compare timetables->id to the value.

results in

Quote

Property "Services.timetables.id" is not defined.

Where am I going wrong?

Working based on information in issue 217 I have extended CHtml to let me work with objects.

See attached file.

Place in /protected/components and call via OCHtml::activeListBox($model,$attribute,CHtml::listData(Model::model()->findAll(),$valueField,$textField),array('object'=>$related_attribute));

So for me:

OCHtml::activeListBox($services,'timetables',CHtml::listData(Timetables::model()->findAll(),'id','name'),array('object'=>'id'));

I've the same problem and 'ive tried your solution with good result.

Is that the only solution? Haven't you found a solution without modifying the yii code?

Excellent solution scythah.

Works great.

Thanks

Anis

I have the same problem but I cannot find the attachment any more. Can someone post it again, please?

You can get it from the issue item.

Thanks a lot. I see, this was a newbie question. I will look in the issue tracker first from now on.

For my case it works perfectly without modifying CHtml:


		<?php

		echo CHtml::activeListBox(

			$model,'pupils',CHtml::listData($users, 'id', 'username'),

		 		array('multiple' => 'multiple'));

		?>



$users here is an array of objects. My mistake was not to register it as parameter by the right render()-method.

Thanks

Mantus