remove product id from product category variants table

This commit is contained in:
Brandon Shipley 2025-09-21 17:51:54 -07:00
parent 4549f30930
commit 892a85d30a
Signed by: bmfs
GPG Key ID: 14E38571D8BB0DE4
3 changed files with 30 additions and 43 deletions

View File

@ -28,10 +28,6 @@ class CreateProductCategoryVariants extends AbstractMigration
'default' => null, 'default' => null,
'null' => true, 'null' => true,
]); ]);
$table->addColumn('product_id', 'uuid', [
'default' => null,
'null' => true,
]);
$table->addColumn('enabled', 'boolean', [ $table->addColumn('enabled', 'boolean', [
'default' => null, 'default' => null,
'null' => false, 'null' => false,
@ -42,18 +38,12 @@ class CreateProductCategoryVariants extends AbstractMigration
'name' => 'VARIANTS_BY_PRODUCT_CATEGORY_ID', 'name' => 'VARIANTS_BY_PRODUCT_CATEGORY_ID',
'unique' => false, 'unique' => false,
]); ]);
$table->addIndex([
'product_id',
], [
'name' => 'VARIANTS_BY_PRODUCT_ID',
'unique' => false,
]);
// $table->addIndex([ // $table->addIndex([
// 'name', // 'name',
// 'product_category_id', // '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, // 'unique' => true,
// ]); // ]);
$table->create(); $table->create();

View File

@ -12,11 +12,9 @@ use Cake\ORM\Entity;
* @property string $id * @property string $id
* @property string $name * @property string $name
* @property string|null $product_category_id * @property string|null $product_category_id
* @property string|null $product_id
* @property bool $enabled * @property bool $enabled
* *
* @property ProductCategory|EntityInterface $product_category * @property ProductCategory|EntityInterface $product_category
* @property Product|EntityInterface $product
* @property ProductCategoryVariantOption[]|EntityInterface[] $product_category_variant_options * @property ProductCategoryVariantOption[]|EntityInterface[] $product_category_variant_options
*/ */
class ProductCategoryVariant extends Entity class ProductCategoryVariant extends Entity
@ -33,12 +31,10 @@ class ProductCategoryVariant extends Entity
protected array $_accessible = [ protected array $_accessible = [
'name' => true, 'name' => true,
'product_category_id' => true, 'product_category_id' => true,
'product_id' => true,
'enabled' => true, 'enabled' => true,
// entities // entities
'product_category' => false, 'product_category' => false,
'product' => false,
'product_category_variant_options' => true, 'product_category_variant_options' => true,
]; ];
} }

View File

@ -4,30 +4,37 @@ declare(strict_types=1);
namespace CakeProducts\Model\Table; namespace CakeProducts\Model\Table;
use Cake\Core\Configure; 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\Query\SelectQuery;
use Cake\ORM\RulesChecker; use Cake\ORM\RulesChecker;
use Cake\ORM\Table; use Cake\ORM\Table;
use Cake\Validation\Validator; use Cake\Validation\Validator;
use CakeProducts\Model\Entity\ProductCategoryVariant;
use Closure;
use Psr\SimpleCache\CacheInterface;
/** /**
* ProductCategoryVariants Model * ProductCategoryVariants Model
* *
* @property \App\Model\Table\ProductCategoriesTable&\Cake\ORM\Association\BelongsTo $ProductCategories * @property ProductCategoriesTable&BelongsTo $ProductCategories
* @property \App\Model\Table\ProductsTable&\Cake\ORM\Association\BelongsTo $Products * @property ProductsTable&BelongsTo $Products
* *
* @method \App\Model\Entity\ProductCategoryVariant newEmptyEntity() * @method ProductCategoryVariant newEmptyEntity()
* @method \App\Model\Entity\ProductCategoryVariant newEntity(array $data, array $options = []) * @method ProductCategoryVariant newEntity(array $data, array $options = [])
* @method array<\App\Model\Entity\ProductCategoryVariant> newEntities(array $data, array $options = []) * @method array<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 ProductCategoryVariant get(mixed $primaryKey, array|string $finder = 'all', 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 ProductCategoryVariant findOrCreate($search, ?callable $callback = null, array $options = [])
* @method \App\Model\Entity\ProductCategoryVariant patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = []) * @method ProductCategoryVariant patchEntity(EntityInterface $entity, array $data, array $options = [])
* @method array<\App\Model\Entity\ProductCategoryVariant> patchEntities(iterable $entities, array $data, array $options = []) * @method array<ProductCategoryVariant> patchEntities(iterable $entities, array $data, array $options = [])
* @method \App\Model\Entity\ProductCategoryVariant|false save(\Cake\Datasource\EntityInterface $entity, array $options = []) * @method ProductCategoryVariant|false save(EntityInterface $entity, array $options = [])
* @method \App\Model\Entity\ProductCategoryVariant saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = []) * @method ProductCategoryVariant saveOrFail(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<ProductCategoryVariant>|ResultSetInterface<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<ProductCategoryVariant>|ResultSetInterface<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<ProductCategoryVariant>|ResultSetInterface<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 iterable<ProductCategoryVariant>|ResultSetInterface<ProductCategoryVariant> deleteManyOrFail(iterable $entities, array $options = [])
*/ */
class ProductCategoryVariantsTable extends Table class ProductCategoryVariantsTable extends Table
{ {
@ -54,11 +61,6 @@ class ProductCategoryVariantsTable extends Table
'bindingKey' => 'internal_id', 'bindingKey' => 'internal_id',
'className' => 'CakeProducts.ProductCategories', 'className' => 'CakeProducts.ProductCategories',
]); ]);
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'className' => 'CakeProducts.Products',
]);
$this->hasMany('ProductCategoryVariantOptions', [ $this->hasMany('ProductCategoryVariantOptions', [
'foreignKey' => 'product_category_variant_id', 'foreignKey' => 'product_category_variant_id',
'className' => 'CakeProducts.ProductCategoryVariantOptions', 'className' => 'CakeProducts.ProductCategoryVariantOptions',
@ -70,8 +72,8 @@ class ProductCategoryVariantsTable extends Table
/** /**
* Default validation rules. * Default validation rules.
* *
* @param \Cake\Validation\Validator $validator Validator instance. * @param Validator $validator Validator instance.
* @return \Cake\Validation\Validator * @return Validator
*/ */
public function validationDefault(Validator $validator): 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 * Returns a rules checker object that will be used for validating
* application integrity. * application integrity.
* *
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified. * @param RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker * @return RulesChecker
*/ */
public function buildRules(RulesChecker $rules): 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_category_id'], 'ProductCategories'), ['errorField' => 'product_category_id']);
$rules->add($rules->existsIn(['product_id'], 'Products'), ['errorField' => 'product_id']);
return $rules; return $rules;
} }
@ -117,7 +118,7 @@ class ProductCategoryVariantsTable extends Table
* @param SelectQuery $query * @param SelectQuery $query
* @param string $internalCategoryId * @param string $internalCategoryId
* *
* @return array|\Cake\ORM\Query|SelectQuery * @return array|Query|SelectQuery
*/ */
public function findAllCategoryVariantsForCategoryId(SelectQuery $query, string $internalCategoryId) public function findAllCategoryVariantsForCategoryId(SelectQuery $query, string $internalCategoryId)
{ {