Dropdown: Sub-Query for showing String instead of ID

Hi guys,

I’m quite a newbie in Yii but managed to get some stuff done reading this forum.

There’s an issue I’m dealing with right now and I’d appreciate some help.

There are three tables:




-----------   -----------   -----------

|   Box   |   | Address |   |  Street |

-----------   -----------   -----------

|  boxID  |   | addrID  |   | streetID|

|FK_addrID|   |FK_strID |   |  name   |

-----------   -----------   -----------



What I want a dropDownList() in the Managing-Pane of Box showing the ‘name’ of the ‘Street’, the ‘FK_addrID’ is in.

To get that I inserted the following in the _form.php:




<?php

$boxModel = Box::model()->findAll(array('order' => 'boxID'));

$boxList = CHTML::listData($boxModel , 'boxID', Address::getAdress('FK_addrID'));


echo $form->dropDownList($model,'attributeOfFoo', $boxList , array('empty'=>'ausw&aumlhlen'));

?>



In Address.php there is a function called as above returning the specific value as string. But all I get is a PHP Error saying he’s unable getting property of non-object.

What I know is, that he passes the String ‘FK_addrID’ to the function getAdress($id) instead of the actual value.

Can anyone give me a hint how to pass the actual value to the function or solve the problem another way?

Thanks

Does anyone know how to display the name, which is stored in a via foreign key referenced table’s column in a dropdown?

If there are two simple tables:




---------------   ------------

|     pet     |   |  animal  |

|-------------|   |----------|

|    petID    |   | animalID |

| FK_animalID |   |   name   |

---------------   ------------



How can I get a dropdown showing the pet’s name instead the FK_animalID?

I tried something like this, which unfortunately does not work (Trying to get property of non-object):




$petmodels = Pet::model()->findAll(array('order' => 'petID'));

$petlist = CHTML::listData($petmodels , 'petID', Animal::model()->findByPk('FK_animalID')->name);

Does anyone can help me with this?

Thanks

In case someone is interested. The easiest way to get this running seems to be writing an own query with the built in QueryBuilder:




$pets= Yii::app()->db->createCommand()

    ->select('pet.petID, animal.name')

    ->from('pet')

    ->join('animal', 'animal.animalID = pet.FK_animalID')

    ->queryAll();



Now you can create a listData item:


$petlist = CHtml::listData($pets, 'petID', 'name');

And echo the dropDownList:


echo $form->dropDownList($model, 'FK_animalID', $petlist);