From 30aad1dea482118cc3210462677565f2c53f331d Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Sun, 12 Oct 2025 01:04:36 -0700 Subject: [PATCH] category photos should be working --- ...07_AddProductCategoryIdToProductPhotos.php | 29 +++++++++++++++++++ .../ProductCategoriesController.php | 1 + src/Controller/ProductPhotosController.php | 5 +++- src/Model/Entity/Product.php | 1 + src/Model/Entity/ProductCategory.php | 1 + src/Model/Entity/ProductPhoto.php | 17 +++++++---- src/Model/Table/ProductCategoriesTable.php | 21 ++++++++++++++ src/Model/Table/ProductPhotosTable.php | 16 ++++++++-- tests/Fixture/ProductPhotosFixture.php | 18 ++++++++++++ .../ProductPhotosControllerTest.php | 1 + .../Table/ProductCategoriesTableTest.php | 4 ++- 11 files changed, 105 insertions(+), 9 deletions(-) create mode 100644 config/Migrations/20251012073807_AddProductCategoryIdToProductPhotos.php diff --git a/config/Migrations/20251012073807_AddProductCategoryIdToProductPhotos.php b/config/Migrations/20251012073807_AddProductCategoryIdToProductPhotos.php new file mode 100644 index 0000000..6348ea8 --- /dev/null +++ b/config/Migrations/20251012073807_AddProductCategoryIdToProductPhotos.php @@ -0,0 +1,29 @@ +table('product_photos'); + $table->addColumn('product_category_id', 'uuid', [ + 'default' => null, + 'null' => true, + ]); + $table->addColumn('primary_category_photo', 'boolean', [ + 'default' => false, + 'null' => false, + ]); + $table->update(); + } +} diff --git a/src/Controller/ProductCategoriesController.php b/src/Controller/ProductCategoriesController.php index 299c63e..379d896 100644 --- a/src/Controller/ProductCategoriesController.php +++ b/src/Controller/ProductCategoriesController.php @@ -59,6 +59,7 @@ class ProductCategoriesController extends AppController 'ChildProductCategories', 'ProductCategoryAttributes', 'ProductCategoryAttributes.ProductCategoryAttributeOptions', + 'PrimaryProductPhotos', ]); $productCategoryAttributes = $this->getTable()->ProductCategoryAttributes->getAllCategoryAttributesForCategoryId($productCategory->internal_id); diff --git a/src/Controller/ProductPhotosController.php b/src/Controller/ProductPhotosController.php index 6c11dda..2311b18 100644 --- a/src/Controller/ProductPhotosController.php +++ b/src/Controller/ProductPhotosController.php @@ -45,7 +45,7 @@ class ProductPhotosController extends AppController */ public function view($id = null) { - $productPhoto = $this->getTable()->get($id, contain: ['Products', 'ProductSkus']); + $productPhoto = $this->getTable()->get($id, contain: ['Products', 'ProductSkus', 'ProductCategories']); $this->set(compact('productPhoto')); } @@ -94,6 +94,7 @@ class ProductPhotosController extends AppController if (!file_exists($destination)) { throw new ForbiddenException('Failed to move the uploaded image to the appropriate folder. Please try again.'); } + $postData['product_category_id'] = $product->product_category_id ?? null; $postData['photo_dir'] = $path; $postData['photo_filename'] = $uuid; $productPhoto = $productPhotosTable->patchEntity($productPhoto, $postData); @@ -102,6 +103,8 @@ class ProductPhotosController extends AppController return $this->redirect(['action' => 'index']); } + dd($productPhoto->product_category_id); +// dd(print_r($productPhoto->getErrors(), true)); $this->Flash->error(__('The product photo could not be saved. Please, try again.')); } $products = $productPhotosTable->Products->find('list', limit: 200)->all(); diff --git a/src/Model/Entity/Product.php b/src/Model/Entity/Product.php index b9a5b8d..1d221f3 100644 --- a/src/Model/Entity/Product.php +++ b/src/Model/Entity/Product.php @@ -42,5 +42,6 @@ class Product extends Entity 'product_category' => false, 'product_attributes' => true, 'product_category_variants' => true, + 'primary_product_photo' => true, ]; } diff --git a/src/Model/Entity/ProductCategory.php b/src/Model/Entity/ProductCategory.php index 4ea26f9..f50e188 100644 --- a/src/Model/Entity/ProductCategory.php +++ b/src/Model/Entity/ProductCategory.php @@ -53,5 +53,6 @@ class ProductCategory extends Entity 'product_catalog' => true, 'parent_product_category' => true, 'child_product_categories' => true, + 'primary_product_photo' => true, ]; } diff --git a/src/Model/Entity/ProductPhoto.php b/src/Model/Entity/ProductPhoto.php index ed30166..46177e4 100644 --- a/src/Model/Entity/ProductPhoto.php +++ b/src/Model/Entity/ProductPhoto.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace CakeProducts\Model\Entity; +use Cake\I18n\DateTime; use Cake\ORM\Entity; /** @@ -10,17 +11,21 @@ use Cake\ORM\Entity; * * @property string $id * @property string $product_id + * @property string|null $product_category_id * @property string|null $product_sku_id * @property string $photo_dir * @property string $photo_filename * @property bool $primary_photo + * @property bool $primary_category_photo * @property int $photo_position * @property bool $enabled - * @property \Cake\I18n\DateTime $created - * @property \Cake\I18n\DateTime|null $modified - * @property \Cake\I18n\DateTime|null $deleted + * @property DateTime $created + * @property DateTime|null $modified + * @property DateTime|null $deleted * - * @property \CakeProducts\Model\Entity\Product $product + * @property Product|null $product + * @property ProductSku|null $product_sku + * @property ProductCategory $product_category */ class ProductPhoto extends Entity { @@ -36,16 +41,18 @@ class ProductPhoto extends Entity protected array $_accessible = [ 'product_id' => true, 'product_sku_id' => true, + 'product_category_id' => true, 'photo_dir' => true, 'photo_filename' => true, 'primary_photo' => true, + 'primary_category_photo' => true, 'photo_position' => true, 'enabled' => true, 'created' => true, 'modified' => true, 'deleted' => true, - + // entities 'product' => true, ]; } diff --git a/src/Model/Table/ProductCategoriesTable.php b/src/Model/Table/ProductCategoriesTable.php index e02b92f..017ea6a 100644 --- a/src/Model/Table/ProductCategoriesTable.php +++ b/src/Model/Table/ProductCategoriesTable.php @@ -100,6 +100,27 @@ class ProductCategoriesTable extends Table 'dependent' => true, 'cascadeCallbacks' => true, ]); + $this->hasMany('Products', [ + 'foreignKey' => 'product_category_id', + 'bindingKey' => 'internal_id', + 'className' => 'CakeProducts.Products', + 'dependent' => true, + 'cascadeCallbacks' => true, + ]); + $this->hasMany('ProductPhotos', [ + 'foreignKey' => 'product_category_id', + 'bindingKey' => 'internal_id', + 'className' => 'CakeProducts.ProductPhotos', + 'dependent' => true, + 'cascadeCallbacks' => true, + ]); + $this->hasOne('PrimaryProductPhotos', [ + 'foreignKey' => 'product_category_id', + 'bindingKey' => 'internal_id', + 'conditions' => ['PrimaryProductPhotos.primary_category_photo' => true], + 'className' => 'CakeProducts.ProductPhotos', + 'dependent' => true, + ]); $this->getSchema()->setColumnType('default_product_type_id', EnumType::from(ProductProductTypeId::class)); diff --git a/src/Model/Table/ProductPhotosTable.php b/src/Model/Table/ProductPhotosTable.php index 1fa8fb7..c7c2c22 100644 --- a/src/Model/Table/ProductPhotosTable.php +++ b/src/Model/Table/ProductPhotosTable.php @@ -61,10 +61,17 @@ class ProductPhotosTable extends Table $this->belongsTo('Products', [ 'foreignKey' => 'product_id', - 'joinType' => 'INNER', + 'joinType' => 'LEFT', 'className' => 'CakeProducts.Products', ]); + $this->belongsTo('ProductCategories', [ + 'foreignKey' => 'product_category_id', + 'bindingKey' => 'internal_id', + 'joinType' => 'LEFT', + 'className' => 'CakeProducts.ProductCategories', + ]); + $this->belongsTo('ProductSkus', [ 'foreignKey' => 'product_sku_id', 'joinType' => 'LEFT', @@ -82,12 +89,16 @@ class ProductPhotosTable extends Table { $validator ->uuid('product_id') - ->notEmptyString('product_id'); + ->allowEmptyString('product_id'); $validator ->uuid('product_sku_id') ->allowEmptyString('product_sku_id'); + $validator + ->uuid('product_category_id') + ->allowEmptyString('product_category_id'); + $validator ->scalar('photo_dir') ->maxLength('photo_dir', 255) @@ -130,6 +141,7 @@ class ProductPhotosTable extends Table { $rules->add($rules->existsIn(['product_id'], 'Products'), ['errorField' => 'product_id']); $rules->add($rules->existsIn(['product_sku_id'], 'ProductSkus'), ['errorField' => 'product_sku_id']); + $rules->add($rules->existsIn(['product_category_id'], 'ProductCategories'), ['errorField' => 'product_category_id']); return $rules; } diff --git a/tests/Fixture/ProductPhotosFixture.php b/tests/Fixture/ProductPhotosFixture.php index 3888f6a..ef59da7 100644 --- a/tests/Fixture/ProductPhotosFixture.php +++ b/tests/Fixture/ProductPhotosFixture.php @@ -22,9 +22,27 @@ class ProductPhotosFixture extends TestFixture 'id' => '2c386086-f4c5-4093-bea5-ee9c29479f58', 'product_id' => 'cfc98a9a-29b2-44c8-b587-8156adc05317', 'product_sku_id' => null, + 'product_category_id' => null, 'photo_dir' => 'cfc98a9a-29b2-44c8-b587-8156adc05317', 'photo_filename' => '2c386086-f4c5-4093-bea5-ee9c29479f58.png', 'primary_photo' => 1, + 'primary_category_photo' => 0, + 'photo_position' => 100, + 'enabled' => 1, + 'created' => '2025-08-10 04:32:10', + 'modified' => '2025-08-10 04:32:10', + 'deleted' => null, + ], + + [ + 'id' => '2c386086-f4c5-4093-bea5-ee9c29479f51', + 'product_id' => 'cfc98a9a-29b2-44c8-b587-8156adc05317', + 'product_sku_id' => null, + 'product_category_id' => '3c2377c5-b97c-4bc9-9660-8f77b4893d8b', + 'photo_dir' => 'categories', + 'photo_filename' => '2c386086-f4c5-4093-bea5-ee9c29479f51.png', + 'primary_photo' => 1, + 'primary_category_photo' => 1, 'photo_position' => 100, 'enabled' => 1, 'created' => '2025-08-10 04:32:10', diff --git a/tests/TestCase/Controller/ProductPhotosControllerTest.php b/tests/TestCase/Controller/ProductPhotosControllerTest.php index f46e561..bab9d6d 100644 --- a/tests/TestCase/Controller/ProductPhotosControllerTest.php +++ b/tests/TestCase/Controller/ProductPhotosControllerTest.php @@ -38,6 +38,7 @@ class ProductPhotosControllerTest extends BaseControllerTest 'plugin.CakeProducts.Products', 'plugin.CakeProducts.ProductSkus', 'plugin.CakeProducts.ProductPhotos', + 'plugin.CakeProducts.ProductCategories', ]; /** diff --git a/tests/TestCase/Model/Table/ProductCategoriesTableTest.php b/tests/TestCase/Model/Table/ProductCategoriesTableTest.php index 2c4beb2..aa4f96d 100644 --- a/tests/TestCase/Model/Table/ProductCategoriesTableTest.php +++ b/tests/TestCase/Model/Table/ProductCategoriesTableTest.php @@ -65,9 +65,11 @@ class ProductCategoriesTableTest extends TestCase 'ProductCatalogs', 'ParentProductCategories', 'ChildProductCategories', -// 'Products', + 'Products', 'ProductCategoryAttributes', 'ProductCategoryVariants', + 'ProductPhotos', + 'PrimaryProductPhotos', ]; $associations = $this->ProductCategories->associations();