From 803e161a9aa3948bc779f2ff8660a861aaa01063 Mon Sep 17 00:00:00 2001 From: Brandon Shipley Date: Thu, 21 Aug 2025 23:40:00 -0700 Subject: [PATCH] get product skus table via product photos in product photos controller --- src/Controller/ProductPhotosController.php | 4 ++-- src/Model/Table/ProductPhotosTable.php | 1 + src/Model/Table/ProductsTable.php | 7 +++++++ tests/TestCase/Controller/ProductPhotosControllerTest.php | 4 +++- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Controller/ProductPhotosController.php b/src/Controller/ProductPhotosController.php index 48eb315..bcc65d4 100644 --- a/src/Controller/ProductPhotosController.php +++ b/src/Controller/ProductPhotosController.php @@ -104,7 +104,7 @@ class ProductPhotosController extends AppController $this->Flash->error(__('The product photo could not be saved. Please, try again.')); } $products = $productPhotosTable->Products->find('list', limit: 200)->all(); - $productSkus = $productPhotosTable->Products->ProductSkus->find('list', limit: 200)->all(); + $productSkus = $productPhotosTable->ProductSkus->find('list', limit: 200)->all(); $this->set(compact('productPhoto', 'products', 'productSkus')); } @@ -132,7 +132,7 @@ class ProductPhotosController extends AppController $this->Flash->error(__('The product photo could not be saved. Please, try again.')); } $products = $productPhotosTable->Products->find('list', limit: 200)->all(); - $productSkus = $productPhotosTable->Products->ProductSkus->find('list', limit: 200)->all(); + $productSkus = $productPhotosTable->ProductSkus->find('list', limit: 200)->all(); $this->set(compact('productPhoto', 'products', 'productSkus')); } diff --git a/src/Model/Table/ProductPhotosTable.php b/src/Model/Table/ProductPhotosTable.php index 57c5b40..333067b 100644 --- a/src/Model/Table/ProductPhotosTable.php +++ b/src/Model/Table/ProductPhotosTable.php @@ -18,6 +18,7 @@ use Psr\SimpleCache\CacheInterface; * ProductPhotos Model * * @property ProductsTable&BelongsTo $Products + * @property ProductSkusTable&BelongsTo $ProductSkus * * @method ProductPhoto newEmptyEntity() * @method ProductPhoto newEntity(array $data, array $options = []) diff --git a/src/Model/Table/ProductsTable.php b/src/Model/Table/ProductsTable.php index fdf80cb..8404fe7 100644 --- a/src/Model/Table/ProductsTable.php +++ b/src/Model/Table/ProductsTable.php @@ -71,6 +71,13 @@ class ProductsTable extends Table 'cascadeCallbacks' => true, ]); +// $this->hasMany('ProductSkus', [ +// 'foreignKey' => 'product_id', +// 'className' => 'CakeProducts.ProductSkus', +// 'dependent' => true, +// 'cascadeCallbacks' => true, +// ]); + $this->getSchema()->setColumnType('product_type_id', EnumType::from(ProductProductTypeId::class)); $this->addBehavior('Muffin/Trash.Trash'); diff --git a/tests/TestCase/Controller/ProductPhotosControllerTest.php b/tests/TestCase/Controller/ProductPhotosControllerTest.php index ff39884..dd852e0 100644 --- a/tests/TestCase/Controller/ProductPhotosControllerTest.php +++ b/tests/TestCase/Controller/ProductPhotosControllerTest.php @@ -7,6 +7,7 @@ use Cake\Core\Configure; use Cake\ORM\Table; use CakeProducts\Controller\ProductPhotosController; use CakeProducts\Model\Table\ProductPhotosTable; +use CakeProducts\Model\Table\ProductsTable; use FilesystemIterator; use Laminas\Diactoros\UploadedFile; use PHPUnit\Exception; @@ -47,7 +48,8 @@ class ProductPhotosControllerTest extends BaseControllerTest protected function setUp(): void { parent::setUp(); - $this->ProductPhotos = $this->getTableLocator()->get('ProductPhotos'); + $config = $this->getTableLocator()->exists('ProductPhotos') ? [] : ['className' => ProductPhotosTable::class]; + $this->ProductPhotos = $this->getTableLocator()->get('ProductPhotos', $config); } /**