product attributes
CI / testsuite (mysql, 8.1, ) (push) Failing after 1s Details
CI / testsuite (mysql, 8.4, ) (push) Failing after 0s Details
CI / testsuite (pgsql, 8.1, ) (push) Failing after 1s Details
CI / testsuite (pgsql, 8.4, ) (push) Failing after 0s Details
CI / testsuite (sqlite, 8.1, ) (push) Failing after 0s Details
CI / testsuite (sqlite, 8.1, prefer-lowest) (push) Failing after 0s Details
CI / testsuite (sqlite, 8.4, ) (push) Failing after 0s Details
CI / Coding Standard & Static Analysis (push) Failing after 0s Details

This commit is contained in:
Brandon Shipley 2025-04-05 02:06:23 -07:00
parent 4e6b7ca1ac
commit bb1dab1f43
Signed by: bmfs
GPG Key ID: 14E38571D8BB0DE4
12 changed files with 319 additions and 12 deletions

View File

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
use Migrations\BaseMigration;
class CreateProductAttributes extends BaseMigration
{
/**
* Change Method.
*
* More information on this method is available here:
* https://book.cakephp.org/migrations/4/en/migrations.html#the-change-method
* @return void
*/
public function change(): void
{
$table = $this->table('product_attributes', ['id' => false, 'primary_key' => ['id']]);
$table->addColumn('id', 'uuid', [
'default' => null,
'null' => false,
]);
$table->addColumn('product_id', 'uuid', [
'default' => null,
'null' => false,
]);
$table->addColumn('product_category_attribute_id', 'uuid', [
'default' => null,
'null' => false,
]);
$table->addColumn('attribute_value', 'string', [
'default' => null,
'limit' => 255,
'null' => true,
]);
$table->addColumn('product_category_attribute_option_id', 'uuid', [
'default' => null,
'null' => true,
]);
$table->create();
}
}

View File

@ -67,8 +67,16 @@ class ProductsController extends AppController
$productsTable = $this->getTable(); $productsTable = $this->getTable();
$product = $productsTable->newEmptyEntity(); $product = $productsTable->newEmptyEntity();
if ($this->request->is('post')) { if ($this->request->is('post')) {
$product = $productsTable->patchEntity($product, $this->request->getData()); $postData = $this->request->getData();
if ($productsTable->save($product)) { $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.')); $this->Flash->success(__('The product has been saved.'));
return $this->redirect(['action' => 'index']); return $this->redirect(['action' => 'index']);
@ -104,8 +112,9 @@ class ProductsController extends AppController
Log::debug(print_r($product->getErrors(), true)); Log::debug(print_r($product->getErrors(), true));
$this->Flash->error(__('The product could not be saved. Please, try again.')); $this->Flash->error(__('The product could not be saved. Please, try again.'));
} }
$productCategories = $productsTable->ProductCategories->find('list', limit: 200)->all(); $productCategory = $product->product_category_id ? $productsTable->ProductCategories->find()->where(['internal_id' => $product->product_category_id])->first() : null;
$this->set(compact('product', 'productCategories')); $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\Enum\ProductProductTypeId $product_type_id
* *
* @property \CakeProducts\Model\Entity\ProductCategory $product_category * @property \CakeProducts\Model\Entity\ProductCategory $product_category
* @property \CakeProducts\Model\Entity\ProductAttribute[] $product_attributes
*/ */
class Product extends Entity class Product extends Entity
{ {
@ -31,5 +32,6 @@ class Product extends Entity
'product_category_id' => true, 'product_category_id' => true,
'product_type_id' => true, 'product_type_id' => true,
'product_category' => 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; 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([ $this->ProductCategories->behaviors()->get('Tree')->setConfig([
'scope' => [ 'scope' => [
@ -121,9 +127,12 @@ class ProductCategoryAttributesTable extends Table
], ],
]); ]);
return $category ? $this->ProductCategories return $this->ProductCategories
->find('path', for: $category->id) ->find('path', for: $category->id)
->contain(['ProductCategoryAttributes', 'ProductCategoryAttributes.ProductCategoryAttributeOptions']) ->contain(['ProductCategoryAttributes', 'ProductCategoryAttributes.ProductCategoryAttributeOptions']);
->toArray() : []; }
public function getAllCategoryAttributesForCategoryId(string $internalCategoryId)
{
return $this->find('allCategoryAttributesForCategoryId', $internalCategoryId)->toArray();
} }
} }

View File

@ -58,6 +58,9 @@ class ProductsTable extends Table
'className' => 'CakeProducts.ProductCategories', 'className' => 'CakeProducts.ProductCategories',
]); ]);
$this->hasMany('ProductAttributes', [
'className' => 'CakeProducts.ProductAttributes',
]);
$this->getSchema()->setColumnType('product_type_id', EnumType::from(ProductProductTypeId::class)); $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->isUnique(['product_category_id', 'name']), ['errorField' => 'product_category_id']);
$rules->add($rules->existsIn(['product_category_id'], 'ProductCategories'), ['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; return $rules;
} }

View File

@ -28,7 +28,7 @@ foreach ($productCategoryAttributes as $productCategoryAttribute) {
if ($productCategoryAttribute->attribute_type_id === ProductCategoryAttributeTypeId::Integer) { if ($productCategoryAttribute->attribute_type_id === ProductCategoryAttributeTypeId::Integer) {
$inputType = 'number'; $inputType = 'number';
} }
echo $this->Form->control('product_attributes.' . $cnt . '.product_category_attribute_id', [ echo $this->Form->control('product_attributes.' . $cnt . '.attribute_value', [
'type' => $inputType, 'type' => $inputType,
'label' => $productCategoryAttribute->name, 'label' => $productCategoryAttribute->name,
'options' => $options, 'options' => $options,

View File

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Test\Fixture;
use Cake\TestSuite\Fixture\TestFixture;
/**
* ProductAttributesFixture
*/
class ProductAttributesFixture extends TestFixture
{
/**
* Init method
*
* @return void
*/
public function init(): void
{
$this->records = [
];
parent::init();
}
}

View File

@ -21,10 +21,24 @@ class ProductCategoryAttributeOptionsFixture extends TestFixture
[ [
'id' => 'e06f1723-2456-483a-b3c4-004603e032a8', 'id' => 'e06f1723-2456-483a-b3c4-004603e032a8',
'product_category_attribute_id' => '37078cf0-0130-4b93-bb7e-abe7d665ed2c', 'product_category_attribute_id' => '37078cf0-0130-4b93-bb7e-abe7d665ed2c',
'attribute_value' => 'Lorem ipsum dolor sit amet', 'attribute_value' => 'Red',
'attribute_label' => 'Lorem ipsum dolor sit amet', 'attribute_label' => 'Red',
'enabled' => 1, 'enabled' => 1,
], ],
[
'id' => 'e06f1723-2456-483a-b3c4-004603e032a1',
'product_category_attribute_id' => '37078cf0-0130-4b93-bb7e-abe7d665ed2c',
'attribute_value' => 'Blue',
'attribute_label' => 'Blue',
'enabled' => 1,
],
[
'id' => 'e06f1723-2456-483a-b3c4-004603e032a2',
'product_category_attribute_id' => '37078cf0-0130-4b93-bb7e-abe7d665ed2c',
'attribute_value' => 'Green',
'attribute_label' => 'Green',
'enabled' => 1,
]
]; ];
parent::init(); parent::init();
} }

View File

@ -32,7 +32,10 @@ class ProductsControllerTest extends BaseControllerTest
*/ */
protected array $fixtures = [ protected array $fixtures = [
'plugin.CakeProducts.Products', 'plugin.CakeProducts.Products',
'plugin.CakeProducts.ProductAttributes',
'plugin.CakeProducts.ProductCategories', 'plugin.CakeProducts.ProductCategories',
'plugin.CakeProducts.ProductCategoryAttributes',
'plugin.CakeProducts.ProductCategoryAttributeOptions',
// 'plugin.CakeProducts.ProductCatalogs', // 'plugin.CakeProducts.ProductCatalogs',
]; ];
@ -135,6 +138,52 @@ class ProductsControllerTest extends BaseControllerTest
$this->assertEquals($cntBefore, $cntAfter); $this->assertEquals($cntBefore, $cntAfter);
} }
/**
* Test add method
*
* Tests the add action with a logged in user
*
* @return void
* @throws Exception
*
* @uses ProductsController::add
*/
public function testAddPostSuccess(): void
{
$cntBefore = $this->Products->find()->count();
$productAttributesCntBefore = $this->Products->ProductAttributes->find()->count();
$this->loginUserByRole('admin');
$url = [
'plugin' => 'CakeProducts',
'controller' => 'Products',
'action' => 'add',
];
$data = [
// test new data here
'product_catalog_id' => '115153f3-2f59-4234-8ff8-e1b205761428',
'product_category_id' => '6d223283-361b-4f9f-a7f1-c97aa0ca4c23',
'name' => '14AWG Red Wire',
'product_type_id' => 1,
'product_attributes' => [
[
'product_category_attribute_id' => '37078cf0-0130-4b93-bb7e-abe7d665ed2c',
'product_category_attribute_option_id' => 'e06f1723-2456-483a-b3c4-004603e032a2', // green
'attribute_value' => '',
],
],
];
$this->post($url, $data);
$this->assertResponseCode(302);
$this->assertRedirectContains('products');
$cntAfter = $this->Products->find()->count();
$productAttributesCntAfter = $this->Products->ProductAttributes->find()->count();
$this->assertEquals($cntBefore + 1, $cntAfter);
$this->assertEquals($productAttributesCntBefore + 1, $productAttributesCntAfter);
}
/** /**
* Test add method * Test add method
* *
@ -160,6 +209,7 @@ class ProductsControllerTest extends BaseControllerTest
'product_category_id' => '', 'product_category_id' => '',
'name' => '', 'name' => '',
'product_type_id' => 1, 'product_type_id' => 1,
'product_attributes' => [],
]; ];
$this->post($url, $data); $this->post($url, $data);
$this->assertResponseCode(200); $this->assertResponseCode(200);

View File

@ -63,6 +63,7 @@ class ProductsTableTest extends TestCase
// verify all associations loaded // verify all associations loaded
$expectedAssociations = [ $expectedAssociations = [
'ProductCategories', 'ProductCategories',
'ProductAttributes',
]; ];
$associations = $this->Products->associations(); $associations = $this->Products->associations();