Dropdown Optgroup Relation

Hi guys

Just wondering if anyone has any ideas about this.

I have a dropdown menu filled with data from the database.

It contains a list of course application statuses which I would like to group using optGroup.

For example, the applicant status can have multiple application status e.g.,

Admitted => ‘Application Approved’, ‘Payment Received’, ‘Confirmation Sent’

Rejected => ‘Application Rejected’, ‘Unpaid Invoice’ etc etc

I have the following which is working, but the problem is that 'applicant_status is a relation and I’m only getting the id rather the name.

Here is the dropdown code:




// The list should be sorted using applicant_status as an optGroup

CHtml::activeDropDownList($data, 'status',

                    CHtml::listData(Status::model()->findAll(array(

                        'order'=>'status_name')),'status_id', 'status_name', 'applicant_status'));



and the relation




'appStatus' => array(self::BELONGS_TO, 'ApplicantStatus', array(

       'applicant_status'=>'applicant_status_id'), 'through'=>'status'),



This works but I am getting applicant_status_id rather than applicant_status_name.

Any ideas would be most appreciated!

Just found time today to come back to this and it was easy to fix in the end.

Just need to change the findAll() query in linkData to a method that returns an array of all the statuses as well as the relation joined. I was then able to add ‘applicant_status_name’ as the optGroup parameter and everything works fine.

In case anyone ever gets stuck…




// Get the statuses as an array

public function getAllStatuses()

    {

        return Yii::app()->db

            ->createCommand()

            ->select('S.status_id, S.status_name, AS.applicant_status_name')

            ->from('status S')

            ->join('applicant_status AS', 'AS.applicant_status_id=S.applicant_status')

            ->queryAll();

    }






// Replace the above method in listData 

return CHtml::activeDropDownList($data, 'status',

                    CHtml::listData(Status::model()->getAllStatuses(),

                        'status_id', 'status_name', 'applicant_status_name'));