Yii - Update field

Hello everyone,

I am new to using the Yii framework, I have a model that corresponds to a table in the database and I need a form to edit only one field of that table, this table is made up of two primary keys.

For example, I have a table with 3 attributes, ID, ID2, and Name (Both ID and ID2 are primary keys). When I enter a user profile, I want it to fetch both ID’s and only change the name field, What do I need to do?

I’ve already tried to create a form in the views where I want it to appear, and a function in the model that fetches the ID’s from the user I’m searching, I can see the ‘Name’ field of that user, but I can not edit it.

Thank you all for your time. :D

Can you show us the code you wrote so far?

Hi, thanks for the reply, in the view:




<div class="form-group">

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

</div>



In the model,something like that




public function actionUpdate($id1, $id2) {

        $model = $this->loadModel($id1, 'notas');




        if (isset($_POST['Notas'])) {

            $model->setAttributes($_POST['Notas']);


            if ($model->save()) {

                $this->redirect(array('view', 'id1' => $model->id1,'id2' => $model->id2 ));

            }

        }


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

            'model' => $model,

        ));

}






$model = $this->loadModel($id1, 'notas');



Why do you use only one field to load the model?

I tried another solution, through a query, how do I have a form to update that field?


public static function updateField($ID1, $Notes){

     Yii::$app->db->createCommand("UPDATE Table SET Notes= '$Notas' WHERE ID1= '$ID1'")

    ->execute();

  }

  



I tried a form like this in the view and nothing happened




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

 <?= $form->field($model, 'ID1')->Input(['readOnly' => true]) ?>

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

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

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