Yii Framework Forum: Updates both model - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Updates both model Rate Topic: -----

#1 User is offline   carn4ge 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 04-January 17

Posted 06 September 2017 - 01:03 PM

When I create something in forms it automatically gets the default date and time from the system and put it on the time_start, I'm trying to do when I update the details it gets the current date and time and save it on time_end but it updates both time_start and time_end

public function actionCreate()
    {
        $model = new Ticket();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            $model->time_start = date('d-m-y h:i:s');
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }


The update code gets the time_end

 public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
                $model->time_end = date('y-m-d h:i:s');            
                return $this->render('update', [
                'model' => $model,
            ]); 

        }
    }

0

#2 User is offline   zabachok 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 03-November 15

Posted 13 September 2017 - 06:28 AM

Use Timestamp behavior in your model: http://www.yiiframew...mpbehavior.html
0

#3 User is offline   shiv 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 95
  • Joined: 05-November 11
  • Location:India

Posted 14 September 2017 - 02:15 AM

I think you should use beforeSave function in model.

You can check $insert to find if new record or old

public function beforeSave($insert){ if (!parent::beforeSave($insert)) { return false; }
// ...custom code here... return true;}
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users