diff --git a/config/Migrations/20250629054627_CreateProductCategoryVariants.php b/config/Migrations/20250629054627_CreateProductCategoryVariants.php index 7bf161f..8e35324 100644 --- a/config/Migrations/20250629054627_CreateProductCategoryVariants.php +++ b/config/Migrations/20250629054627_CreateProductCategoryVariants.php @@ -48,14 +48,14 @@ class CreateProductCategoryVariants extends AbstractMigration 'name' => 'VARIANTS_BY_PRODUCT_ID', 'unique' => false, ]); - $table->addIndex([ - 'name', - 'product_category_id', - 'product_id', - ], [ - 'name' => 'VARIANTS_BY_NAME_AND_PRODUCT_CATEGORY_ID_AND_PRODUCT_ID_UNIQUE', - 'unique' => true, - ]); +// $table->addIndex([ +// 'name', +// 'product_category_id', +// 'product_id', +// ], [ +// 'name' => 'VARIANTS_BY_NAME_AND_PRODUCT_CATEGORY_ID_AND_PRODUCT_ID_UNIQUE', +// 'unique' => true, +// ]); $table->create(); } } diff --git a/config/Migrations/20250706011935_CreateProductCategoryVariantOptions.php b/config/Migrations/20250706011935_CreateProductCategoryVariantOptions.php index 3491a85..2628b15 100644 --- a/config/Migrations/20250706011935_CreateProductCategoryVariantOptions.php +++ b/config/Migrations/20250706011935_CreateProductCategoryVariantOptions.php @@ -49,8 +49,7 @@ class CreateProductCategoryVariantOptions extends BaseMigration 'default' => true, 'null' => false, ]); - - $table->addForeignKey('product_category_variant_id', 'product_category_variants'); +// $table->addForeignKey('product_category_variant_id', 'product_category_variants'); // @TODO why cant this be included??? breaks tests on tearDown $table->create(); } diff --git a/src/Controller/ProductCategoryVariantsController.php b/src/Controller/ProductCategoryVariantsController.php index 9420db4..6de1897 100644 --- a/src/Controller/ProductCategoryVariantsController.php +++ b/src/Controller/ProductCategoryVariantsController.php @@ -5,6 +5,7 @@ namespace CakeProducts\Controller; use App\Controller\AppController; use Cake\Core\Configure; +use Cake\Log\Log; use Cake\ORM\Table; use Cake\ORM\TableRegistry; use CheeseCake\Controller\Traits\OverrideTableTrait; @@ -66,12 +67,23 @@ class ProductCategoryVariantsController extends AppController $productCategoryVariant = $productCategoryVariantsTable->newEmptyEntity(); if ($this->request->is('post')) { - $productCategoryVariant = $productCategoryVariantsTable->patchEntity($productCategoryVariant, $this->request->getData()); + $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)) { $this->Flash->success(__('The product category variant has been saved.')); return $this->redirect(['action' => 'index']); } + Log::debug('print_r($productCategoryVariant->getErrors(), true) failed to save in product category variants add'); + Log::debug(print_r($productCategoryVariant->getErrors(), true)); $this->Flash->error(__('The product category variant could not be saved. Please, try again.')); } $productCategories = $productCategoryVariantsTable->ProductCategories->find('list', keyField: 'internal_id', valueField: 'name')->all(); diff --git a/src/Model/Table/ProductCategoryVariantsTable.php b/src/Model/Table/ProductCategoryVariantsTable.php index 6cca32b..d478e85 100644 --- a/src/Model/Table/ProductCategoryVariantsTable.php +++ b/src/Model/Table/ProductCategoryVariantsTable.php @@ -57,6 +57,8 @@ class ProductCategoryVariantsTable extends Table $this->hasMany('ProductCategoryVariantOptions', [ 'foreignKey' => 'product_category_variant_id', 'className' => 'CakeProducts.ProductCategoryVariantOptions', + 'dependent' => true, + 'cascadeCallbacks' => true, ]); } @@ -99,7 +101,7 @@ class ProductCategoryVariantsTable extends Table */ 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->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']); diff --git a/templates/ProductCategoryVariantOptions/add.php b/templates/ProductCategoryVariantOptions/add.php new file mode 100644 index 0000000..205412a --- /dev/null +++ b/templates/ProductCategoryVariantOptions/add.php @@ -0,0 +1,20 @@ +setLayout('ajax'); +$prefix = $prefix ?? ''; +if ($this->request->getQuery('prefix') !== null) { + $prefix = 'product_category_variant_options.' . $this->request->getQuery('prefix') . '.'; +} +echo '