category photos should be working
This commit is contained in:
parent
66db31c7ad
commit
30aad1dea4
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -59,6 +59,7 @@ class ProductCategoriesController extends AppController
|
||||||
'ChildProductCategories',
|
'ChildProductCategories',
|
||||||
'ProductCategoryAttributes',
|
'ProductCategoryAttributes',
|
||||||
'ProductCategoryAttributes.ProductCategoryAttributeOptions',
|
'ProductCategoryAttributes.ProductCategoryAttributeOptions',
|
||||||
|
'PrimaryProductPhotos',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$productCategoryAttributes = $this->getTable()->ProductCategoryAttributes->getAllCategoryAttributesForCategoryId($productCategory->internal_id);
|
$productCategoryAttributes = $this->getTable()->ProductCategoryAttributes->getAllCategoryAttributesForCategoryId($productCategory->internal_id);
|
||||||
|
|
|
@ -45,7 +45,7 @@ class ProductPhotosController extends AppController
|
||||||
*/
|
*/
|
||||||
public function view($id = null)
|
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'));
|
$this->set(compact('productPhoto'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,6 +94,7 @@ class ProductPhotosController extends AppController
|
||||||
if (!file_exists($destination)) {
|
if (!file_exists($destination)) {
|
||||||
throw new ForbiddenException('Failed to move the uploaded image to the appropriate folder. Please try again.');
|
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_dir'] = $path;
|
||||||
$postData['photo_filename'] = $uuid;
|
$postData['photo_filename'] = $uuid;
|
||||||
$productPhoto = $productPhotosTable->patchEntity($productPhoto, $postData);
|
$productPhoto = $productPhotosTable->patchEntity($productPhoto, $postData);
|
||||||
|
@ -102,6 +103,8 @@ class ProductPhotosController extends AppController
|
||||||
|
|
||||||
return $this->redirect(['action' => 'index']);
|
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.'));
|
$this->Flash->error(__('The product photo could not be saved. Please, try again.'));
|
||||||
}
|
}
|
||||||
$products = $productPhotosTable->Products->find('list', limit: 200)->all();
|
$products = $productPhotosTable->Products->find('list', limit: 200)->all();
|
||||||
|
|
|
@ -42,5 +42,6 @@ class Product extends Entity
|
||||||
'product_category' => false,
|
'product_category' => false,
|
||||||
'product_attributes' => true,
|
'product_attributes' => true,
|
||||||
'product_category_variants' => true,
|
'product_category_variants' => true,
|
||||||
|
'primary_product_photo' => true,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,5 +53,6 @@ class ProductCategory extends Entity
|
||||||
'product_catalog' => true,
|
'product_catalog' => true,
|
||||||
'parent_product_category' => true,
|
'parent_product_category' => true,
|
||||||
'child_product_categories' => true,
|
'child_product_categories' => true,
|
||||||
|
'primary_product_photo' => true,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace CakeProducts\Model\Entity;
|
namespace CakeProducts\Model\Entity;
|
||||||
|
|
||||||
|
use Cake\I18n\DateTime;
|
||||||
use Cake\ORM\Entity;
|
use Cake\ORM\Entity;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -10,17 +11,21 @@ use Cake\ORM\Entity;
|
||||||
*
|
*
|
||||||
* @property string $id
|
* @property string $id
|
||||||
* @property string $product_id
|
* @property string $product_id
|
||||||
|
* @property string|null $product_category_id
|
||||||
* @property string|null $product_sku_id
|
* @property string|null $product_sku_id
|
||||||
* @property string $photo_dir
|
* @property string $photo_dir
|
||||||
* @property string $photo_filename
|
* @property string $photo_filename
|
||||||
* @property bool $primary_photo
|
* @property bool $primary_photo
|
||||||
|
* @property bool $primary_category_photo
|
||||||
* @property int $photo_position
|
* @property int $photo_position
|
||||||
* @property bool $enabled
|
* @property bool $enabled
|
||||||
* @property \Cake\I18n\DateTime $created
|
* @property DateTime $created
|
||||||
* @property \Cake\I18n\DateTime|null $modified
|
* @property DateTime|null $modified
|
||||||
* @property \Cake\I18n\DateTime|null $deleted
|
* @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
|
class ProductPhoto extends Entity
|
||||||
{
|
{
|
||||||
|
@ -36,16 +41,18 @@ class ProductPhoto extends Entity
|
||||||
protected array $_accessible = [
|
protected array $_accessible = [
|
||||||
'product_id' => true,
|
'product_id' => true,
|
||||||
'product_sku_id' => true,
|
'product_sku_id' => true,
|
||||||
|
'product_category_id' => true,
|
||||||
'photo_dir' => true,
|
'photo_dir' => true,
|
||||||
'photo_filename' => true,
|
'photo_filename' => true,
|
||||||
'primary_photo' => true,
|
'primary_photo' => true,
|
||||||
|
'primary_category_photo' => true,
|
||||||
'photo_position' => true,
|
'photo_position' => true,
|
||||||
'enabled' => true,
|
'enabled' => true,
|
||||||
'created' => true,
|
'created' => true,
|
||||||
'modified' => true,
|
'modified' => true,
|
||||||
'deleted' => true,
|
'deleted' => true,
|
||||||
|
|
||||||
|
// entities
|
||||||
'product' => true,
|
'product' => true,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,6 +100,27 @@ class ProductCategoriesTable extends Table
|
||||||
'dependent' => true,
|
'dependent' => true,
|
||||||
'cascadeCallbacks' => 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));
|
$this->getSchema()->setColumnType('default_product_type_id', EnumType::from(ProductProductTypeId::class));
|
||||||
|
|
||||||
|
|
|
@ -61,10 +61,17 @@ class ProductPhotosTable extends Table
|
||||||
|
|
||||||
$this->belongsTo('Products', [
|
$this->belongsTo('Products', [
|
||||||
'foreignKey' => 'product_id',
|
'foreignKey' => 'product_id',
|
||||||
'joinType' => 'INNER',
|
'joinType' => 'LEFT',
|
||||||
'className' => 'CakeProducts.Products',
|
'className' => 'CakeProducts.Products',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$this->belongsTo('ProductCategories', [
|
||||||
|
'foreignKey' => 'product_category_id',
|
||||||
|
'bindingKey' => 'internal_id',
|
||||||
|
'joinType' => 'LEFT',
|
||||||
|
'className' => 'CakeProducts.ProductCategories',
|
||||||
|
]);
|
||||||
|
|
||||||
$this->belongsTo('ProductSkus', [
|
$this->belongsTo('ProductSkus', [
|
||||||
'foreignKey' => 'product_sku_id',
|
'foreignKey' => 'product_sku_id',
|
||||||
'joinType' => 'LEFT',
|
'joinType' => 'LEFT',
|
||||||
|
@ -82,12 +89,16 @@ class ProductPhotosTable extends Table
|
||||||
{
|
{
|
||||||
$validator
|
$validator
|
||||||
->uuid('product_id')
|
->uuid('product_id')
|
||||||
->notEmptyString('product_id');
|
->allowEmptyString('product_id');
|
||||||
|
|
||||||
$validator
|
$validator
|
||||||
->uuid('product_sku_id')
|
->uuid('product_sku_id')
|
||||||
->allowEmptyString('product_sku_id');
|
->allowEmptyString('product_sku_id');
|
||||||
|
|
||||||
|
$validator
|
||||||
|
->uuid('product_category_id')
|
||||||
|
->allowEmptyString('product_category_id');
|
||||||
|
|
||||||
$validator
|
$validator
|
||||||
->scalar('photo_dir')
|
->scalar('photo_dir')
|
||||||
->maxLength('photo_dir', 255)
|
->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_id'], 'Products'), ['errorField' => 'product_id']);
|
||||||
$rules->add($rules->existsIn(['product_sku_id'], 'ProductSkus'), ['errorField' => 'product_sku_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;
|
return $rules;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,9 +22,27 @@ class ProductPhotosFixture extends TestFixture
|
||||||
'id' => '2c386086-f4c5-4093-bea5-ee9c29479f58',
|
'id' => '2c386086-f4c5-4093-bea5-ee9c29479f58',
|
||||||
'product_id' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
'product_id' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
||||||
'product_sku_id' => null,
|
'product_sku_id' => null,
|
||||||
|
'product_category_id' => null,
|
||||||
'photo_dir' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
'photo_dir' => 'cfc98a9a-29b2-44c8-b587-8156adc05317',
|
||||||
'photo_filename' => '2c386086-f4c5-4093-bea5-ee9c29479f58.png',
|
'photo_filename' => '2c386086-f4c5-4093-bea5-ee9c29479f58.png',
|
||||||
'primary_photo' => 1,
|
'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,
|
'photo_position' => 100,
|
||||||
'enabled' => 1,
|
'enabled' => 1,
|
||||||
'created' => '2025-08-10 04:32:10',
|
'created' => '2025-08-10 04:32:10',
|
||||||
|
|
|
@ -38,6 +38,7 @@ class ProductPhotosControllerTest extends BaseControllerTest
|
||||||
'plugin.CakeProducts.Products',
|
'plugin.CakeProducts.Products',
|
||||||
'plugin.CakeProducts.ProductSkus',
|
'plugin.CakeProducts.ProductSkus',
|
||||||
'plugin.CakeProducts.ProductPhotos',
|
'plugin.CakeProducts.ProductPhotos',
|
||||||
|
'plugin.CakeProducts.ProductCategories',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -65,9 +65,11 @@ class ProductCategoriesTableTest extends TestCase
|
||||||
'ProductCatalogs',
|
'ProductCatalogs',
|
||||||
'ParentProductCategories',
|
'ParentProductCategories',
|
||||||
'ChildProductCategories',
|
'ChildProductCategories',
|
||||||
// 'Products',
|
'Products',
|
||||||
'ProductCategoryAttributes',
|
'ProductCategoryAttributes',
|
||||||
'ProductCategoryVariants',
|
'ProductCategoryVariants',
|
||||||
|
'ProductPhotos',
|
||||||
|
'PrimaryProductPhotos',
|
||||||
];
|
];
|
||||||
$associations = $this->ProductCategories->associations();
|
$associations = $this->ProductCategories->associations();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue