category photos should be working

This commit is contained in:
2025-10-12 01:04:36 -07:00
parent 66db31c7ad
commit 30aad1dea4
11 changed files with 105 additions and 9 deletions

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