Cannot Get The Values Of Related Model In Form Fields

I’m new to Yii framework. _form.php has fields of two tables. I can get the values of one table populated in its fields. But, for the second model the values are not getting populated in the respective fields of update.php. But, I can print it on screen. Why is this happening. How can I get it populated to fields in the form.

Controller:


 public function actionUpdate($id)

    {       $model_al=new AuditLogin;    

                //$model_apt=new Apartment;    

        //$model=$this->loadModel($id);

                 $model= ApartmentSettings::model()->findByPk($id);

                 $model_apt = Apartment::model()->findByAttributes(array('Id'=>$model->ApartmentId));

                

    

    

        // Uncomment the following line if AJAX validation is needed

        // $this->performAjaxValidation($model);


        if(isset($_POST['ApartmentSettings'])&&isset($_POST['AuditLogin']))

        {

            

                    $model->attributes=$_POST['ApartmentSettings'];                                                                                                                                                                                      

                      

                        if($model->save())                                                  

                        $model_al->attributes=$_POST['AuditLogin'];                        

                        $approval=Yii::app()->getRequest()->getParam('approval');

                        echo $approval;

                        if ($approval==0)

                        {

                            

                            $model_al->moduleId="1001";

                            $model_al->content=$model->GreetingsApprovedSMSText;

                            $greet_approve=$model->GreetingsApproval;

                            if ($greet_approve==0)

                            $model_al->activity="Greetings approval rejected";

                            else    

                            $model_al->activity="Greetings approval accepted";

                        echo $approval;

                            

                        }

                        elseif($approval==1){

                            

                        $model_al->moduleId="1002";

                        $model_al->content=$model->ContactApprovedSMSText;                        

                        $contact_approve=$model->ContactApproval;

                        if ($contact_approve==0)

                        $model_al->activity="Contact approval rejected";

                        else    

                        $model_al->activity="Contact approval accepted";                                                    

                        

                        }  

                        

                        elseif($approval==2)

                        {                                                    

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

                        $model_apt->attributes=$_POST['Apartment'];    

                        $model_al->moduleId="1004";

                        $model_al->activity="Microsite fields updated";                

                        $model_al->content=$_POST['Apartment']['MetatagsDesc'];                        

                        $model_apt->save();                        

                        

                        }

                        

                        if($model_al->save())                            

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

                }

                

                

                      

                

        $this->render('update',array(

            'model'=>$model,'model_apt'=>$model_apt,'model_al'=>$model_al,

        ));

    }

When I print the values in update.php, the values get printed in update page.




 <?php

echo "latitude :" . $model_apt->Latitude;

echo "longitude :" . $model_apt->Longitude;

echo $this->renderPartial('_form', array(

    'model' => $model,

    'model_apt' => $model_apt,

    'model_al' => $model_al

));

?> 



But, it doesn’t get populated in the fields of _form.php.




 <div class="row">

    <?php echo $form->labelEx($model_apt, 'Latitude'); ?>            

    <?php echo $form->textField($model_apt, 'Latitude', array('size' => 60, 'maxlength' => 250)); ?>

    <?php echo $form->error($model_apt, 'Latitude'); ?>    

    </div>

            

    <div class="row">        

    <?php echo $form->labelEx($model_apt, 'Longitude'); ?>            

    <?php echo $form->textField($model_apt, 'Longitude', array('size' => 60, 'maxlength' => 250)); ?>

    <?php echo $form->error($model_apt, 'Longitude'); ?>

    </div>



Hi Rudra




 <?php

echo "latitude :" . $model_apt->Latitude;

echo "longitude :" . $model_apt->Longitude;

echo $this->renderPartial('_form', array(

    'model' => $model,

    'model_apt' => $model_apt,

    'model_al' => $model_al

));

?> 



In your update.php, remove the ‘echo’ that is before $this->renderPartial. I think that’s might be a problem. If that does not work, can you post the complete _form.php?

Rudra

Your question here appears to be similar to the one you posted at:

http://www.yiiframework.com/forum/index.php/topic/46711-cannot-get-the-values-of-related-model-in-form-fields/

It got me confused.