From 17f0c6bbfb16fa54d155dd49d16a977325ce1d54 Mon Sep 17 00:00:00 2001 From: Alexander Smirnov Date: Tue, 30 Jul 2024 15:25:39 +0300 Subject: [PATCH] =?utf8?q?=D0=98=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE?= =?utf8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20timestamp=20behaviour?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/records/Motivation.php | 17 +++++++++++++++-- erp24/services/MotivationService.php | 6 +----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/erp24/records/Motivation.php b/erp24/records/Motivation.php index 321ca3dd..e843bd00 100644 --- a/erp24/records/Motivation.php +++ b/erp24/records/Motivation.php @@ -3,6 +3,8 @@ namespace yii_app\records; use Yii; +use yii\behaviors\TimestampBehavior; +use yii\db\Expression; /** * This is the model class for table "motivation". @@ -24,16 +26,27 @@ class Motivation extends \yii\db\ActiveRecord return 'motivation'; } + public function behaviors() + { + return [ + [ + 'class' => TimestampBehavior::class, + 'createdAtAttribute' => 'created_at', + 'updatedAtAttribute' => 'updated_at', + 'value' => new Expression('NOW()'), + ] + ]; + } + /** * {@inheritdoc} */ public function rules() { return [ - [['store_id', 'year', 'month', 'updated_at', 'created_at'], 'required'], + [['store_id', 'year', 'month'], 'required'], [['store_id', 'year', 'month'], 'default', 'value' => null], [['store_id', 'year', 'month'], 'integer'], - [['updated_at', 'created_at'], 'safe'], ]; } diff --git a/erp24/services/MotivationService.php b/erp24/services/MotivationService.php index d0151435..ca1aeed1 100644 --- a/erp24/services/MotivationService.php +++ b/erp24/services/MotivationService.php @@ -65,15 +65,11 @@ class MotivationService $motivation = Motivation::find()->where(['store_id' => $store->id, 'year' => $year, 'month' => $month])->one(); /** @var $motivation Motivation */ - if ($motivation) { - $motivation->updated_at = date('Y-m-d H:i:s'); - } else { + if (!$motivation) { $motivation = new Motivation; $motivation->store_id = $store->id; $motivation->year = $year; $motivation->month = $month; - $motivation->created_at = date('Y-m-d H:i:s'); - $motivation->updated_at = date('Y-m-d H:i:s'); } $storeStr = false; -- 2.39.5