category photos should be working
CI / testsuite (mysql, 8.2, ) (push) Failing after 6m13s Details
CI / testsuite (mysql, 8.4, ) (push) Successful in 10m31s Details
CI / testsuite (sqlite, 8.2, prefer-lowest) (push) Failing after 5m54s Details
CI / Coding Standard & Static Analysis (push) Failing after 5m40s Details

This commit is contained in:
Brandon Shipley 2025-10-12 01:04:36 -07:00
parent 66db31c7ad
commit 30aad1dea4
Signed by: bmfs
GPG Key ID: 14E38571D8BB0DE4
11 changed files with 105 additions and 9 deletions

View File

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
use Migrations\BaseMigration;
class AddProductCategoryIdToProductPhotos 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_photos');
$table->addColumn('product_category_id', 'uuid', [
'default' => null,
'null' => true,
]);
$table->addColumn('primary_category_photo', 'boolean', [
'default' => false,
'null' => false,
]);
$table->update();
}
}

View File

@ -59,6 +59,7 @@ class ProductCategoriesController extends AppController
'ChildProductCategories',
'ProductCategoryAttributes',
'ProductCategoryAttributes.ProductCategoryAttributeOptions',
'PrimaryProductPhotos',
]);
$productCategoryAttributes = $this->getTable()->ProductCategoryAttributes->getAllCategoryAttributesForCategoryId($productCategory->internal_id);

View File

@ -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();

View File

@ -42,5 +42,6 @@ class Product extends Entity
'product_category' => false,
'product_attributes' => true,
'product_category_variants' => true,
'primary_product_photo' => true,
];
}

View File

@ -53,5 +53,6 @@ class ProductCategory extends Entity
'product_catalog' => true,
'parent_product_category' => true,
'child_product_categories' => true,
'primary_product_photo' => true,
];
}

View File

@ -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,
];
}

View File

@ -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));

View File

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

View File

@ -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',

View File

@ -38,6 +38,7 @@ class ProductPhotosControllerTest extends BaseControllerTest
'plugin.CakeProducts.Products',
'plugin.CakeProducts.ProductSkus',
'plugin.CakeProducts.ProductPhotos',
'plugin.CakeProducts.ProductCategories',
];
/**

View File

@ -65,9 +65,11 @@ class ProductCategoriesTableTest extends TestCase
'ProductCatalogs',
'ParentProductCategories',
'ChildProductCategories',
// 'Products',
'Products',
'ProductCategoryAttributes',
'ProductCategoryVariants',
'ProductPhotos',
'PrimaryProductPhotos',
];
$associations = $this->ProductCategories->associations();