From 9d777f12b2c493f5af8924f98852be91e8b38808 Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Sat, 5 Jul 2025 23:18:54 -0700 Subject: [PATCH] default product type id on categories table --- ...efaultProductTypeIdToProductCategories.php | 25 +++++++++++++++++++ .../ProductCategoryVariantsController.php | 19 ++++++++++---- src/Model/Entity/Product.php | 10 +++++--- src/Model/Entity/ProductCategory.php | 3 +++ src/Model/Table/ProductCategoriesTable.php | 8 ++++++ templates/ProductCategoryVariants/index.php | 10 ++++++++ templates/element/ProductCategories/form.php | 1 + 7 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 config/Migrations/20250706061008_AddDefaultProductTypeIdToProductCategories.php diff --git a/config/Migrations/20250706061008_AddDefaultProductTypeIdToProductCategories.php b/config/Migrations/20250706061008_AddDefaultProductTypeIdToProductCategories.php new file mode 100644 index 0000000..fecca37 --- /dev/null +++ b/config/Migrations/20250706061008_AddDefaultProductTypeIdToProductCategories.php @@ -0,0 +1,25 @@ +table('product_categories'); + $table->addColumn('default_product_type_id', 'integer', [ + 'default' => null, + 'limit' => 11, + 'null' => true, + ]); + $table->update(); + } +} diff --git a/src/Controller/ProductCategoryVariantsController.php b/src/Controller/ProductCategoryVariantsController.php index 6de1897..1615bf6 100644 --- a/src/Controller/ProductCategoryVariantsController.php +++ b/src/Controller/ProductCategoryVariantsController.php @@ -37,7 +37,7 @@ class ProductCategoryVariantsController extends AppController public function index() { $query = $this->getTable()->find() - ->contain(['ProductCategories', 'Products']); + ->contain(['ProductCategories', 'Products', 'ProductCategoryVariantOptions']); $productCategoryVariants = $this->paginate($query); $this->set(compact('productCategoryVariants')); @@ -52,7 +52,7 @@ class ProductCategoryVariantsController extends AppController */ public function view($id = null) { - $productCategoryVariant = $this->getTable()->get($id, contain: ['ProductCategories', 'Products']); + $productCategoryVariant = $this->getTable()->get($id, contain: ['ProductCategories', 'Products', 'ProductCategoryVariantOptions']); $this->set(compact('productCategoryVariant')); } @@ -77,7 +77,7 @@ class ProductCategoryVariantsController extends AppController ], ]; $productCategoryVariant = $productCategoryVariantsTable->patchEntity($productCategoryVariant, $postData, $saveOptions); - if ($productCategoryVariantsTable->save($productCategoryVariant)) { + if ($productCategoryVariantsTable->save($productCategoryVariant, $saveOptions)) { $this->Flash->success(__('The product category variant has been saved.')); return $this->redirect(['action' => 'index']); @@ -103,8 +103,17 @@ class ProductCategoryVariantsController extends AppController $productCategoryVariantsTable = $this->getTable(); $productCategoryVariant = $productCategoryVariantsTable->get($id, contain: []); if ($this->request->is(['patch', 'post', 'put'])) { - $productCategoryVariant = $productCategoryVariantsTable->patchEntity($productCategoryVariant, $this->request->getData()); - if ($productCategoryVariantsTable->save($productCategoryVariant)) { + $postData = $this->request->getData(); +// if ($this->request->getSession()->read('Auth.User.id')) { +// $postData['created_by'] = $this->request->getSession()->read('Auth.User.id'); +// } + $saveOptions = [ + 'associated' => [ + 'ProductCategoryVariantOptions' + ], + ]; + $productCategoryVariant = $productCategoryVariantsTable->patchEntity($productCategoryVariant, $postData, $saveOptions); + if ($productCategoryVariantsTable->save($productCategoryVariant, $saveOptions)) { $this->Flash->success(__('The product category variant has been saved.')); return $this->redirect(['action' => 'index']); diff --git a/src/Model/Entity/Product.php b/src/Model/Entity/Product.php index 88b1023..8e05639 100644 --- a/src/Model/Entity/Product.php +++ b/src/Model/Entity/Product.php @@ -5,6 +5,7 @@ namespace CakeProducts\Model\Entity; use Cake\I18n\DateTime; use Cake\ORM\Entity; +use CakeProducts\Model\Enum\ProductProductTypeId; /** * Product Entity @@ -12,11 +13,13 @@ use Cake\ORM\Entity; * @property string $id * @property string $name * @property string $product_category_id - * @property \CakeProducts\Model\Enum\ProductProductTypeId $product_type_id + * @property ProductProductTypeId $product_type_id * @property DateTime|null $deleted * - * @property \CakeProducts\Model\Entity\ProductCategory $product_category - * @property \CakeProducts\Model\Entity\ProductAttribute[] $product_attributes + * @property ProductCategory $product_category + * @property ProductAttribute[] $product_attributes + * @property ProductCategoryVariant[] $product_category_variants + * */ class Product extends Entity { @@ -37,5 +40,6 @@ class Product extends Entity // entities 'product_category' => false, 'product_attributes' => true, + 'product_category_variants' => false, ]; } diff --git a/src/Model/Entity/ProductCategory.php b/src/Model/Entity/ProductCategory.php index eb5c112..0590733 100644 --- a/src/Model/Entity/ProductCategory.php +++ b/src/Model/Entity/ProductCategory.php @@ -5,6 +5,7 @@ namespace CakeProducts\Model\Entity; use Cake\I18n\DateTime; use Cake\ORM\Entity; +use CakeProducts\Model\Enum\ProductProductTypeId; /** * ProductCategory Entity @@ -19,6 +20,7 @@ use Cake\ORM\Entity; * @property int $rght * @property bool $enabled * @property DateTime|null $deleted + * @property ProductProductTypeId|null $default_product_type_id * * @property \CakeProducts\Model\Entity\ProductCatalog $product_catalog * @property \CakeProducts\Model\Entity\ParentProductCategory $parent_product_category @@ -40,6 +42,7 @@ class ProductCategory extends Entity 'internal_id' => true, 'name' => true, 'category_description' => true, + 'default_product_type_id' => true, 'parent_id' => true, 'lft' => true, 'rght' => true, diff --git a/src/Model/Table/ProductCategoriesTable.php b/src/Model/Table/ProductCategoriesTable.php index 7c14475..e02b92f 100644 --- a/src/Model/Table/ProductCategoriesTable.php +++ b/src/Model/Table/ProductCategoriesTable.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace CakeProducts\Model\Table; use Cake\Core\Configure; +use Cake\Database\Type\EnumType; use Cake\Datasource\EntityInterface; use Cake\Datasource\ResultSetInterface; use Cake\ORM\Association\BelongsTo; @@ -14,6 +15,7 @@ use Cake\ORM\RulesChecker; use Cake\ORM\Table; use Cake\Validation\Validator; use CakeProducts\Model\Entity\ProductCategory; +use CakeProducts\Model\Enum\ProductProductTypeId; use Closure; use Psr\SimpleCache\CacheInterface; @@ -99,6 +101,8 @@ class ProductCategoriesTable extends Table 'cascadeCallbacks' => true, ]); + $this->getSchema()->setColumnType('default_product_type_id', EnumType::from(ProductProductTypeId::class)); + $this->behaviors()->Tree->setConfig('scope', ['product_catalog_id' => $this->treeCatalogId]); $this->addBehavior('Muffin/Trash.Trash'); } @@ -137,6 +141,10 @@ class ProductCategoriesTable extends Table ->dateTime('deleted') ->allowEmptyDateTime('deleted'); + $validator + ->integer('default_product_type_id') + ->allowEmptyString('default_product_type_id'); + return $validator; } diff --git a/templates/ProductCategoryVariants/index.php b/templates/ProductCategoryVariants/index.php index 9e1600f..1ce7013 100644 --- a/templates/ProductCategoryVariants/index.php +++ b/templates/ProductCategoryVariants/index.php @@ -17,6 +17,7 @@ Paginator->sort('product_id') ?> Paginator->sort('attribute_type_id') ?> Paginator->sort('enabled') ?> + @@ -29,6 +30,15 @@ hasValue('product') ? $this->Html->link($productCategoryVariant->product->name, ['controller' => 'Products', 'action' => 'view', $productCategoryVariant->product->id]) : '' ?> Number->format($productCategoryVariant->attribute_type_id) ?> enabled) ?> + + + product_category_variant_options) && $productCategoryVariant->product_category_variant_options ? count($productCategoryVariant->product_category_variant_options) : 0; + foreach ($productCategoryVariant->product_category_variant_options as $index => $productCategoryVariantOption) : ?> + variant_value); ?> + + + Html->link(__('View'), ['action' => 'view', $productCategoryVariant->id]) ?> Html->link(__('Edit'), ['action' => 'edit', $productCategoryVariant->id]) ?> diff --git a/templates/element/ProductCategories/form.php b/templates/element/ProductCategories/form.php index 410c01a..b629f02 100644 --- a/templates/element/ProductCategories/form.php +++ b/templates/element/ProductCategories/form.php @@ -26,5 +26,6 @@ echo $this->Form->control('parent_id', [ 'empty' => true, 'id' => 'product_category_parent_id', ]); +echo $this->Form->control('default_product_type_id'); echo $this->Form->control('enabled'); ?> \ No newline at end of file