Auto Update in ActiveRecord form

Hi,

I have one update form page which was extended from ActiveRecord class.

I changed some data in the update page. Then I went to another page such as View page.

The data were saved once I lost focus from that Update page.

How can I remove that auto-save function?

I appreciate your help.

Win

Is not that clear what you are meaning, but there is no auto-save function.

If you use the CRUD generated code, it is saving only if you are pressing the save/create button, links on menu are not saving.

If you were using your own code, just keep in mind that the function CActiveRecord::save() is the only one that actually saves.

Hi Zaccaria,

I generated all forms with CRUD. Then I modified a bit. I was aware that it saved automatically after I changed and move the focus to the other page. The process is tested in my Update page only.

This is my Update Action.

/**

 * Updates a particular model.


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


 */


public function actionUpdate()


{


        $model = new TsAddressBook('update');


        $model->setIsNewRecord(false);





        // If not in login state, redirect to Login page

// if(!TsCustomers::model()->isLoginState())

// $this->redirect(array(‘site/login’));

        if(isset($_POST['TsAddressBook']))


        {


            $_addressbookId = Yii::app()->user->getState('sesAddressBookId');


            //$model = $this->retrieveModel($_addressbookId);


            $model->attributes=$_POST['TsAddressBook'];


            $model->address_book_id = $_addressbookId;


            //$model->customers_id= TsCustomers::model()->getLoginUserId();





            Yii::trace('Before Validate.....................');





            if($model->validate()) {


                Yii::trace('After Validate.....................');


                //$model->address_book_id = Yii::app()->user->getState('sesAddressBookId');


                //$model->customers_id= TsCustomers::model()->getLoginUserId();


                //Yii::trace('Address book id for Update.................'.$model->address_book_id);


                //Yii::trace('Is New Record:...................'.$model->getIsNewRecord());





                if($model->save(false))


                    $this->redirect(array('view','id'=>$model->address_book_id));


            }


            else {


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


                    'model'=>$model,


                ));


            }


            


            


        }            


        else {


            $model = $this->retrieveModel(Yii::app()->user->getState('sesAddressBookId'));                


            if($model !== null)


            {


                Yii::app()->user->setState('sesAddressBookId', $model->address_book_id);


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


                    'model'=>$model,


                ));


            }


        }


}

This is my Update view page.

<?php $form=$this->beginWidget(‘CActiveForm’, array(

'id'=&gt;'ts-address-book-form',


'enableAjaxValidation'=&gt;false,

)); ?>

&lt;?php &#036;countries = TsCountries::model()-&gt;getCountries(); ?&gt;





&#60;script type=&quot;text/javascript&quot;&gt;


   &#036;(document).ready(function(){


       &#036;('#butCancel').click(function(){


            &#036;('#ts-address-book-form').action = '&lt;?php echo Yii::app()-&gt;request-&gt;baseUrl;?&gt;/index.php?r=tsAddressBook/view';


            &#036;('#ts-address-book-form').submit();


       });





       &#036;('#_checkBilling').change(function(){


           if(&#036;('#_checkBilling').is(':checked')) {


               &#036;('#billingaddressblock').show('slow');


           }


           else {


               &#036;('#billingaddressblock').hide(1000);


           }





       });





       &#036;('#_checkDelivery').change(function(){


           if(&#036;('#_checkDelivery').is(':checked')) {


               &#036;('#deliveryaddressblock').show('slow');


           }


           else {


               &#036;('#deliveryaddressblock').hide(1000);


           }





       });


   });


&lt;/script&gt;





&lt;p class=&quot;note&quot;&gt;Fields with &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; are required.&lt;/p&gt;





&lt;?php echo &#036;form-&gt;errorSummary(&#036;model); ?&gt;





    &lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;


        &lt;tr&gt;


            &lt;td colspan=&quot;2&quot; class=&quot;colourrow&quot;&gt;&lt;h4&gt;Contact Address&lt;/h4&gt;&lt;/td&gt;


        &lt;/tr&gt;


        &lt;tr&gt;


            &lt;td&gt;&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'company'); ?&gt;&lt;/td&gt;


            &lt;td colspan=&quot;2&quot; &gt;


                &lt;?php echo &#036;form-&gt;textField(&#036;model,'company',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;


                &lt;?php echo &#036;form-&gt;error(&#036;model,'company'); ?&gt;


            &lt;/td&gt;


        &lt;/tr&gt;


        &lt;tr&gt;


            &lt;td colspan=&quot;2&quot;&gt;Contact Person&lt;/td&gt;


        &lt;/tr&gt;


        &lt;tr&gt;


            &lt;td&gt;&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'firstname'); ?&gt;&lt;/td&gt;


            &lt;td&gt;&lt;?php echo &#036;form-&gt;textField(&#036;model,'firstname',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;


                &lt;?php echo &#036;form-&gt;error(&#036;model,'firstname'); ?&gt;


            &lt;/td&gt;





        &lt;/tr&gt;


        &lt;tr&gt;


            &lt;td&gt;&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'lastname'); ?&gt;&lt;/td&gt;


            &lt;td&gt;&lt;?php echo &#036;form-&gt;textField(&#036;model,'lastname',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;


                &lt;?php echo &#036;form-&gt;error(&#036;model,'lastname'); ?&gt;


            &lt;/td&gt;


        &lt;/tr&gt;


        &lt;tr&gt;


            &lt;td&gt;&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'street_address'); ?&gt;&lt;/td&gt;


            &lt;td&gt;


                &lt;?php echo &#036;form-&gt;textField(&#036;model,'street_address',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;


                &lt;?php echo &#036;form-&gt;error(&#036;model,'street_address'); ?&gt;


            &lt;/td&gt;


        &lt;/tr&gt;


        &lt;tr&gt;


            &lt;td&gt;&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'suburb'); ?&gt;&lt;/td&gt;


            &lt;td&gt;


                &lt;?php echo &#036;form-&gt;textField(&#036;model,'suburb',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;


                &lt;?php echo &#036;form-&gt;error(&#036;model,'suburb'); ?&gt;


            &lt;/td&gt;


        &lt;/tr&gt;


        &lt;tr&gt;


            &lt;td&gt;&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'postcode'); ?&gt;&lt;/td&gt;


            &lt;td&gt;


                &lt;?php echo &#036;form-&gt;textField(&#036;model,'postcode',array('size'=&gt;20,'maxlength'=&gt;60)); ?&gt;


                &lt;?php echo &#036;form-&gt;error(&#036;model,'postcode'); ?&gt;


            &lt;/td&gt;


        &lt;/tr&gt;


        &lt;tr&gt;


            &lt;td&gt;&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'city'); ?&gt;&lt;/td&gt;


            &lt;td&gt;


                &lt;?php echo &#036;form-&gt;textField(&#036;model,'city',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;


                &lt;?php echo &#036;form-&gt;error(&#036;model,'city'); ?&gt;


            &lt;/td&gt;


        &lt;/tr&gt;


        &lt;tr&gt;


            &lt;td&gt;&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'state'); ?&gt;&lt;/td&gt;


            &lt;td&gt;


                &lt;?php echo &#036;form-&gt;textField(&#036;model,'state',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;


                &lt;?php echo &#036;form-&gt;error(&#036;model,'state'); ?&gt;


            &lt;/td&gt;


        &lt;/tr&gt;


        &lt;tr&gt;


            &lt;td&gt;&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'country_id'); ?&gt;&lt;/td&gt;


            &lt;td&gt;


                &lt;?php echo CHtml::dropDownList('TsAddressBook[country_id]', &#036;model-&gt;country_id, CHtml::listData(&#036;countries,'countries_id','countries_name'), array('empty' =&gt; '--Please Select--')); ?&gt;


                &lt;?php echo &#036;form-&gt;error(&#036;model,'country_id'); ?&gt;


            &lt;/td&gt;


        &lt;/tr&gt;


        &lt;tr&gt;


            &lt;td&gt;&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'telephone'); ?&gt;&lt;/td&gt;


            &lt;td&gt;


                &lt;?php echo &#036;form-&gt;textField(&#036;model,'telephone',array('size'=&gt;20,'maxlength'=&gt;60)); ?&gt;


                &lt;?php echo &#036;form-&gt;error(&#036;model,'telephone'); ?&gt;


            &lt;/td&gt;


        &lt;/tr&gt;


        &lt;tr&gt;


            &lt;td&gt;&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'fax'); ?&gt;&lt;/td&gt;


            &lt;td&gt;


                &lt;?php echo &#036;form-&gt;textField(&#036;model,'fax',array('size'=&gt;20,'maxlength'=&gt;60)); ?&gt;


                &lt;?php echo &#036;form-&gt;error(&#036;model,'fax'); ?&gt;


            &lt;/td&gt;


        &lt;/tr&gt;


        &lt;tr&gt;


            &lt;td colspan=&quot;2&quot;&gt;


                &lt;?php echo MyHtml::checkBox('_checkBilling', false, array('id'=&gt;'_checkBilling'));?&gt;


                &lt;?php echo MyHtml::label('Show/Hide Billing Address', 'showhidebillingblock') ?&gt;


            &lt;/td&gt;


        &lt;/tr&gt;


        &lt;tr&gt;


            &lt;td colspan=&quot;2&quot;&gt;


                 &lt;?php echo MyHtml::checkBox('_checkDelivery', false, array('id'=&gt;'_checkDelivery'));?&gt;


                &lt;?php// echo MyHtml::checkBox(&#036;model, '_checkDelivery');?&gt;


                &lt;?php echo MyHtml::label('Show/Hide Delivery Address', 'showhidedeliveryblock') ?&gt;


            &lt;/td&gt;


        &lt;/tr&gt;


    &lt;/table&gt;

</div>

&lt;div class=&quot;row buttons&quot;&gt;


	&lt;?php echo CHtml::submitButton(&#036;model-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt;


            &lt;?php echo MyHtml::button('Cancel', array('id'=&gt;'butCancel')); ?&gt;


&lt;/div&gt;

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

</div><!-- form -->

Thanks for your help. I am looking forward your reply.




public function actionUpdate()

{

   $model = new TsAddressBook('update');

   $model->setIsNewRecord(false);



Just for begin, why aren’t you loading from database the record to update?

I suggest you to restart from a CRUD generated update, this code is really strange. You should start with a loadMode, that do something like TsAddressBook::model()->findByPk() and so on.

Also the setIsNewRecord should not be used like that.

Hi,

I removed the action assignment in the Javascript in the View page.

$(’#ts-address-book-form’).action = ‘<?php echo Yii::app()->request->baseUrl;?>/index.php?r=tsAddressBook/view’;

$(’#ts-address-book-form’).submit();

That is something wrong. After I remove the action from the Javascript, the data was not updated automatically.

It worked fine.

Thank you so much for your suggestion. Hope to see you again.

Win