]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Изменение миграций и моделей
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 27 Nov 2024 12:09:47 +0000 (15:09 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 27 Nov 2024 12:09:47 +0000 (15:09 +0300)
erp24/api2/controllers/DataController.php
erp24/controllers/ProductsController.php
erp24/migrations/m241102_124933_create_product_1c_prop_type_table.php [deleted file]
erp24/migrations/m241102_124933_create_products_1c_prop_type_table.php [new file with mode: 0644]
erp24/migrations/m241102_124956_create_products_1c_additional_characteristics_table.php
erp24/records/Product1cPropType.php [deleted file]
erp24/records/Products1cAdditionalCharacteristics.php
erp24/records/Products1cPropType.php [new file with mode: 0644]

index 9f2ee0a4d8a3a6e5fd02d19bc1d11b4c77d841c7..8bb0d9e9f948523836f14ba2ba03355c29d3125b 100644 (file)
@@ -28,7 +28,7 @@ use yii_app\records\PaymentTypes;
 use yii_app\records\Prices;
 use yii_app\records\PricesDynamic;
 use yii_app\records\PricesZakup;
-use yii_app\records\Product1cPropType;
+use yii_app\records\Products1CPropType;
 use yii_app\records\Products1c;
 use yii_app\records\Products1cAdditionalCharacteristics;
 use yii_app\records\Sales;
@@ -577,11 +577,37 @@ class DataController extends BaseController {
                     }
 
                     if (!empty($arr["AdditionCharacteristics"])) {
-                        foreach ($arr["AdditionCharacteristics"] as $characteristic) {
 
-                            $propertyType = Product1cPropType::findOne(['id' => $characteristic["id"]]);
+
+                            $passedCharacteristics = array_column($arr["AdditionCharacteristics"], null, "id");
+
+
+                            $currentCharacteristics = Products1cAdditionalCharacteristics::find()
+                                ->where(['product_id' => $arr["id"]])
+                                ->indexBy('property_id')
+                                ->all();
+
+
+                            foreach ($currentCharacteristics as $propertyId => $currentCharacteristic) {
+                                if (!isset($passedCharacteristics[$propertyId])) {
+
+                                    if (!$currentCharacteristic->delete()) {
+                                        LogService::apiErrorLog(json_encode([
+                                            "error_id" => 8.3,
+                                            "error" => $currentCharacteristic->getErrors()
+                                        ], JSON_UNESCAPED_UNICODE));
+                                    }
+                                }
+                            }
+
+
+
+
+                            foreach ($arr["AdditionCharacteristics"] as $characteristic) {
+
+                            $propertyType = Products1CPropType::findOne(['id' => $characteristic["id"]]);
                             if (!$propertyType) {
-                                $propertyType = new Product1cPropType();
+                                $propertyType = new Products1CPropType();
                                 $propertyType->id = $characteristic["id"];
                                 $propertyType->name = $characteristic["name"];
                                 if (!$propertyType->save()) {
index faf8832d815866cc9e611179289464d05c2c64b8..31c5c3e0d060e897fe73fbc7f88a27f239aaa9db 100755 (executable)
@@ -8,7 +8,7 @@ use yii\helpers\Html;
 use yii\web\Controller;
 use yii_app\records\ExportImportTable;
 use yii_app\records\Prices;
-use yii_app\records\Product1cPropType;
+use yii_app\records\Products1CPropType;
 use yii_app\records\Products1c;
 use yii_app\records\Products1cAdditionalCharacteristics;
 use yii_app\records\Products1cOptions;
@@ -219,7 +219,7 @@ class ProductsController extends Controller
             }
 
 
-            $properties = Product1cPropType::find()
+            $properties = Products1CPropType::find()
                 ->select(['id', 'name'])
                 ->all();
             $properties = ArrayHelper::map($properties, 'id', 'name');
diff --git a/erp24/migrations/m241102_124933_create_product_1c_prop_type_table.php b/erp24/migrations/m241102_124933_create_product_1c_prop_type_table.php
deleted file mode 100644 (file)
index e99444c..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-<?php
-
-use yii\db\Migration;
-
-/**
- * Handles the creation of table `{{%product_1c_prop_type}}`.
- */
-class m241102_124933_create_product_1c_prop_type_table extends Migration
-{
-
-    const TABLE_NAME = 'erp24.product_1c_prop_type';
-    /**
-     * {@inheritdoc}
-     */
-    public function safeUp()
-    {
-        $this->createTable(self::TABLE_NAME, [
-            'id' => $this->string()->notNull()->unique()->comment('GUID свойства'),
-            'name' => $this->string()->notNull()->comment('Имя свойства')
-        ]);
-
-        $this->addPrimaryKey('pk_product_1c_prop_type', self::TABLE_NAME, 'id');
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function safeDown()
-    {
-        $this->dropTable(self::TABLE_NAME);
-    }
-}
diff --git a/erp24/migrations/m241102_124933_create_products_1c_prop_type_table.php b/erp24/migrations/m241102_124933_create_products_1c_prop_type_table.php
new file mode 100644 (file)
index 0000000..b1083e6
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+
+use yii\db\Migration;
+
+/**
+ * Handles the creation of table `{{%products_1c_prop_type}}`.
+ */
+class m241102_124933_create_products_1c_prop_type_table extends Migration
+{
+
+    const TABLE_NAME = 'erp24.products_1c_prop_type';
+    /**
+     * {@inheritdoc}
+     */
+    public function safeUp()
+    {
+
+        $tableSchema = $this->db->getTableSchema(self::TABLE_NAME);
+
+        if (!isset($tableSchema)) {
+            $this->createTable(self::TABLE_NAME, [
+                'id' => $this->string()->notNull()->unique()->comment('GUID свойства'),
+                'name' => $this->string()->notNull()->comment('Имя свойства')
+            ]);
+
+            $this->addPrimaryKey('pk_products_1c_prop_type', self::TABLE_NAME, 'id');
+        }
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function safeDown()
+    {
+        $tableSchema = $this->db->getTableSchema(self::TABLE_NAME);
+        if (isset($tableSchema)) {
+            $this->dropTable(self::TABLE_NAME);
+        }
+    }
+}
index 2f707f2850907442e8d027a70ed3ca65344279fd..d6f6576849fa8d8182f5b7e0f326a236a80f85c6 100644 (file)
@@ -14,21 +14,30 @@ class m241102_124956_create_products_1c_additional_characteristics_table extends
      */
     public function safeUp()
     {
-        $this->createTable(self::TABLE_NAME, [
-            'id' => $this->primaryKey()->comment('Идентификатор инкрементируемый'),
-            'product_id' => $this->string()->notNull()->comment('GUID товара'),
-            'property_id' => $this->string()->notNull()->comment('ID из product_1c_prop_type'),
-            'value' => $this->string()->notNull()->comment('Название свойства, привязанного к продукту и относящегося к данному property_id')         ]);
-
-        $this->addForeignKey(
-            'fk_products_1c_additional_characteristics_property_id',
-            self::TABLE_NAME,
-            'property_id',
-            '{{%erp24.product_1c_prop_type}}',
-            'id',
-            'CASCADE',
-            'CASCADE'
-        );
+
+        $tableSchema = $this->db->getTableSchema(self::TABLE_NAME);
+
+        if (!isset($tableSchema)) {
+            $this->createTable(self::TABLE_NAME, [
+
+                'product_id' => $this->string()->notNull()->comment('GUID товара'),
+                'property_id' => $this->string()->notNull()->comment('ID из products_1c_prop_type'),
+                'value' => $this->string()->notNull()->comment('Название свойства, привязанного к продукту и относящегося к данному property_id')]);
+
+            $this->addPrimaryKey(
+                'pk_products_1c_additional_characteristics',
+                self::TABLE_NAME,
+                ['product_id', 'property_id']
+            );
+
+            $this->addForeignKey(
+                'fk_products_1c_additional_characteristics_property_id',
+                self::TABLE_NAME,
+                'property_id',
+                '{{%erp24.products_1c_prop_type}}',
+                'id',
+            );
+        }
     }
 
     /**
@@ -36,7 +45,11 @@ class m241102_124956_create_products_1c_additional_characteristics_table extends
      */
     public function safeDown()
     {
-        $this->dropForeignKey('fk_products_1c_additional_characteristics_property_id', self::TABLE_NAME);
-        $this->dropTable(self::TABLE_NAME);
+        $tableSchema = $this->db->getTableSchema(self::TABLE_NAME);
+        if (isset($tableSchema)) {
+            $this->dropPrimaryKey('pk_products_1c_additional_characteristics', self::TABLE_NAME);
+            $this->dropForeignKey('fk_products_1c_additional_characteristics_property_id', self::TABLE_NAME);
+            $this->dropTable(self::TABLE_NAME);
+        }
     }
 }
\ No newline at end of file
diff --git a/erp24/records/Product1cPropType.php b/erp24/records/Product1cPropType.php
deleted file mode 100644 (file)
index b80f36f..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-<?php
-
-namespace yii_app\records;
-
-use Yii;
-
-/**
- * This is the model class for table "product_1c_prop_type".
- *
- * @property string $id GUID свойства
- * @property string $name Имя свойства
- *
- * @property Products1cAdditionalCharacteristics[] $products1cAdditionalCharacteristics
- */
-class Product1cPropType extends \yii\db\ActiveRecord
-{
-    /**
-     * {@inheritdoc}
-     */
-    public static function tableName()
-    {
-        return 'product_1c_prop_type';
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function rules()
-    {
-        return [
-            [['id', 'name'], 'required'],
-            [['id', 'name'], 'string', 'max' => 255],
-            [['id'], 'unique'],
-        ];
-    }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function attributeLabels()
-    {
-        return [
-            'id' => 'ID',
-            'name' => 'Name',
-        ];
-    }
-
-    /**
-     * Gets query for [[Products1cAdditionalCharacteristics]].
-     *
-     * @return \yii\db\ActiveQuery
-     */
-    public function getProducts1cAdditionalCharacteristics()
-    {
-        return $this->hasMany(Products1cAdditionalCharacteristics::class, ['property_id' => 'id']);
-    }
-}
index f90b9a9f66ff89afd1bc61f1525abc7e444a910e..0cd8219f2dcf4586f5c195a58b083630c221a06a 100644 (file)
@@ -7,12 +7,12 @@ use Yii;
 /**
  * This is the model class for table "products_1c_additional_characteristics".
  *
- * @property int $id Идентификатор инкрементируемый
+
  * @property string $product_id GUID товара
  * @property string $property_id ID из product_1c_prop_type
  * @property string $value Название свойства, привязанного к продукту и относящегося к данному property_id
  *
- * @property Product1cPropType $property
+ * @property Products1CPropType $property
  */
 class Products1cAdditionalCharacteristics extends \yii\db\ActiveRecord
 {
@@ -24,6 +24,13 @@ class Products1cAdditionalCharacteristics extends \yii\db\ActiveRecord
         return 'products_1c_additional_characteristics';
     }
 
+
+    public static function primaryKey()
+    {
+        return ['product_id', 'property_id'];
+    }
+
+
     /**
      * {@inheritdoc}
      */
@@ -32,7 +39,7 @@ class Products1cAdditionalCharacteristics extends \yii\db\ActiveRecord
         return [
             [['product_id', 'property_id', 'value'], 'required'],
             [['product_id', 'property_id', 'value'], 'string', 'max' => 255],
-            [['property_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product1cPropType::class, 'targetAttribute' => ['property_id' => 'id']],
+            [['property_id'], 'exist', 'skipOnError' => true, 'targetClass' => Products1CPropType::class, 'targetAttribute' => ['property_id' => 'id']],
         ];
     }
 
@@ -42,7 +49,7 @@ class Products1cAdditionalCharacteristics extends \yii\db\ActiveRecord
     public function attributeLabels()
     {
         return [
-            'id' => 'ID',
+
             'product_id' => 'Product ID',
             'property_id' => 'Property ID',
             'value' => 'Value Name',
@@ -56,6 +63,6 @@ class Products1cAdditionalCharacteristics extends \yii\db\ActiveRecord
      */
     public function getProperty()
     {
-        return $this->hasOne(Product1cPropType::class, ['id' => 'property_id']);
+        return $this->hasOne(Products1CPropType::class, ['id' => 'property_id']);
     }
 }
diff --git a/erp24/records/Products1cPropType.php b/erp24/records/Products1cPropType.php
new file mode 100644 (file)
index 0000000..3f534c4
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+
+namespace yii_app\records;
+
+use Yii;
+
+/**
+ * This is the model class for table "products_1c_prop_type".
+ *
+ * @property string $id GUID свойства
+ * @property string $name Имя свойства
+ *
+ * @property Products1cAdditionalCharacteristics[] $products1cAdditionalCharacteristics
+ */
+class Products1CPropType extends \yii\db\ActiveRecord
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return 'products_1c_prop_type';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            [['id', 'name'], 'required'],
+            [['id', 'name'], 'string', 'max' => 255],
+            [['id'], 'unique'],
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => 'ID',
+            'name' => 'Name',
+        ];
+    }
+
+    /**
+     * Gets query for [[Products1cAdditionalCharacteristics]].
+     *
+     * @return \yii\db\ActiveQuery
+     */
+    public function getProducts1cAdditionalCharacteristics()
+    {
+        return $this->hasMany(Products1cAdditionalCharacteristics::class, ['property_id' => 'id']);
+    }
+}