From 8e73da4f4ad91b7bca97bc302547d024bcce5eca Mon Sep 17 00:00:00 2001 From: vova Date: Tue, 12 Apr 2022 17:53:16 +0300 Subject: [PATCH] product models and migrations --- common/messages/ru/app.php | 2 +- common/models/Product.php | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/models/ProductLang.php | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ console/migrations/m220412_144550_create_product_table.php | 43 +++++++++++++++++++++++++++++++++++++++++++ console/migrations/m220412_144602_create_product_lang_table.php | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 304 insertions(+), 1 deletion(-) create mode 100644 common/models/Product.php create mode 100644 common/models/ProductLang.php create mode 100644 console/migrations/m220412_144550_create_product_table.php create mode 100644 console/migrations/m220412_144602_create_product_lang_table.php diff --git a/common/messages/ru/app.php b/common/messages/ru/app.php index 23cefc7..23eff86 100755 --- a/common/messages/ru/app.php +++ b/common/messages/ru/app.php @@ -390,7 +390,7 @@ return [ 'section-question__title'=> 'ЧАСТО ЗАДАВАЕМЫЕ ВОПРОСЫ', 'Номінальна потужність мережевого інвертора'=>'Номінальна потужність мережевого інвертора', 'Встановлена потужність фотоелектричних модулів'=>'Встановлена потужність фотоелектричних модулів', - 'Вартість системи "під ключ"'=>'Вартість системи "під ключ"', + 'Вартість системи "під ключ"'=>'Стоимость системы "под ключ"', diff --git a/common/models/Product.php b/common/models/Product.php new file mode 100644 index 0000000..3508a3e --- /dev/null +++ b/common/models/Product.php @@ -0,0 +1,74 @@ + [ + 'class' => LanguageBehavior::className(), + ], + ]; + } + + public function rules() + { + return [ +// [ +// [ +// 'image', +// ], +// 'required', +// ], + [ + [ + 'status', + ], + 'boolean', + ], + [ + [ + 'sku', + ], + 'string', + 'max' => 255, + ], + [ + [ + 'sort', + 'image_id', + ], + 'integer', + ], + [ + ['image_id'], + 'exist', + 'skipOnError' => true, + 'targetClass' => Image::className(), + 'targetAttribute' => ['image_id' => 'id'], + ], + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getImage() + { + return $this->hasOne(Image::className(), ['id' => 'image_id']); + } +} \ No newline at end of file diff --git a/common/models/ProductLang.php b/common/models/ProductLang.php new file mode 100644 index 0000000..c198987 --- /dev/null +++ b/common/models/ProductLang.php @@ -0,0 +1,85 @@ + 255, + ], + [ + [ + 'description', + ], + 'string', + ], + [ + [ 'language_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => Language::className(), + 'targetAttribute' => [ 'language_id' => 'id' ], + ], + [ + [ 'tile_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => Product::className(), + 'targetAttribute' => [ 'product_id' => 'id' ], + ], + ]; + } + + + /** + * @return \yii\db\ActiveQuery + */ + public function getLanguage() + { + return $this->hasOne(Language::className(), [ 'id' => 'language_id' ]); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getProduct() + { + return $this->hasOne(Product::className(), [ 'id' => 'product_id' ]); + } +} \ No newline at end of file diff --git a/console/migrations/m220412_144550_create_product_table.php b/console/migrations/m220412_144550_create_product_table.php new file mode 100644 index 0000000..565da45 --- /dev/null +++ b/console/migrations/m220412_144550_create_product_table.php @@ -0,0 +1,43 @@ +createTable('product', [ + 'id' => $this->primaryKey(), + 'status' => $this->boolean(), + 'sort' => $this->smallInteger(), + 'sku' =>$this->string(255), + 'image_id' => $this->integer(), + ]); + $this->addForeignKey( + 'fk-product-image_id', + 'product', + 'image_id', + 'ImageManager', + 'id', + 'CASCADE' + ); + } + + /** + * @inheritdoc + */ + public function down() + { + $this->dropForeignKey( + 'fk-product-image_id', + 'product' + ); + $this->dropTable('product'); + } +} diff --git a/console/migrations/m220412_144602_create_product_lang_table.php b/console/migrations/m220412_144602_create_product_lang_table.php new file mode 100644 index 0000000..f682ebd --- /dev/null +++ b/console/migrations/m220412_144602_create_product_lang_table.php @@ -0,0 +1,101 @@ +createTable('product_lang', [ + 'product_id' => $this->integer()->notNull(), + 'language_id' => $this->smallInteger()->notNull(), + 'description' => $this->text(), + 'title' => $this->string(255), + ]); + // creates index for column `product_id` + $this->createIndex( + 'idx-product_lang-product_id', + 'product_lang', + 'product_id' + ); + + // add foreign key for table `object` + $this->addForeignKey( + 'fk-product_lang-product_id', + 'product_lang', + 'product_id', + 'product', + 'id', + 'CASCADE' + ); + + // creates index for column `language_id` + $this->createIndex( + 'idx-product_lang-language_id', + 'product_lang', + 'language_id' + ); + + // add foreign key for table `language` + $this->addForeignKey( + 'fk-product_lang-language_id', + 'product_lang', + 'language_id', + 'language', + 'id', + 'CASCADE' + ); + // add double primary key + $this->addPrimaryKey( + "pk-product_lang-product_id-language_id", + "product_lang", + [ + "product_id", + "language_id", + ] + ); + } + + /** + * @inheritdoc + */ + public function down() + { + // drop double primary key + $this->dropPrimaryKey( + "pk-product_lang-product_id-language_id", + "product_lang" + ); + + // drops foreign key for table `product` + $this->dropForeignKey( + 'fk-product_lang-product_id', + 'product_lang' + ); + + // drops index for column `product_id` + $this->dropIndex( + 'idx-product_lang-product_id', + 'product_lang' + ); + + // drops foreign key for table `language` + $this->dropForeignKey( + 'fk-product_lang-language_id', + 'product_lang' + ); + + // drops index for column `language_id` + $this->dropIndex( + 'idx-product_lang-language_id', + 'product_lang' + ); + $this->dropTable('product_lang'); + } +} -- libgit2 0.21.4