DropDown list with values from join table

Hi all,

I have 3 tables:

  • tbl_user (id)

  • tbl_company (id, name)

  • tbl_company_has_tbl_user (tbl_user_id, tbl_company_id)

In one of the views of the controller user I want to have a dropdown form list with all companies associated to that user.

The relation in the model User is the following and works:

                        'tblCompany' => array(self::MANY_MANY, 'Company', 'tbl_company_has_tbl_user(tbl_user_id, tbl_company_id)', 'index'=>'id'),

Now in the view, I know how to create a dropdown with all available names (column ‘name’ and ‘id’ in tbl_company ):

                <?php echo $form->dropDownList($model, 'tblCompany', CHtml::listData(Company::model()->findAll(), 'id', 'name'), array('prompt' => 'Select a State') ); ?>

.How can I restrict this to only the companies that are assigned to the logged in $model->id?

Thanks for your help…

The solution is

... CHtml::listData($model->company), 'id', 'name') ...

Hope this will help someone in the future.