wip product category variants

This commit is contained in:
2025-06-29 21:52:07 -07:00
parent a01805dc53
commit f61a4161be
12 changed files with 502 additions and 16 deletions

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Model\Entity;
use Cake\ORM\Entity;
/**
* ProductCategoryVariant Entity
*
* @property string $id
* @property string $name
* @property string|null $product_category_id
* @property string|null $product_id
* @property int $attribute_type_id
* @property bool $enabled
*
* @property \App\Model\Entity\ProductCategory $product_category
* @property \App\Model\Entity\Product $product
*/
class ProductCategoryVariant 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_id' => true,
'product_id' => true,
'attribute_type_id' => true,
'enabled' => true,
'product_category' => true,
'product' => true,
];
}

View File

@@ -0,0 +1,108 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Model\Table;
use Cake\ORM\Query\SelectQuery;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* ProductCategoryVariants Model
*
* @property \App\Model\Table\ProductCategoriesTable&\Cake\ORM\Association\BelongsTo $ProductCategories
* @property \App\Model\Table\ProductsTable&\Cake\ORM\Association\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 = [])
*/
class ProductCategoryVariantsTable 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_variants');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
$this->belongsTo('ProductCategories', [
'foreignKey' => 'product_category_id',
'bindingKey' => 'internal_id',
'className' => 'CakeProducts.ProductCategories',
]);
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'className' => 'CakeProducts.Products',
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->scalar('name')
->maxLength('name', 255)
->requirePresence('name', 'create')
->notEmptyString('name');
$validator
->uuid('product_category_id')
->allowEmptyString('product_category_id');
$validator
->uuid('product_id')
->allowEmptyString('product_id');
$validator
->integer('attribute_type_id')
->requirePresence('attribute_type_id', 'create')
->notEmptyString('attribute_type_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 \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\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->existsIn(['product_category_id'], 'ProductCategories'), ['errorField' => 'product_category_id']);
$rules->add($rules->existsIn(['product_id'], 'Products'), ['errorField' => 'product_id']);
return $rules;
}
}