]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Использование timestamp behaviour
authorAlexander Smirnov <fredeom@mail.ru>
Tue, 30 Jul 2024 12:25:39 +0000 (15:25 +0300)
committerAlexander Smirnov <fredeom@mail.ru>
Tue, 30 Jul 2024 12:25:39 +0000 (15:25 +0300)
erp24/records/Motivation.php
erp24/services/MotivationService.php

index 321ca3ddcbab6aaca84b3f95422a1fb6828d4706..e843bd00fd99583eac94f3048066263252bcfed0 100644 (file)
@@ -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'],
         ];
     }
 
index d015143596100dbe2da02df49ee470e53cd9a81b..ca1aeed15b22287fb8bb50d68cacc65993f96416 100644 (file)
@@ -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;