Drop Down List Problems

Hey all,

I am pretty new to Yii and php in general, as in just saw it for the first time on Monday. I’ve been working on this site and trying to populate a drop down list from database values. For some reason instead of displaying the actual string values, the drop down list has a list of 17 strings that say “Object id #120” to “Object id #136”. There are supposed to be 17 records from the data base, so it seems like they are getting that part right, but I don’t know why it won’t display the actual string values.

Any help will be greatly appreciated! Thanks.

the controller code for this section is:

public function actionIndustries()

    {


            if(!isset($_GET['id'])) throw new CHttpException(404,'The requested page does not exist.');


            $model=Company::model()->with('industries')->findbyPk($_GET['id']);


            if(isset($_POST['industries']))


            {


                    Company_Industries::model()->deleteAll('company_id=:company',array(':company'=>$model->id));





                    foreach($_POST['industries'] as $industry):


                            $compind=new Company_Industries;


                            $compind->company_id = $model->id;


                            $compind->industry_id = $industry;


                            $compind->save();


                    endforeach;





                    $this->redirect(array('show','id'=>$model->id));


            }


            $industrylist=Industry::model()->findAll(array('order'=>'industry ASC'));


            $this->render('industries',array('model'=>$model,'industrylist'=>$industrylist));


    }

the view code is:

<div id="adminnav">

    &lt;?php &#036;this-&gt;widget('application.components.MainMenu',array(


            'items'=&gt;array(


            array('label'=&gt;'Browse', 'url'=&gt;array('/contact/list')),


            array('label'=&gt;'|','url'=&gt;array('')),


            array('label'=&gt;'Industry List', 'url'=&gt;array('/industry/list')),


            ),


    )); ?&gt;

</div>

<div id="blackline"></div>

<h2>Browse</h2>

<?php $this->widget(‘CLinkPager’,array(‘pages’=>$pages)); ?>

<div id="filter">

    Filter by Industry:





    &lt;?php echo CHtml::dropDownList('industry','industry', &#036;industries); ?&gt;





    &lt;br /&gt;

</div>

and the model is:

class Industry extends CActiveRecord

{

    /**


     * The followings are the available columns in table 'Industry':


     * @var integer &#036;id


     * @var string &#036;name


     */





    /**


     * Returns the static model of the specified AR class.


     * @return CActiveRecord the static model class


     */


    public static function model(&#036;className=__CLASS__)


    {


            return parent::model(&#036;className);


    }





    /**


     * @return string the associated database table name


     */


    public function tableName()


    {


            return 'Industry';


    }





    /**


     * @return array validation rules for model attributes.


     */


    public function rules()


    {


            return array(


                    array('name','length','max'=&gt;50),


                    array('name', 'required'),


            );


    }





    /**


     * @return array relational rules.


     */


    public function relations()


    {


            // NOTE: you may need to adjust the relation name and the related


            // class name for the relations automatically generated below.


            return array(


                    'companies'=&gt;array(self::MANY_MANY, 'Company', 'Company_Industries(industry_id, company_id)'),


            );


    }





    /**


     * @return array customized attribute labels (name=&gt;label)


     */


    public function attributeLabels()


    {


            return array(


                    'id' =&gt; 'Industry',


                    'name' =&gt; 'Industry Name',


            );


    }

}

Ok, for this type of drop down list, it is expecting a key/value array, not an array of objects, which is what you get from the Industry::model()->findAll(). What you need to do is this:




$industrylist = array();

foreach(Industry::model()->findAll() as $industry){

  $industrylist[$industry->industry_id] = $industry->industry;

}



Then pass this list on the way you are doing an everything should turn out right.

Jaz Manister,

Thanks a lot! That makes a lot of sense, I just didn’t even know to think about that.

I did have to change

[$industry->industry_id] = $industry->industry

to

[$industry->id] = $industry->name

and then it worked perfectly. Thanks again!