Update related table

Hi,

looking through the forum I cannot find a simple solution.

I cannot update the related field idAssicurato0->nomeAssicurato selected by a dropdown, like this

_form.php




<?php


use yii\helpers\Html;

use yii\widgets\ActiveForm;

use yii\helpers\ArrayHelper;

use app\models\Assicurato;


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

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

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

?>


<div class="pratica-form">


    <?php $form = ActiveForm::begin(); ?>

	

    <?= $form->field($model, 'protocollo')->textInput() ?>


	<?= $form->field($model, 'id')->dropDownList(ArrayHelper::map(Assicurato::find()->all(), 'id', 'nomeAssicurato')) ?>

	

    <?= $form->field($model, 'numeroPolizza')->textInput() ?>


    <?= $form->field($model, 'tipoPolizza')->textInput() ?>


    <?= $form->field($model, 'dataSinistro')->textInput() ?>


    <?= $form->field($model, 'tipoEvento')->textInput() ?>


    <?= $form->field($model, 'indirizzoSinistro')->textInput(['maxlength' => 11]) ?>


    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    </div>


    <?php ActiveForm::end(); ?>


</div>



this is the model:




<?php


namespace app\models;


use Yii;


/**

 * This is the model class for table "pratica".

 *

 * @property integer $id

 * @property integer $protocollo

 * @property integer $idAssicurato

 * @property integer $numeroPolizza

 * @property integer $tipoPolizza

 * @property string $dataSinistro

 * @property integer $tipoEvento

 * @property string $indirizzoSinistro

 *

 * @property Assicurato $idAssicurato0

 */

class Pratica extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'pratica';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['protocollo', 'idAssicurato', 'numeroPolizza', 'tipoPolizza', 'tipoEvento'], 'integer'],

            [['dataSinistro'], 'safe'],

            [['indirizzoSinistro'], 'string', 'max' => 11]

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'id' => 'ID',

            'protocollo' => 'Protocollo',

            'idAssicurato' => 'Id Assicurato',

            'numeroPolizza' => 'Numero Polizza',

            'tipoPolizza' => 'Tipo Polizza',

            'dataSinistro' => 'Data Sinistro',

            'tipoEvento' => 'Tipo Evento',

            'indirizzoSinistro' => 'Indirizzo Sinistro',

        ];

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getIdAssicurato0()

    {

        return $this->hasOne(Assicurato::className(), ['id' => 'idAssicurato']);

    }

}




and this is the controller:




<?php


namespace app\controllers;


use Yii;

use app\models\Pratica;

use app\models\PraticaSearch;

use yii\web\Controller;

use yii\web\NotFoundHttpException;

use yii\filters\VerbFilter;


/**

 * PraticaController implements the CRUD actions for Pratica model.

 */

class PraticaController extends Controller

{

    public function behaviors()

    {

        return [

            'verbs' => [

                'class' => VerbFilter::className(),

                'actions' => [

                    'delete' => ['post'],

                ],

            ],

        ];

    }


    /**

     * Lists all Pratica models.

     * @return mixed

     */

    public function actionIndex()

    {

        $searchModel = new PraticaSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


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

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }


    /**

     * Displays a single Pratica model.

     * @param integer $id

     * @return mixed

     */

    public function actionView($id)

    {

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

            'model' => $this->findModel($id),

        ]);

    }


    /**

     * Creates a new Pratica model.

     * If creation is successful, the browser will be redirected to the 'view' page.

     * @return mixed

     */

    public function actionCreate()

    {

        $model = new Pratica();


        if ($model->load(Yii::$app->request->post()) && $model->save()) {


            return $this->redirect(['view', 'id' => $model->id]);

        } else {

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

                'model' => $model,

            ]);

        }

    }


    /**

     * Updates an existing Pratica model.

     * If update is successful, the browser will be redirected to the 'view' page.

     * @param integer $id

     * @return mixed

     */

    public function actionUpdate($id)

    {

        $model = $this->findModel($id);


        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->id]);

        } else {

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

                'model' => $model,

            ]);

        }

    }


    /**

     * Deletes an existing Pratica model.

     * If deletion is successful, the browser will be redirected to the 'index' page.

     * @param integer $id

     * @return mixed

     */

    public function actionDelete($id)

    {

        $this->findModel($id)->delete();


        return $this->redirect(['index']);

    }


    /**

     * Finds the Pratica model based on its primary key value.

     * If the model is not found, a 404 HTTP exception will be thrown.

     * @param integer $id

     * @return Pratica the loaded model

     * @throws NotFoundHttpException if the model cannot be found

     */

    protected function findModel($id)

    {

        if (($model = Pratica::findOne($id)) !== null) {

            return $model;

        } else {

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

        }

    }

}



I think it’s a simple thing but I cannot find an example quite simple easy for me to understand. If someone could me explain the logic behind it would be great!

Bye

Alex

Your dropdown lets users set an id, not an idAssicurato. Is that intended?

Hello Sourcerer, yes you are right!

That was the issue, is was using id and not idAssicurato… just a stupid error, but now I understand something more.

I believe that the road for fully yii comprehension will be still veery long ;)

Thank you so much

Alex