From 892a85d30ae2fefd5d1192cb1e7c2ab9828f4ddb Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Sun, 21 Sep 2025 17:51:54 -0700 Subject: [PATCH] remove product id from product category variants table --- ...29054627_CreateProductCategoryVariants.php | 14 +---- src/Model/Entity/ProductCategoryVariant.php | 4 -- .../Table/ProductCategoryVariantsTable.php | 55 ++++++++++--------- 3 files changed, 30 insertions(+), 43 deletions(-) diff --git a/config/Migrations/20250629054627_CreateProductCategoryVariants.php b/config/Migrations/20250629054627_CreateProductCategoryVariants.php index 8e35324..172fd3b 100644 --- a/config/Migrations/20250629054627_CreateProductCategoryVariants.php +++ b/config/Migrations/20250629054627_CreateProductCategoryVariants.php @@ -28,10 +28,6 @@ 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, @@ -42,18 +38,12 @@ 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_AND_PRODUCT_ID_UNIQUE', +// 'name' => 'VARIANTS_BY_NAME_AND_PRODUCT_CATEGORY_ID_UNIQUE', // 'unique' => true, // ]); $table->create(); diff --git a/src/Model/Entity/ProductCategoryVariant.php b/src/Model/Entity/ProductCategoryVariant.php index 9270ebf..b756ee3 100644 --- a/src/Model/Entity/ProductCategoryVariant.php +++ b/src/Model/Entity/ProductCategoryVariant.php @@ -12,11 +12,9 @@ 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 - * @property Product|EntityInterface $product * @property ProductCategoryVariantOption[]|EntityInterface[] $product_category_variant_options */ class ProductCategoryVariant extends Entity @@ -33,12 +31,10 @@ 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 8fef4ac..2dd0b20 100644 --- a/src/Model/Table/ProductCategoryVariantsTable.php +++ b/src/Model/Table/ProductCategoryVariantsTable.php @@ -4,30 +4,37 @@ declare(strict_types=1); namespace CakeProducts\Model\Table; use Cake\Core\Configure; +use Cake\Datasource\EntityInterface; +use Cake\Datasource\ResultSetInterface; +use Cake\ORM\Association\BelongsTo; +use Cake\ORM\Query; use Cake\ORM\Query\SelectQuery; use Cake\ORM\RulesChecker; use Cake\ORM\Table; use Cake\Validation\Validator; +use CakeProducts\Model\Entity\ProductCategoryVariant; +use Closure; +use Psr\SimpleCache\CacheInterface; /** * ProductCategoryVariants Model * - * @property \App\Model\Table\ProductCategoriesTable&\Cake\ORM\Association\BelongsTo $ProductCategories - * @property \App\Model\Table\ProductsTable&\Cake\ORM\Association\BelongsTo $Products + * @property ProductCategoriesTable&BelongsTo $ProductCategories + * @property ProductsTable&BelongsTo $Products * - * @method \App\Model\Entity\ProductCategoryVariant newEmptyEntity() - * @method \App\Model\Entity\ProductCategoryVariant newEntity(array $data, array $options = []) - * @method array<\App\Model\Entity\ProductCategoryVariant> newEntities(array $data, array $options = []) - * @method \App\Model\Entity\ProductCategoryVariant get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args) - * @method \App\Model\Entity\ProductCategoryVariant findOrCreate($search, ?callable $callback = null, array $options = []) - * @method \App\Model\Entity\ProductCategoryVariant patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) - * @method array<\App\Model\Entity\ProductCategoryVariant> patchEntities(iterable $entities, array $data, array $options = []) - * @method \App\Model\Entity\ProductCategoryVariant|false save(\Cake\Datasource\EntityInterface $entity, array $options = []) - * @method \App\Model\Entity\ProductCategoryVariant saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = []) - * @method iterable<\App\Model\Entity\ProductCategoryVariant>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\ProductCategoryVariant>|false saveMany(iterable $entities, array $options = []) - * @method iterable<\App\Model\Entity\ProductCategoryVariant>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\ProductCategoryVariant> saveManyOrFail(iterable $entities, array $options = []) - * @method iterable<\App\Model\Entity\ProductCategoryVariant>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\ProductCategoryVariant>|false deleteMany(iterable $entities, array $options = []) - * @method iterable<\App\Model\Entity\ProductCategoryVariant>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\ProductCategoryVariant> deleteManyOrFail(iterable $entities, array $options = []) + * @method ProductCategoryVariant newEmptyEntity() + * @method ProductCategoryVariant newEntity(array $data, array $options = []) + * @method array newEntities(array $data, array $options = []) + * @method ProductCategoryVariant get(mixed $primaryKey, array|string $finder = 'all', CacheInterface|string|null $cache = null, Closure|string|null $cacheKey = null, mixed ...$args) + * @method ProductCategoryVariant findOrCreate($search, ?callable $callback = null, array $options = []) + * @method ProductCategoryVariant patchEntity(EntityInterface $entity, array $data, array $options = []) + * @method array patchEntities(iterable $entities, array $data, array $options = []) + * @method ProductCategoryVariant|false save(EntityInterface $entity, array $options = []) + * @method ProductCategoryVariant saveOrFail(EntityInterface $entity, array $options = []) + * @method iterable|ResultSetInterface|false saveMany(iterable $entities, array $options = []) + * @method iterable|ResultSetInterface saveManyOrFail(iterable $entities, array $options = []) + * @method iterable|ResultSetInterface|false deleteMany(iterable $entities, array $options = []) + * @method iterable|ResultSetInterface deleteManyOrFail(iterable $entities, array $options = []) */ class ProductCategoryVariantsTable extends Table { @@ -54,11 +61,6 @@ 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', @@ -70,8 +72,8 @@ class ProductCategoryVariantsTable extends Table /** * Default validation rules. * - * @param \Cake\Validation\Validator $validator Validator instance. - * @return \Cake\Validation\Validator + * @param Validator $validator Validator instance. + * @return Validator */ public function validationDefault(Validator $validator): Validator { @@ -101,14 +103,13 @@ class ProductCategoryVariantsTable extends Table * Returns a rules checker object that will be used for validating * application integrity. * - * @param \Cake\ORM\RulesChecker $rules The rules object to be modified. - * @return \Cake\ORM\RulesChecker + * @param RulesChecker $rules The rules object to be modified. + * @return RulesChecker */ public function buildRules(RulesChecker $rules): RulesChecker { -// $rules->add($rules->isUnique(['name', 'product_category_id', 'product_id'], ['allowMultipleNulls' => true]), ['errorField' => 'product_category_id']); + $rules->add($rules->isUnique(['name', 'product_category_id'], ['allowMultipleNulls' => true]), ['errorField' => 'product_category_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; } @@ -117,7 +118,7 @@ class ProductCategoryVariantsTable extends Table * @param SelectQuery $query * @param string $internalCategoryId * - * @return array|\Cake\ORM\Query|SelectQuery + * @return array|Query|SelectQuery */ public function findAllCategoryVariantsForCategoryId(SelectQuery $query, string $internalCategoryId) {