How the update operation works in Yii

I am new to the Yii framework. I created a table TblUser. Now I have three columns username, passwordand email. I use operations CRUDin Yii. The connection to the database was successful. I also managed to create a new record in the table. Now the database entry reads as

+----+----------+----------+--------------------+
| id | username | password | email              |
+----+----------+----------+--------------------+
|  1 | test1    | pass1    | test1@example.com  |
|  2 | test2    | pass2    | test2@example.com  |
|  3 | test3    | pass3    | test3@example.com  |
+-----------------------------------------------+

Now in the presentation form, I deleted the field emailby commenting (_form.php). I use this form to update the remaining fields.

 <?php
    /* @var $this TblUserController */
    /* @var $model TblUser */
    /* @var $form CActiveForm */
    ?>

    <div class="form">

    <?php $form=$this->beginWidget('CActiveForm', array(
            'id'=>'tbl-user-form',
            'enableAjaxValidation'=>false,
    )); ?>

            <p class="note">Fields with <span class="required">*</span> are required.</p>

            <?php echo $form->errorSummary($model); ?>

            <div class="row">
                    <?php echo $form->labelEx($model,'username'); ?>
                    <?php echo $form->textField($model,'username',array('size'=>60,'maxlength'=>128)); ?>
                    <?php echo $form->error($model,'username'); ?>
            </div>

            <div class="row">
                    <?php echo $form->labelEx($model,'password'); ?>
                    <?php echo $form->passwordField($model,'password',array('size'=>60,'maxlength'=>128)); ?>
                    <?php echo $form->error($model,'password'); ?>
            </div>
<!--    
            <div class="row">
                    <?php echo $form->labelEx($model,'email'); ?>
                    <?php echo $form->textField($model,'email',array('size'=>60,'maxlength'=>128)); ?>
                    <?php echo $form->error($model,'email'); ?>
            </div>
-->    
            <div class="row buttons">
                    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
            </div>

    <?php $this->endWidget(); ?>

To use the rest of the fields, I commented out the field emailin _form.php. Now in my controller I have an update code as shown below:

public function actionUpdate($id)
        {
                $model=$this->loadModel($id);

                // Uncomment the following line if AJAX validation is needed
                // $this->performAjaxValidation($model);

                if(isset($_POST['TblUser']))
                {
                        $model->attributes=$_POST['TblUser'];
                        if($model->save())
                                $this->redirect(array('view','id'=>$model->id));
                }

                $this->render('update',array(
                        'model'=>$model,
                ));
        }

When I see request logs, updated request

UPDATE `tbl_user` SET `id`=2, `username`='1234', `password`='1234', `email`='test2@example.com' WHERE `tbl_user`.`id`=2  

but not

UPDATE `tbl_user` SET `password`='1234' WHERE `tbl_user`.`id`=2  

, , , . . .

+4
3

Yii, , .

, .

$model = new User; 

isNewRecord. false/0. /

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

isNewRecord true/1

Insert Update.

Yii , . , ( i.e) .

+1

. , , . 1 , . , 1. , , . , Yii , . , !

0

, update() save(). :

$model->update(array('password'));

Yii does not automatically detect which columns have been changed, so you only need to track if you want to update the columns that have changed. But in the case of an update password form or update status or something similar, to use update()instead save(), since you know that only one field changes.

For reference: http://www.yiiframework.com/doc/api/1.1/CActiveRecord#update-detail

0
source

All Articles