variant options wip

This commit is contained in:
2025-07-05 20:08:56 -07:00
parent c9d34f7115
commit 5adc791c20
16 changed files with 462 additions and 1 deletions

View File

@@ -91,6 +91,14 @@ class ProductCategoriesTable extends Table
'dependent' => true,
'cascadeCallbacks' => true,
]);
$this->hasMany('ProductCategoryVariants', [
'foreignKey' => 'product_category_id',
'bindingKey' => 'internal_id',
'className' => 'CakeProducts.ProductCategoryVariants',
'dependent' => true,
'cascadeCallbacks' => true,
]);
$this->behaviors()->Tree->setConfig('scope', ['product_catalog_id' => $this->treeCatalogId]);
$this->addBehavior('Muffin/Trash.Trash');
}

View File

@@ -0,0 +1,107 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Model\Table;
use CakeProducts\Model\Entity\ProductCategoryVariantOption;
use CakeProducts\Model\Table\ProductCategoryVariantsTable;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\ResultSetInterface;
use Cake\ORM\Association\BelongsTo;
use Cake\ORM\Behavior\TimestampBehavior;
use Cake\ORM\Query\SelectQuery;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Closure;
use Psr\SimpleCache\CacheInterface;
/**
* ProductCategoryVariantOptions Model
*
* @property ProductCategoryVariantsTable&BelongsTo $ProductCategoryVariants
*
* @method ProductCategoryVariantOption newEmptyEntity()
* @method ProductCategoryVariantOption newEntity(array $data, array $options = [])
* @method array<ProductCategoryVariantOption> newEntities(array $data, array $options = [])
* @method ProductCategoryVariantOption get(mixed $primaryKey, array|string $finder = 'all', CacheInterface|string|null $cache = null, Closure|string|null $cacheKey = null, mixed ...$args)
* @method ProductCategoryVariantOption findOrCreate($search, ?callable $callback = null, array $options = [])
* @method ProductCategoryVariantOption patchEntity(EntityInterface $entity, array $data, array $options = [])
* @method array<ProductCategoryVariantOption> patchEntities(iterable $entities, array $data, array $options = [])
* @method ProductCategoryVariantOption|false save(EntityInterface $entity, array $options = [])
* @method ProductCategoryVariantOption saveOrFail(EntityInterface $entity, array $options = [])
* @method iterable<ProductCategoryVariantOption>|ResultSetInterface<ProductCategoryVariantOption>|false saveMany(iterable $entities, array $options = [])
* @method iterable<ProductCategoryVariantOption>|ResultSetInterface<ProductCategoryVariantOption> saveManyOrFail(iterable $entities, array $options = [])
* @method iterable<ProductCategoryVariantOption>|ResultSetInterface<ProductCategoryVariantOption>|false deleteMany(iterable $entities, array $options = [])
* @method iterable<ProductCategoryVariantOption>|ResultSetInterface<ProductCategoryVariantOption> deleteManyOrFail(iterable $entities, array $options = [])
*
* @mixin TimestampBehavior
*/
class ProductCategoryVariantOptionsTable 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_category_variant_options');
$this->setDisplayField('variant_value');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('ProductCategoryVariants', [
'foreignKey' => 'product_category_variant_id',
'joinType' => 'INNER',
]);
}
/**
* Default validation rules.
*
* @param Validator $validator Validator instance.
* @return Validator
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->uuid('product_category_variant_id')
->notEmptyString('product_category_variant_id');
$validator
->scalar('variant_value')
->maxLength('variant_value', 255)
->requirePresence('variant_value', 'create')
->notEmptyString('variant_value');
$validator
->scalar('variant_label')
->maxLength('variant_label', 255)
->allowEmptyString('variant_label');
$validator
->dateTime('deleted')
->allowEmptyDateTime('deleted');
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']);
return $rules;
}
}

View File

@@ -53,6 +53,11 @@ class ProductCategoryVariantsTable extends Table
'foreignKey' => 'product_id',
'className' => 'CakeProducts.Products',
]);
$this->hasMany('ProductCategoryVariantOptions', [
'foreignKey' => 'product_category_variant_id',
'className' => 'CakeProducts.ProductCategoryVariantOptions',
]);
}
/**
@@ -100,4 +105,34 @@ class ProductCategoryVariantsTable extends Table
return $rules;
}
/**
* @param SelectQuery $query
* @param string $internalCategoryId
*
* @return array|\Cake\ORM\Query|SelectQuery
*/
public function findAllCategoryVariantsForCategoryId(SelectQuery $query, string $internalCategoryId)
{
$category = $this->ProductCategories->find()->where(['internal_id' => $internalCategoryId])->firstOrFail();
$this->ProductCategories->behaviors()->get('Tree')->setConfig([
'scope' => [
'product_catalog_id' => $category->product_catalog_id ?? 1,
],
]);
return $this->ProductCategories
->find('path', for: $category->id)
->contain(['ProductCategoryVariants']);
}
/**
* @param string $internalCategoryId
* @return array
*/
public function getAllCategoryVariantsForCategoryId(string $internalCategoryId)
{
return $this->find('allCategoryVariantsForCategoryId', $internalCategoryId)->toArray();
}
}

View File

@@ -63,6 +63,14 @@ class ProductsTable extends Table
'dependent' => true,
'cascadeCallbacks' => true,
]);
$this->hasMany('ProductCategoryVariants', [
'foreignKey' => 'product_id',
'className' => 'CakeProducts.ProductCategoryVariants',
'dependent' => true,
'cascadeCallbacks' => true,
]);
$this->getSchema()->setColumnType('product_type_id', EnumType::from(ProductProductTypeId::class));
$this->addBehavior('Muffin/Trash.Trash');