Widgets And Models

Hello,

How to access my model from my widget ?

Here \components\views\tfLanguagesCreate.php




<div id="tfLanguagesCreate"> 

<?php  


    $Langues = baLangue::model()->findAll(array('order' => 'langue_id'));

    $i = -1;

  

    foreach ($Langues as $Langue) {

        $i +=1;

        ?>

        <div class="row">

            <?php 

       

            echo CHtml::activeLabel($model, yii::t('view', 'nom') . ' [' . $Langue->langue_nom . ']'); 

            ?>

            <?php echo CHtml::activeTextField($modelLangue, '[' . $i . ']nom'); 

            ?>

        </div>


        <?php

    }

?>

</div>



here my class :




class textFieldLanguagesCreate extends CWidget{

    //put your code here

    public $myLangues = array();

    

   // public $form;

    public function init()

    {

         parent::init();

    }

    

    

    public function run()

    {   

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

            'model' => $model,

            'modelProfilLangue' => $modelProfilLangue,

        ));

        

        

    }

}



how I use my widget :




<?php 

                 $this->widget('application.components.textFieldLanguagesCreate'

                 ); 

     ?>



I have this erreor :

what is wrong please?

Nath

Where are $model and $modelProfilLangue coming from in your run() method? The widget class doesn’t have access to these objects.

One option is to create instance variables for the objects and assign to them when you construct the widget:




class textFieldLanguagesCreate extends CWidget{

    public $myLangues = array();

    public $model = null;

    public $modelProfilLangues = null;

    

    public function init()

    {

         parent::init();

    }

    

    

    public function run()

    {   

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

            'model' => $this->model,

            'modelProfilLangue' => $this->modelProfilLangue,

        ));

    }

}



Then when calling the widget:




    $this->widget('application.components.textFieldLanguagesCreate',

        array('model'=>$model, 'modelProfilLangue'=>$modelProfilLangue)

    ); 



Or something like that.

thank you for your answer. But I still have a problem.

now I got this error message :

Can you post the line of code that that refers to? The full stack trace would be useful.

I give you the full stack trace :

In lines 31 and 32, you should be using $model and $modelProfilLangue without $this->. That’s assuming that you send them to the view in variables with those names.

ok, but if I do that :

my widget :




 

   class textFieldLanguagesCreate extends CWidget{

    //put your code here

    //public $myLangues = array();

    public $model = null;

    public $modelProfilLangue = null;

    

   // public $form;

    public function init()

    { 

         parent::init();

        $model = new AdProfil;

        $modelProfilLangue = new AdProfilLangue;

         

    }


    

    public function run()

    {   

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

            'model' => $model,

            'modelProfilLangue' => $modelProfilLangue,

        ));

        

    }

}

        



and in my page :




<?php   

                 $this->widget('application.components.textFieldLanguagesCreate',

                        array('model'=>$model, 

                              'modelProfilLangue'=>$modelProfilLangue)); 

                 

     ?>



After I don’t get the object model, I have this eror :

I don’t anderstand how to pass my model from the page who call the widget :huh:

Where are you populating $model and $modelProfilLangue?

I tought that $model and $modelProfilLangue was populating there :




 $this->widget('application.components.textFieldLanguagesCreate',

                        array('model'=>$model, 

                              'modelProfilLangue'=>$modelProfilLangue)); 



but apparently, not !

It is that the point, I don’t anderstand how and where I have to populate them.

I tried to find some exemple, but I didn’t find anything.

Before passing those objects into the widget, you need to populate them with data. You generally do that in the action, then pass them into the view using the second parameter of render().

thank you very much for your help.

So I don’t use anymore a Widget.

I have my page where I call my view :




 <?php

      

        echo $this->renderPartial('application.components.views.textFieldLanguagesCreate',

                array('model'=>$model, 

                      'modelProfilLangue'=>$modelProfilLangue,

                      'form' => $form));             

     ?>



and the Partial view (application.components.views.textFieldLanguagesCreate):




<div id="textFieldLanguagesCreate"> 

<?php  


    $Langues = baLangue::model()->findAll(array('order' => 'langue_id'));

    $i = -1;

  

    foreach ($Langues as $Langue) {

        $i +=1;

        ?>

        <div class="row">

             <?php echo $form->labelEx($model, yii::t('view', 'nom') . ' [' . $Langue->langue_nom . ']'); ?>

            <?php echo $form->textField($modelProfilLangue, '[' . $i . ']nom'); ?>

            <?php echo $form->error($modelProfilLangue, '[' . $i . ']nom'); ?>

        </div>


        <?php

    }

?>

</div>