From 5b38de39fca80f93f19e817f307304d129c2db1a Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Mon, 22 Sep 2025 21:51:52 -0700 Subject: [PATCH] product id back on product category variants table...dumb --- ...0250629054627_CreateProductCategoryVariants.php | 14 ++++++++++++-- src/Model/Entity/ProductCategoryVariant.php | 3 +++ src/Model/Table/ProductCategoryVariantsTable.php | 6 ++++++ .../Controller/ProductSkusControllerTest.php | 2 +- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/config/Migrations/20250629054627_CreateProductCategoryVariants.php b/config/Migrations/20250629054627_CreateProductCategoryVariants.php index 172fd3b..8e35324 100644 --- a/config/Migrations/20250629054627_CreateProductCategoryVariants.php +++ b/config/Migrations/20250629054627_CreateProductCategoryVariants.php @@ -28,6 +28,10 @@ class CreateProductCategoryVariants extends AbstractMigration 'default' => null, 'null' => true, ]); + $table->addColumn('product_id', 'uuid', [ + 'default' => null, + 'null' => true, + ]); $table->addColumn('enabled', 'boolean', [ 'default' => null, 'null' => false, @@ -38,12 +42,18 @@ class CreateProductCategoryVariants extends AbstractMigration 'name' => 'VARIANTS_BY_PRODUCT_CATEGORY_ID', 'unique' => false, ]); - + $table->addIndex([ + 'product_id', + ], [ + 'name' => 'VARIANTS_BY_PRODUCT_ID', + 'unique' => false, + ]); // $table->addIndex([ // 'name', // 'product_category_id', +// 'product_id', // ], [ -// 'name' => 'VARIANTS_BY_NAME_AND_PRODUCT_CATEGORY_ID_UNIQUE', +// 'name' => 'VARIANTS_BY_NAME_AND_PRODUCT_CATEGORY_ID_AND_PRODUCT_ID_UNIQUE', // 'unique' => true, // ]); $table->create(); diff --git a/src/Model/Entity/ProductCategoryVariant.php b/src/Model/Entity/ProductCategoryVariant.php index b756ee3..d56fa7b 100644 --- a/src/Model/Entity/ProductCategoryVariant.php +++ b/src/Model/Entity/ProductCategoryVariant.php @@ -12,6 +12,7 @@ use Cake\ORM\Entity; * @property string $id * @property string $name * @property string|null $product_category_id + * @property string|null $product_id * @property bool $enabled * * @property ProductCategory|EntityInterface $product_category @@ -31,10 +32,12 @@ class ProductCategoryVariant extends Entity protected array $_accessible = [ 'name' => true, 'product_category_id' => true, + 'product_id' => true, 'enabled' => true, // entities 'product_category' => false, + 'product' => false, 'product_category_variant_options' => true, ]; } diff --git a/src/Model/Table/ProductCategoryVariantsTable.php b/src/Model/Table/ProductCategoryVariantsTable.php index 2dd0b20..b9767a5 100644 --- a/src/Model/Table/ProductCategoryVariantsTable.php +++ b/src/Model/Table/ProductCategoryVariantsTable.php @@ -61,6 +61,10 @@ class ProductCategoryVariantsTable extends Table 'bindingKey' => 'internal_id', 'className' => 'CakeProducts.ProductCategories', ]); + $this->belongsTo('Products', [ + 'foreignKey' => 'product_id', + 'className' => 'CakeProducts.Products', + ]); $this->hasMany('ProductCategoryVariantOptions', [ 'foreignKey' => 'product_category_variant_id', 'className' => 'CakeProducts.ProductCategoryVariantOptions', @@ -109,7 +113,9 @@ class ProductCategoryVariantsTable extends Table public function buildRules(RulesChecker $rules): RulesChecker { $rules->add($rules->isUnique(['name', 'product_category_id'], ['allowMultipleNulls' => true]), ['errorField' => 'product_category_id']); + $rules->add($rules->isUnique(['name', 'product_id'], ['allowMultipleNulls' => true]), ['errorField' => 'product_id']); $rules->add($rules->existsIn(['product_category_id'], 'ProductCategories'), ['errorField' => 'product_category_id']); + $rules->add($rules->existsIn(['product_id'], 'Products'), ['errorField' => 'product_id']); return $rules; } diff --git a/tests/TestCase/Controller/ProductSkusControllerTest.php b/tests/TestCase/Controller/ProductSkusControllerTest.php index 392fdd0..616cfcf 100644 --- a/tests/TestCase/Controller/ProductSkusControllerTest.php +++ b/tests/TestCase/Controller/ProductSkusControllerTest.php @@ -180,7 +180,7 @@ class ProductSkusControllerTest extends BaseControllerTest 'cost' => 1.5, 'product_sku_variant_values' => [ 0 => [ - 'product_category_variant_id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d94', + 'product_category_variant_id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d93', 'product_category_variant_option_id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d26', ], ],