[SOLVED] Load or Create new model always save new model

Hi All,

I need to load a TransactionSession for current user or display a form for him/her to create a new session. However, my codes, always create a new session after loading the site/index. I do not call save() on model but why, it always create a new one?




public function actionIndex() {

        if (Yii::app()->user->isGuest) {

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

        } else {

            $this->layout = '//layouts/main';


            $criteria = new CDbCriteria;

            $criteria->condition = 'username = :username AND timeFinish IS NULL';

            $criteria->params = array(':username' => Yii::app()->user->name);

            $criteria->order = 'timeStart DESC';


            $sessionObj = TransactionSession::model()->find($criteria);

            $model = null;


            if ($sessionObj != null) {

                $model->attributes = $sessionObj->attributes;

            } else {

                $model = new TransactionSession;

                $model->openAmount = null;

            }


//            if (isset($_POST['TransactionSession'])) {

//                $model = new TransactionSession;

//                $model->attributes = $_POST['TransactionSession'];

//                $model->username = Yii::app()->user->name;

//                $model->timeStart = date('Y-m-d H:i:s');

//

//                if ($model->save()) {

//                    Yii::app()->user->setFlash('success', 'Sesi transaksi telah berhasil dibuka.');

//                    $this->redirect(array('/site/index'));

//                } else {

//                    Yii::app()->user->setFlash('error', 'Sesi transaksi gagal dibuka!');

//                }

//            }


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

                'sessionObj' => $model,

            ));

        }

    }



As if my commented code always executed if I uncomment them. Can anyone help on this?

TIA

I was wondering why this line


 $model = new TransactionSession;

will always create a new row in the database? Why do I need not to call save() method but it automatically save the model?

Is it because I use this on the rules?


array('id', 'default', 'setOnEmpty' => true, 'value' => date('ymdHis') . str_pad(Yii::app()->user->id, 2, '0', STR_PAD_LEFT)),

Any help? I am getting really frustrated with this…