From: fomichev Date: Wed, 27 Nov 2024 12:09:47 +0000 (+0300) Subject: Изменение миграций и моделей X-Git-Tag: 1.7~212^2~8 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=b4a70292d753f8826d6d5ad9300798badcd74852;p=erp24_rep%2Fyii-erp24%2F.git Изменение миграций и моделей --- diff --git a/erp24/api2/controllers/DataController.php b/erp24/api2/controllers/DataController.php index 9f2ee0a4..8bb0d9e9 100644 --- a/erp24/api2/controllers/DataController.php +++ b/erp24/api2/controllers/DataController.php @@ -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()) { diff --git a/erp24/controllers/ProductsController.php b/erp24/controllers/ProductsController.php index faf8832d..31c5c3e0 100755 --- a/erp24/controllers/ProductsController.php +++ b/erp24/controllers/ProductsController.php @@ -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 index e99444c4..00000000 --- a/erp24/migrations/m241102_124933_create_product_1c_prop_type_table.php +++ /dev/null @@ -1,32 +0,0 @@ -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 index 00000000..b1083e63 --- /dev/null +++ b/erp24/migrations/m241102_124933_create_products_1c_prop_type_table.php @@ -0,0 +1,40 @@ +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); + } + } +} diff --git a/erp24/migrations/m241102_124956_create_products_1c_additional_characteristics_table.php b/erp24/migrations/m241102_124956_create_products_1c_additional_characteristics_table.php index 2f707f28..d6f65768 100644 --- a/erp24/migrations/m241102_124956_create_products_1c_additional_characteristics_table.php +++ b/erp24/migrations/m241102_124956_create_products_1c_additional_characteristics_table.php @@ -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 index b80f36f3..00000000 --- a/erp24/records/Product1cPropType.php +++ /dev/null @@ -1,57 +0,0 @@ - 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']); - } -} diff --git a/erp24/records/Products1cAdditionalCharacteristics.php b/erp24/records/Products1cAdditionalCharacteristics.php index f90b9a9f..0cd8219f 100644 --- a/erp24/records/Products1cAdditionalCharacteristics.php +++ b/erp24/records/Products1cAdditionalCharacteristics.php @@ -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 index 00000000..3f534c4b --- /dev/null +++ b/erp24/records/Products1cPropType.php @@ -0,0 +1,57 @@ + 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']); + } +}