Dropdownlist Depending On Many_Many Relation

hi yii people. i’m fairly new to yii and i’m having this problem.

Table aliados has columns ‘id’, ‘name’ and a bunch of other stuff.

Table centro_medico has columns ‘id’ and ‘name’.

Table aliados_centro_medico has columns aliados_id and centro_medico_id.

In a different view (solicitudes/_form) i have an autocomplete for ‘aliados.name’, when you choose the right one, it has to take the ‘id’ that ‘aliados’, check table ‘aliados_centro_medico’ with that ‘aliados_id’, to see which 'centro_medico_id’s are related to it… then, with the 'centro_medico_id’s that i got from that search, i have to search table ‘centro_medico’ for the names of those ‘centro_medico’, and put all those names in a DropDownList.

I can do the search, although i’m not sure it’s right… however i don’t know how to put the results in a DropDownList, or get it from the function into the view.

Some guidance would be appreciated.

This is my search, and it’s in CentroMedico model




public function getCentroAliado(){

          if(Yii::app()->user->getState('perfil')==4){

          	$aliados_id=Yii::app()->user->getState('aliados_id');

          }

          else

          {

          	$aliados_id=$_POST['Solicitudes']['aliados_id'];

          }

            	$criteria = new CDbCriteria;

            	$criteria->select='nombre';

            	$criteria->alias='cm';

            	$criteria->join="INNER JOIN aliados_centro_medico acm on (cm.id=acm.centro_medico_id) and acm.aliados_id=:aliados_id";

            	$criteria->params=array(':aliados_id'=>$aliados_id);

            	$result=CentroMedico::model()->findAll($criteria);

       }



If the relation is a many-many you should use a checkboxlist, in order to check more than one.

If you always have at most one check, use a one-many relationship

They’re always gonna be able to choose just one…But because of the requirements, one “Aliado” can always be related to many “Centro Medico” and viceversa. That can’t change.

But whether I use a dropdownlist, a radiobuttonlist or a checkboxlist, my problem is actually building the object using the function and then rendering it in the _form.