Yii Framework Forum: Populating activeListBox with data from related table - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Populating activeListBox with data from related table Rate Topic: -----

#1 User is offline   scythah 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 20-April 09

Posted 29 April 2009 - 01:32 AM

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?
0

#2 User is offline   scythah 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 20-April 09

Posted 30 April 2009 - 12:48 AM

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'));

Attached File(s)


0

#3 User is offline   Nardin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 18-May 09

Posted 18 May 2009 - 10:10 AM

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?
0

#4 User is offline   itlinkonline 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 28-May 09

Posted 28 May 2009 - 07:20 AM

Excellent solution scythah.

Works great.

Thanks
Anis
0

#5 User is offline   Mantus 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 29-March 10
  • Location:Ludwigsburg, Germany

Posted 29 March 2010 - 10:56 AM

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

#6 User is offline   scythah 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 44
  • Joined: 20-April 09

Posted 29 March 2010 - 11:05 AM

You can get it from the issue item.
0

#7 User is offline   Mantus 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 29-March 10
  • Location:Ludwigsburg, Germany

Posted 31 March 2010 - 03:10 AM

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users