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