How does the update operation work in Yii

Viewed 6968

I'm new to Yii framework. I created a table TblUser. Now, I have three columns username,password and email. I'm using CRUD operations in Yii. Connection to database is successfully made. I was able to create a new record in the table also. Now the entry in the database 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 view form I removed the field email by commenting (_form.php). I'm using 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 rest of the fields I commented the email field in the _form.php. Now in my controller I have the 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 my query logs, the query updated is

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

and not

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

Now my question why is updating the unchanged values again instead of updating only changed values. Does Yii do this update query for everything. How does this work.

3 Answers
Related