From: Alexander Smirnov Date: Tue, 30 Jul 2024 12:25:39 +0000 (+0300) Subject: Использование timestamp behaviour X-Git-Tag: 1.4~45^2~4 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=17f0c6bbfb16fa54d155dd49d16a977325ce1d54;p=erp24_rep%2Fyii-erp24%2F.git Использование timestamp behaviour --- 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;