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,107 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Controller;
use App\Controller\AppController;
/**
* ProductCategoryVariants Controller
*
* @property \App\Model\Table\ProductCategoryVariantsTable $ProductCategoryVariants
*/
class ProductCategoryVariantsController extends AppController
{
/**
* Index method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function index()
{
$query = $this->ProductCategoryVariants->find()
->contain(['ProductCategories', 'Products']);
$productCategoryVariants = $this->paginate($query);
$this->set(compact('productCategoryVariants'));
}
/**
* View method
*
* @param string|null $id Product Category Variant id.
* @return \Cake\Http\Response|null|void Renders view
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$productCategoryVariant = $this->ProductCategoryVariants->get($id, contain: ['ProductCategories', 'Products']);
$this->set(compact('productCategoryVariant'));
}
/**
* Add method
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$productCategoryVariant = $this->ProductCategoryVariants->newEmptyEntity();
if ($this->request->is('post')) {
$productCategoryVariant = $this->ProductCategoryVariants->patchEntity($productCategoryVariant, $this->request->getData());
if ($this->ProductCategoryVariants->save($productCategoryVariant)) {
$this->Flash->success(__('The product category variant has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The product category variant could not be saved. Please, try again.'));
}
$productCategories = $this->ProductCategoryVariants->ProductCategories->find('list', limit: 200)->all();
$products = $this->ProductCategoryVariants->Products->find('list', limit: 200)->all();
$this->set(compact('productCategoryVariant', 'productCategories', 'products'));
}
/**
* Edit method
*
* @param string|null $id Product Category Variant id.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function edit($id = null)
{
$productCategoryVariant = $this->ProductCategoryVariants->get($id, contain: []);
if ($this->request->is(['patch', 'post', 'put'])) {
$productCategoryVariant = $this->ProductCategoryVariants->patchEntity($productCategoryVariant, $this->request->getData());
if ($this->ProductCategoryVariants->save($productCategoryVariant)) {
$this->Flash->success(__('The product category variant has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The product category variant could not be saved. Please, try again.'));
}
$productCategories = $this->ProductCategoryVariants->ProductCategories->find('list', limit: 200)->all();
$products = $this->ProductCategoryVariants->Products->find('list', limit: 200)->all();
$this->set(compact('productCategoryVariant', 'productCategories', 'products'));
}
/**
* Delete method
*
* @param string|null $id Product Category Variant id.
* @return \Cake\Http\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$productCategoryVariant = $this->ProductCategoryVariants->get($id);
if ($this->ProductCategoryVariants->delete($productCategoryVariant)) {
$this->Flash->success(__('The product category variant has been deleted.'));
} else {
$this->Flash->error(__('The product category variant could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
}

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;
}
}