Yii2 how to set and update data from other models?

In gridview action column… there’s 3 action (view,update,delete) ,

when you click the UPDATE button… it will go to update form and the textfield is filled with data from database…

my problem is… i have other models in 1 form… i want to set their data too in the added textfield when i click the update button…

here are the 2 models,

(MAIN MODEL) evr_tbl: (evr_id,username_service_order_no,years_in_service)

&& evr_informants_tbl: (evr_id,name,position,phone_number) any idea? thanks!

provide your _form.php sample code of other model attribute for better understanding…

Heres he Main Model EVR _form.php

############################################################################################

<?php

#use yii\helpers\Html;

#use yii\widgets\ActiveForm;

use kartik\widgets\ActiveForm;

use kartik\builder\Form;

use kartik\helpers\Html;

use app\models\EvrInformants;

/* @var $this yii\web\View */

/* @var $model app\models\Evr */

/* @var $form yii\widgets\ActiveForm */

?>

<div class="evr-form">

&lt;?php &#036;form = ActiveForm::begin(); ?&gt;





&lt;?php 





	echo Form::widget([


    	'model' =&gt; &#036;model,


    	'form' =&gt; &#036;form,


    	'columns' =&gt; 3,


    	'attributes' =&gt; [


        	'name' =&gt; ['type'=&gt;Form::INPUT_TEXT, 'options'=&gt;['placeholder'=&gt;'Enter Name...',]],


        	'position' =&gt; ['type'=&gt;Form::INPUT_TEXT, 'options'=&gt;['placeholder'=&gt;'Enter Position...',]],


        	'phone_number' =&gt; ['type'=&gt;Form::INPUT_TEXT, 'options'=&gt;['placeholder'=&gt;'Enter Phone Number...',]],


    	]





	]);





	echo Form::widget([


    	'model' =&gt; &#036;model,


    	'form' =&gt; &#036;form,


    	'columns' =&gt; 3,


    	'attributes' =&gt; [


        	'years_in_service' =&gt; ['type'=&gt;Form::INPUT_TEXT,  'options'=&gt;['placeholder'=&gt;'Enter Years of Service...',]],


        	'present_position' =&gt; ['type'=&gt;Form::INPUT_TEXT,  'options'=&gt;['placeholder'=&gt;'Enter Present Position...',]],


        	'salary' =&gt; ['type'=&gt;Form::INPUT_TEXT, 'options'=&gt;['placeholder'=&gt;'Enter Salary...',]],


    	]





	]);





	echo Form::widget([


    	'model' =&gt; &#036;model,


    	'form' =&gt; &#036;form,


    	'columns' =&gt; 1,


    	'attributes' =&gt; [


        	'remarks' =&gt; ['type'=&gt;Form::INPUT_TEXTAREA, 'options'=&gt;['placeholder'=&gt;'Write Remarks...',]],


    	]





	]);





?&gt;

<div class="form-group">

	&lt;?= Html::submitButton(&#036;model-&gt;isNewRecord ? 'Create' :  'Update', ['class' =&gt; &#036;model-&gt;isNewRecord ? 'btn btn-success' :  'btn btn-primary']) ?&gt;


&lt;/div&gt;





&lt;?php ActiveForm::end(); ?&gt;

</div>

###########################################################################

here’s the Second Model EVR_iformants _form.php

<?php

use yii\helpers\Html;

use yii\widgets\ActiveForm;

/* @var $this yii\web\View */

/* @var $model app\models\EvrInformants */

/* @var $form yii\widgets\ActiveForm */

?>

<div class="evr-informants-form">

&lt;?php &#036;form = ActiveForm::begin(); ?&gt;





&lt;?= &#036;form-&gt;field(&#036;model, 'name')-&gt;textInput(['maxlength' =&gt; 100]) ?&gt;





&lt;?= &#036;form-&gt;field(&#036;model, 'evr_id')-&gt;textInput(['maxlength' =&gt; 10]) ?&gt;





&lt;?= &#036;form-&gt;field(&#036;model, 'position')-&gt;textInput(['maxlength' =&gt; 100]) ?&gt;





&lt;?= &#036;form-&gt;field(&#036;model, 'phone_number')-&gt;textInput(['maxlength' =&gt; 20]) ?&gt;





&lt;div class=&quot;form-group&quot;&gt;


	&lt;?= Html::submitButton(&#036;model-&gt;isNewRecord ? 'Create' :  'Update', ['class' =&gt; &#036;model-&gt;isNewRecord ? 'btn btn-success' :  'btn btn-primary']) ?&gt;


&lt;/div&gt;





&lt;?php ActiveForm::end(); ?&gt;

</div>

##################################################################################################

attach is what i want to happen.

CREATE EVR is the MAIN MODEL… I ADD The Name, Position and Phone Number in the create form for multiple inserts in models…

NOW, When i got to Index.php when i click the update action( The Pencil Icon.) i want the data from the EVR_informants ( the second Model ) to be pass back from the EVR Form… for update…

In below code $model = ? ;


<?php 


echo Form::widget([

'model' => $model,

'form' => $form,

'columns' => 3,

'attributes' => [

'name' => ['type'=>Form::INPUT_TEXT, 'options'=>['placeholder'=>'Enter Name...',]],

'position' => ['type'=>Form::INPUT_TEXT, 'options'=>['placeholder'=>'Enter Position...',]],

'phone_number' => ['type'=>Form::INPUT_TEXT, 'options'=>['placeholder'=>'Enter Phone Number...',]],

]


]);

?>

Updated :

I suppose add


$evrinfo = new EvrInformants;

in controller file and pass to view file.

than use it as below code…


<?php 


echo Form::widget([

'model' => $evrinfo,

'form' => $form,

'columns' => 3,

'attributes' => [

'name' => ['type'=>Form::INPUT_TEXT, 'options'=>['placeholder'=>'Enter Name...',]],

'position' => ['type'=>Form::INPUT_TEXT, 'options'=>['placeholder'=>'Enter Position...',]],

'phone_number' => ['type'=>Form::INPUT_TEXT, 'options'=>['placeholder'=>'Enter Phone Number...',]],

]


]);

?>

and at time of save use


$evrinfo->attributes=$_POST['EvrInformants'];


$evrinfo->save();

or


$evrinfo->save(false);

Thanks bro…

ahm… Im a bit newbie… so can you post some samples on how can i set multiple models to view?

(in my controller…)

is it here??

public function actionView($evr_id, $username, $service_order_no)

{


	&#036;evrinfo = new EvrInformants;


	return &#036;this-&gt;render('view', [


    	'model' =&gt; &#036;this-&gt;findModel(&#036;evr_id, &#036;username, &#036;service_order_no),


	]);


}

and here?

protected function findModel($evr_id, $username, $service_order_no)

{


	


	if ((&#036;model = Evr::findOne(['evr_id' =&gt; &#036;evr_id, 'username' =&gt; &#036;username, 'service_order_no' =&gt; &#036;service_order_no])) &#33;== null) {


    	return &#036;model;


	} else {


    	throw new NotFoundHttpException('The requested page does not exist.');


	}


}

or the view.php in my views??

thanks…

You can create object of other models.

for example:

first create object of models,




$info = new MemberInfo;

$user =new User;

then render that…




return $this->render('view', [

                'user' => $user, 'info' => $info,

            ]);