product attributes

This commit is contained in:
2025-04-05 02:06:23 -07:00
parent 4e6b7ca1ac
commit bb1dab1f43
12 changed files with 319 additions and 12 deletions

View File

@@ -67,8 +67,16 @@ class ProductsController extends AppController
$productsTable = $this->getTable();
$product = $productsTable->newEmptyEntity();
if ($this->request->is('post')) {
$product = $productsTable->patchEntity($product, $this->request->getData());
if ($productsTable->save($product)) {
$postData = $this->request->getData();
$saveOptions = [
'associated' => ['ProductAttributes'],
];
// Log::debug(print_r('$postData', true));
// Log::debug(print_r($postData, true));
// Log::debug(print_r('$saveOptions', true));
// Log::debug(print_r($saveOptions, true));
$product = $productsTable->patchEntity($product, $postData, $saveOptions);
if ($productsTable->save($product, $saveOptions)) {
$this->Flash->success(__('The product has been saved.'));
return $this->redirect(['action' => 'index']);
@@ -104,8 +112,9 @@ class ProductsController extends AppController
Log::debug(print_r($product->getErrors(), true));
$this->Flash->error(__('The product could not be saved. Please, try again.'));
}
$productCategories = $productsTable->ProductCategories->find('list', limit: 200)->all();
$this->set(compact('product', 'productCategories'));
$productCategory = $product->product_category_id ? $productsTable->ProductCategories->find()->where(['internal_id' => $product->product_category_id])->first() : null;
$productCatalogs = $productsTable->ProductCategories->ProductCatalogs->find('list')->toArray();
$this->set(compact('product', 'productCatalogs', 'productCategory'));
}
/**

View File

@@ -14,6 +14,7 @@ use Cake\ORM\Entity;
* @property \CakeProducts\Model\Enum\ProductProductTypeId $product_type_id
*
* @property \CakeProducts\Model\Entity\ProductCategory $product_category
* @property \CakeProducts\Model\Entity\ProductAttribute[] $product_attributes
*/
class Product extends Entity
{
@@ -31,5 +32,6 @@ class Product extends Entity
'product_category_id' => true,
'product_type_id' => true,
'product_category' => true,
'product_attributes' => true,
];
}

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Model\Entity;
use Cake\ORM\Entity;
/**
* ProductAttribute Entity
*
* @property string $id
* @property string $product_id
* @property string $product_category_attribute_id
* @property string|null $attribute_value
* @property string|null $product_category_attribute_option_id
*
* @property Product $product
* @property ProductCategoryAttribute $product_category_attribute
* @property ProductCategoryAttributeOption $product_category_attribute_option
*/
class ProductAttribute 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 = [
'product_id' => true,
'product_category_attribute_id' => true,
'attribute_value' => true,
'product_category_attribute_option_id' => true,
'product' => false,
'product_category_attribute' => false,
'product_category_attribute_option' => false,
];
}

View File

@@ -0,0 +1,111 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Model\Table;
use Cake\Core\Configure;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* ProductAttributes Model
*
* @property \App\Model\Table\ProductsTable&\Cake\ORM\Association\BelongsTo $Products
* @property \App\Model\Table\ProductCategoryAttributesTable&\Cake\ORM\Association\BelongsTo $ProductCategoryAttributes
* @property \App\Model\Table\ProductCategoryAttributeOptionsTable&\Cake\ORM\Association\BelongsTo $ProductCategoryAttributeOptions
*
* @method \App\Model\Entity\ProductAttribute newEmptyEntity()
* @method \App\Model\Entity\ProductAttribute newEntity(array $data, array $options = [])
* @method array<\App\Model\Entity\ProductAttribute> newEntities(array $data, array $options = [])
* @method \App\Model\Entity\ProductAttribute 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\ProductAttribute findOrCreate($search, ?callable $callback = null, array $options = [])
* @method \App\Model\Entity\ProductAttribute patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
* @method array<\App\Model\Entity\ProductAttribute> patchEntities(iterable $entities, array $data, array $options = [])
* @method \App\Model\Entity\ProductAttribute|false save(\Cake\Datasource\EntityInterface $entity, array $options = [])
* @method \App\Model\Entity\ProductAttribute saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = [])
* @method iterable<\App\Model\Entity\ProductAttribute>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\ProductAttribute>|false saveMany(iterable $entities, array $options = [])
* @method iterable<\App\Model\Entity\ProductAttribute>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\ProductAttribute> saveManyOrFail(iterable $entities, array $options = [])
* @method iterable<\App\Model\Entity\ProductAttribute>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\ProductAttribute>|false deleteMany(iterable $entities, array $options = [])
* @method iterable<\App\Model\Entity\ProductAttribute>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\ProductAttribute> deleteManyOrFail(iterable $entities, array $options = [])
*/
class ProductAttributesTable 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_attributes');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->setEntityClass(
Configure::read('CakeProducts.ProductAttributes.entity', 'CakeProducts\Model\Entity\ProductAttribute')
);
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'className' => 'CakeProducts.Products',
'joinType' => 'INNER',
]);
$this->belongsTo('ProductCategoryAttributes', [
'foreignKey' => 'product_category_attribute_id',
'className' => 'CakeProducts.ProductCategoryAttributes',
'joinType' => 'INNER',
]);
$this->belongsTo('ProductCategoryAttributeOptions', [
'foreignKey' => 'product_category_attribute_option_id',
'className' => 'CakeProducts.ProductCategoryAttributeOptions',
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->uuid('product_id')
->notEmptyString('product_id');
$validator
->uuid('product_category_attribute_id')
->notEmptyString('product_category_attribute_id');
$validator
->scalar('attribute_value')
->maxLength('attribute_value', 255)
->allowEmptyString('attribute_value');
$validator
->uuid('product_category_attribute_option_id')
->allowEmptyString('product_category_attribute_option_id');
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->existsIn(['product_id'], 'Products'), ['errorField' => 'product_id']);
$rules->add($rules->existsIn(['product_category_attribute_id'], 'ProductCategoryAttributes'), ['errorField' => 'product_category_attribute_id']);
$rules->add($rules->existsIn(['product_category_attribute_option_id'], 'ProductCategoryAttributeOptions'), ['errorField' => 'product_category_attribute_option_id']);
return $rules;
}
}

View File

@@ -111,9 +111,15 @@ class ProductCategoryAttributesTable extends Table
return $rules;
}
public function getAllCategoryAttributesForCategoryId(string $internalCategoryId)
/**
* @param SelectQuery $query
* @param string $internalCategoryId
*
* @return array|\Cake\ORM\Query|SelectQuery
*/
public function findAllCategoryAttributesForCategoryId(SelectQuery $query, string $internalCategoryId)
{
$category = $this->ProductCategories->find()->where(['internal_id' => $internalCategoryId])->first();
$category = $this->ProductCategories->find()->where(['internal_id' => $internalCategoryId])->firstOrFail();
$this->ProductCategories->behaviors()->get('Tree')->setConfig([
'scope' => [
@@ -121,9 +127,12 @@ class ProductCategoryAttributesTable extends Table
],
]);
return $category ? $this->ProductCategories
return $this->ProductCategories
->find('path', for: $category->id)
->contain(['ProductCategoryAttributes', 'ProductCategoryAttributes.ProductCategoryAttributeOptions'])
->toArray() : [];
->contain(['ProductCategoryAttributes', 'ProductCategoryAttributes.ProductCategoryAttributeOptions']);
}
public function getAllCategoryAttributesForCategoryId(string $internalCategoryId)
{
return $this->find('allCategoryAttributesForCategoryId', $internalCategoryId)->toArray();
}
}

View File

@@ -58,6 +58,9 @@ class ProductsTable extends Table
'className' => 'CakeProducts.ProductCategories',
]);
$this->hasMany('ProductAttributes', [
'className' => 'CakeProducts.ProductAttributes',
]);
$this->getSchema()->setColumnType('product_type_id', EnumType::from(ProductProductTypeId::class));
}
@@ -99,6 +102,7 @@ class ProductsTable extends Table
{
$rules->add($rules->isUnique(['product_category_id', 'name']), ['errorField' => 'product_category_id']);
$rules->add($rules->existsIn(['product_category_id'], 'ProductCategories'), ['errorField' => 'product_category_id']);
// $rules->add($rules->validCount('product_attributes', 0, '<=', 'You must not have any tags'));
return $rules;
}