product variants table split off from category variants table, variant values table updated to reflect

This commit is contained in:
2025-10-03 02:16:24 -07:00
parent d49a8784e6
commit 49912abd3a
21 changed files with 477 additions and 46 deletions

View File

@@ -10,7 +10,7 @@ use Cake\ORM\Entity;
*
* @property string $id
* @property string $product_sku_id
* @property string $product_category_variant_id
* @property string $product_variant_id
* @property string $product_category_variant_option_id
*
* @property ProductSku $product_sku
@@ -30,12 +30,12 @@ class ProductSkuVariantValue extends Entity
*/
protected array $_accessible = [
'product_sku_id' => true,
'product_category_variant_id' => true,
'product_variant_id' => true,
'product_category_variant_option_id' => true,
// entities
'product_skus' => true,
'product_category_variant' => true,
'product_variant' => true,
'product_category_variant_option' => true,
];
}

View File

@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Model\Entity;
use Cake\ORM\Entity;
/**
* ProductVariant Entity
*
* @property string $id
* @property string $name
* @property string|null $product_category_variant_id
* @property string $product_id
* @property bool $enabled
*
* @property \App\Model\Entity\ProductCategoryVariant $product_category_variant
* @property \App\Model\Entity\Product $product
*/
class ProductVariant extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* @var array<string, bool>
*/
protected array $_accessible = [
'name' => true,
'product_category_variant_id' => true,
'product_id' => true,
'enabled' => true,
'product_category_variant' => true,
'product' => true,
];
}

View File

@@ -72,9 +72,9 @@ class ProductCategoryVariantsTable extends Table
'cascadeCallbacks' => true,
]);
$this->hasMany('ProductSkuVariantValues', [
$this->hasMany('ProductVariants', [
'foreignKey' => 'product_category_variant_id',
'className' => 'CakeProducts.ProductSkuVariantValues',
'className' => 'CakeProducts.ProductVariants',
'dependent' => true,
'cascadeCallbacks' => true,
]);

View File

@@ -56,9 +56,9 @@ class ProductSkuVariantValuesTable extends Table
'foreignKey' => 'product_sku_id',
'joinType' => 'INNER',
]);
$this->belongsTo('ProductCategoryVariants', [
'className' => 'CakeProducts.ProductCategoryVariants',
'foreignKey' => 'product_category_variant_id',
$this->belongsTo('ProductVariants', [
'className' => 'CakeProducts.ProductVariants',
'foreignKey' => 'product_variant_id',
'joinType' => 'INNER',
]);
$this->belongsTo('ProductCategoryVariantOptions', [
@@ -81,8 +81,8 @@ class ProductSkuVariantValuesTable extends Table
->notEmptyString('product_sku_id');
$validator
->uuid('product_category_variant_id')
->notEmptyString('product_category_variant_id');
->uuid('product_variant_id')
->notEmptyString('product_variant_id');
$validator
->uuid('product_category_variant_option_id')
@@ -103,8 +103,8 @@ class ProductSkuVariantValuesTable extends Table
$rules->add($rules->existsIn(['product_sku_id'], 'ProductSkus'), ['errorField' => 'product_sku_id']);
// @TODO why not working?? causing tests to fail / associated variant values not saving on product-skus/add
// $rules->add($rules->existsIn(['product_category_variant_id'], 'ProductCategoryVariants'), ['errorField' => 'product_category_variant_id']);
// $rules->add($rules->existsIn(['product_category_variant_option_id'], 'ProductCategoryVariantOptions'), ['errorField' => 'product_category_variant_option_id']);
$rules->add($rules->existsIn(['product_variant_id'], 'ProductVariants'), ['errorField' => 'product_variant_id']);
$rules->add($rules->existsIn(['product_category_variant_option_id'], 'ProductCategoryVariantOptions'), ['errorField' => 'product_category_variant_option_id']);
return $rules;
}

View File

@@ -73,6 +73,20 @@ class ProductSkusTable extends Table
'dependent' => true,
'cascadeCallbacks' => true,
]);
$this->hasMany('ProductPhotos', [
'foreignKey' => 'product_sku_id',
'className' => 'CakeProducts.ProductPhotos',
'dependent' => true,
'cascadeCallbacks' => true,
]);
$this->hasOne('PrimaryProductPhotos', [
'foreignKey' => 'product_sku_id',
'conditions' => ['PrimaryProductPhotos.primary_photo' => true],
'className' => 'CakeProducts.ProductPhotos',
'dependent' => true,
]);
}
/**

View File

@@ -0,0 +1,115 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Model\Table;
use CakeProducts\Model\Entity\ProductVariant;
use CakeProducts\Model\Table\ProductCategoryVariantsTable;
use CakeProducts\Model\Table\ProductsTable;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\ResultSetInterface;
use Cake\ORM\Association\BelongsTo;
use Cake\ORM\Query\SelectQuery;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Closure;
use Psr\SimpleCache\CacheInterface;
/**
* ProductVariants Model
*
* @property ProductCategoryVariantsTable&BelongsTo $ProductCategoryVariants
* @property ProductsTable&BelongsTo $Products
*
* @method ProductVariant newEmptyEntity()
* @method ProductVariant newEntity(array $data, array $options = [])
* @method array<ProductVariant> newEntities(array $data, array $options = [])
* @method ProductVariant get(mixed $primaryKey, array|string $finder = 'all', CacheInterface|string|null $cache = null, Closure|string|null $cacheKey = null, mixed ...$args)
* @method ProductVariant findOrCreate($search, ?callable $callback = null, array $options = [])
* @method ProductVariant patchEntity(EntityInterface $entity, array $data, array $options = [])
* @method array<ProductVariant> patchEntities(iterable $entities, array $data, array $options = [])
* @method ProductVariant|false save(EntityInterface $entity, array $options = [])
* @method ProductVariant saveOrFail(EntityInterface $entity, array $options = [])
* @method iterable<ProductVariant>|ResultSetInterface<ProductVariant>|false saveMany(iterable $entities, array $options = [])
* @method iterable<ProductVariant>|ResultSetInterface<ProductVariant> saveManyOrFail(iterable $entities, array $options = [])
* @method iterable<ProductVariant>|ResultSetInterface<ProductVariant>|false deleteMany(iterable $entities, array $options = [])
* @method iterable<ProductVariant>|ResultSetInterface<ProductVariant> deleteManyOrFail(iterable $entities, array $options = [])
*/
class ProductVariantsTable extends Table
{
/**
* Initialize method
*
* @param array<string, mixed> $config The configuration for the Table.
* @return void
*/
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('product_variants');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
$this->belongsTo('ProductCategoryVariants', [
'className' => 'CakeProducts.ProductCategoryVariants',
'foreignKey' => 'product_category_variant_id',
]);
$this->belongsTo('Products', [
'className' => 'CakeProducts.Products',
'foreignKey' => 'product_id',
'joinType' => 'INNER',
]);
$this->hasMany('ProductCategoryVariantOptions', [
'className' => 'CakeProducts.Products',
'foreignKey' => 'product_category_variant_id',
]);
}
/**
* Default validation rules.
*
* @param Validator $validator Validator instance.
* @return Validator
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->scalar('name')
->maxLength('name', 255)
->requirePresence('name', 'create')
->notEmptyString('name');
$validator
->uuid('product_category_variant_id')
->allowEmptyString('product_category_variant_id');
$validator
->uuid('product_id')
->notEmptyString('product_id');
$validator
->boolean('enabled')
->requirePresence('enabled', 'create')
->notEmptyString('enabled');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param RulesChecker $rules The rules object to be modified.
* @return RulesChecker
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->existsIn(['product_category_variant_id'], 'ProductCategoryVariants'), ['errorField' => 'product_category_variant_id']);
$rules->add($rules->existsIn(['product_id'], 'Products'), ['errorField' => 'product_id']);
return $rules;
}
}

View File

@@ -69,9 +69,9 @@ class ProductsTable extends Table
'cascadeCallbacks' => true,
]);
$this->hasMany('ProductCategoryVariants', [
$this->hasMany('ProductVariants', [
'foreignKey' => 'product_id',
'className' => 'CakeProducts.ProductCategoryVariants',
'className' => 'CakeProducts.ProductVariants',
'dependent' => true,
'cascadeCallbacks' => true,
]);