Multipe Saves

I am having an issue where my active record model inserts 2 (and occasionally more) records when saving rather than just one. I have not had this issue with any other model and I am not sure what could be causing it. I’m at a total lost.

The CActiveRecord is for a table called ‘Admin’ and the only edited methods look like this:




	public function beforeSave()

	{

          $this->dataedit = false;

          return parent::beforeSave();

        }


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('username, password, last_name, email', 'required'),

			array('admin_level', 'numerical', 'integerOnly'=>true),

			array('username, first_name, last_name, email, login, sessionid', 'length', 'max'=>50),

			array('password', 'length', 'max'=>255),

			array('username,password, first_name, last_name, email,logout, online, timest, lasttimest, account_id, menutype, created_on, dataedit, edited_on, created_by, edited_by, working_mode, sent_arguments, id_ref', 'safe'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('username, password, first_name, last_name, email, admin_level, id, login, logout, online, sessionid, timest, lasttimest, account_id, menutype, created_on, dataedit, edited_on, created_by, edited_by, working_mode, sent_arguments, id_ref', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'account' => array(self::BELONGS_TO, 'Account', 'account_id'),

		);

	}



and the controller method that handles the saving looks like this:




	public function actionMenuTest()

	{


         $user = new Admin;

         $user->account_id = $this->userAccount->account_id;


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

          {

            $user->attributes = $_POST['Admin'];


            if($user->save())

            {

              $user = new Admin;

              $user->account_id = $this->userAccount->account_id;


            }

          }


         $this->render('menutest', array('user'=>$user));


        }



Has anyone else run into this issue?

Check your configuration for AJAX validation. Try disabling it in the view and check if the problem is gone.

I think your model and controller is fine. Please post your view(menutest) here, maybe something wrong there…

Here is the entire contents of the view:




<div class="wide form">




<?php if($user->isNewRecord): ?>


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

	'action'=>Yii::app()->createUrl('User/search'),

	'method'=>'get',

)); ?>

  <div class="labe_main_header">

   <br />

    Search For an Existing User

  </div>

  <br />


  <div class="row">

<label for="search_last">Last Name:</label>

<?php


    echo CHtml::textField('search_last');


?>

  </div>


	<div class="row buttons">

		<?php echo CHtml::submitButton('Search'); ?>

	</div>

	


<?php  if(isset($last) && $last !== null): ?>




<br />

<div class="hr">

<hr />

</div>


  <div class="labe_main_header">

   <br />

    Select Existing User

  </div>




<?php

//if($entity->isNewRecord && $busName !== null)

//if(isset($last) && $last !== null)

{

$dataProvider=new CActiveDataProvider('Admin',array(

'criteria'=>array('condition'=>'last_name like \'%'.$last.'%\''),

'pagination'=>array('pageSize'=>5) )

);


$this->widget('zii.widgets.grid.CGridView', array(

    'dataProvider'=>$dataProvider,

    'columns'=>array(

                     array('header'=>'Username',

                           'class'=>'CLinkColumn',

                           'labelExpression'=>'$data->username',

                           'urlExpression'=>'array(\'User/Edit\', \'id\'=>' . '$data->id'.')'

                           //CHtml::link('Link',)

                           //'value'=>CHtml::link('Link','www.google.com')//'CHtml::link(\'Link\',\'www.google.com\')'  //CHtml::link('Link','www.google.com')


                           //array('BusinessEntity/Edit', 'id'=>'$data->id')

                           ),


                     'first_name',

                     'last_name',),


));


}

?>


<?php endif; ?>


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




<br />

<div class="hr">

<hr />

</div>

<?php endif; ?>


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

	'id'=>'user-form',

	'enableAjaxValidation'=>true,

	'method'=>'post'

));




CHtml::$afterRequiredLabel = '';//hide default required label

?>








  <div class="labe_main_header">

   <br />

    Create a New User

  </div><br />







	<div class="row">

		<?php echo $form->labelEx($user,'username'); ?>

		<?php echo $form->textField($user,'username'); ?>

		<?php echo $form->error($user,'username'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($user,'password'); ?>

		<?php echo $form->passwordField($user,'password'); ?>

		<?php echo $form->error($user,'password'); ?>

	</div>

	

	<div class="row">

		<?php echo $form->labelEx($user,'password_repeat'); ?>

		<?php echo $form->passwordField($user,'password_repeat'); ?>

		<?php echo $form->error($user,'password_repeat'); ?>

	</div>

	

	<div class="row option">

		<?php echo $form->labelEx($user,'email'); ?>

		<?php echo $form->textField($user,'email'); ?>

		<?php echo $form->error($user,'email'); ?>

	</div>


	<div class="row option">

		<?php echo $form->labelEx($user,'first_name'); ?>

		<?php echo $form->textField($user,'first_name'); ?>

		<?php echo $form->error($user,'first_name'); ?>

	</div>

        

	<div class="row">

		<?php echo $form->labelEx($user,'last_name'); ?>

		<?php echo $form->textField($user,'last_name'); ?>

		<?php echo $form->error($user,'last_name'); ?>

	</div>


<br />

<h3>Advanced Menu Options</h3>

<?php


//$this->buildAdvancedMenuOptions($user);

//$this->buildBasicMenuOpts($user);


?>

       <div class="row">

          <?php echo CHtml::label('Account Administration:', 'Perms[AccAdmin]'); ?>

         <div class="inlineRadioButton">

          <?php echo CHtml::radioButtonList('Perms[AccAdmin]','0', $this->getPermLevelOpts(), array('separator'=>'')); ?>

         </div>

       </div>

       

       <div class="row">

         <?php echo CHtml::label('Records:', 'Perms[Records]'); ?>

         <div class="inlineRadioButton">

         <?php echo CHtml::radioButtonList('Perms[Records]','0', $this->getPermLevelOpts(), array('separator'=>'')); ?>

         </div>

       </div>


       <div class="row">

         <?php echo CHtml::label('Disbursements:', 'Perms[Disbursements]'); ?>

         <div class="inlineRadioButton">

         <?php echo CHtml::radioButtonList('Perms[Disbursements]','0', $this->getPermLevelOpts(), array('separator'=>'')); ?>

         </div>

       </div>


       <div class="row">

         <?php echo CHtml::label('Reports:', 'Perms[Reports]'); ?>

         <div class="inlineRadioButton">

         <?php echo CHtml::radioButtonList('Perms[Reports]','0', $this->getPermLevelOpts(), array('separator'=>'')); ?>

         </div>

       </div>


       <div class="row">

         <?php echo CHtml::label('Transfers and Loans:', 'Perms[Loans]'); ?>

         <div class="inlineRadioButton">

         <?php echo CHtml::radioButtonList('Perms[Loans]','0', $this->getPermLevelOpts(), array('separator'=>'')); ?>

         </div>

       </div>








	<div class="row buttons">

		<?php echo CHtml::submitButton('Save'); ?>

	</div>


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


</div>




Also, disabling ajax-validation had no effect. They model is still doubling.

Since you have ajax validation enabled in the view, you’ll have to add back the (Gii-generated) call to performAjaxValidation() in the controller.

/Tommy

tri is right.

Also, could you check if there are multiple POSTs?

You can use Firefox + Firebug/Network tab.

Thanks Tommy, that did the trick.