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?