Search code examples
phpdrop-down-menuyiimodels

dropDownList yii 3 or more models


My sample database;

My sample databese

MODEL / REALSTATE.PHP

    public function relations()
{
    return array(
        'neighborhood' => array(self::BELONGS_TO, 'Neighborhood', 'fk_id_neighborhood'),

    );
}

What I need is when registering a property, have all option, see an example; VIEW / REALESTATE / ADD.PHP

enter image description here

My question is not related to ajax, is how to do this integration for each DropDownList I have a specific model? like this?


Solution

  • You will need ajax anyway.

    $form->dropDownList($model, $state, CHtml::listData(State::model()->findAll(),'id','state'), array(
            'empty'=>"Select state",    
            'ajax' => array(
                            'type' => 'POST',
                            'url'=>$this->createUrl('registration/state'),   
                            'update' => '#YOURcityID',                        
                    'data'=>array('state'=>'js:this.value',),   
                'success'=> 'function(data) {$("#YourcityID").empty();
                                $("#YourcityID").append(data);
    
                                                    } ',
    
            )));
    

    In yourcontroller:

    public function actionState()
    {
    
     $data=Cities::model()->findAll('fk_id_state=:state', 
                      array(':state'=> $_POST['state']));
    
        $data=CHtml::listData($data,'city_id','city');
                foreach($data as $value=>$city)  {
                            echo CHtml::tag
                                    ('option', array('value'=>$value),CHtml::encode($city),true);
                        }   
    
    }
    

    Back to form:

    $form->dropDownlist($model, 'city', array(),array(
            'empty'=>"Select city",
    'ajax' => array(
                                'type' => 'POST',
                                'url'=>$this->createUrl('registration/city'),   
                                'update' => '#YOURneighborhoodID',                        
                        'data'=>array('city'=>'js:this.value',),    
                    'success'=> 'function(data) {                                       $("#YourneighborhoodID").empty();
                                                $("#YourneighborhoodID").append(data);
    
                                                        } ',
            ));
    

    In controller:

    public function actionCity()
    {
    
     $data=Neighborhood::model()->findAll('fk_id_city=:city', 
                      array(':city'=> $_POST['city']));
    
        $data=CHtml::listData($data,'neghborhood_id','neighborhood');
                foreach($data as $value=>$neighborhood)  {
                            echo CHtml::tag
                                    ('option', array('value'=>$value),CHtml::encode($neighborhood),true);
                        }   
    
    }
    

    This will work like a charm. Check for typos and your variables cause i was hurry. Regards.