allow for extending the table class more easily - catalogs & categories

This commit is contained in:
2025-03-28 02:01:53 -07:00
parent 9e5df45f59
commit 3f45e46f31
2 changed files with 71 additions and 25 deletions

View File

@@ -7,6 +7,8 @@ use Cake\Core\Configure;
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Http\Response;
use Cake\Log\Log;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use CakeProducts\Controller\AppController;
use CakeProducts\Model\Table\ProductCatalogsTable;
use CakeProducts\Service\CatalogManagerService;
@@ -18,6 +20,8 @@ use CakeProducts\Service\CatalogManagerService;
*/
class ProductCatalogsController extends AppController
{
protected ?Table $_productCatalogsTable = null;
/**
* Index method
*
@@ -25,7 +29,7 @@ class ProductCatalogsController extends AppController
*/
public function index()
{
$query = $this->ProductCatalogs->find();
$query = $this->getProductCatalogsTable()->find();
$productCatalogs = $this->paginate($query);
$this->set(compact('productCatalogs'));
@@ -44,7 +48,7 @@ class ProductCatalogsController extends AppController
if (Configure::read('CakeProducts.internal.syncExternally', false)) {
$contain[] = 'ExternalProductCatalogs';
}
$productCatalog = $this->ProductCatalogs->get($id, contain: $contain);
$productCatalog = $this->getProductCatalogsTable()->get($id, contain: $contain);
$this->set(compact('productCatalog'));
}
@@ -55,10 +59,11 @@ class ProductCatalogsController extends AppController
*/
public function add()
{
$productCatalog = $this->ProductCatalogs->newEmptyEntity();
$productCatalogsTable = $this->getProductCatalogsTable();
$productCatalog = $productCatalogsTable->newEmptyEntity();
if ($this->request->is('post')) {
$productCatalog = $this->ProductCatalogs->patchEntity($productCatalog, $this->request->getData());
if ($this->ProductCatalogs->save($productCatalog)) {
$productCatalog = $productCatalogsTable->patchEntity($productCatalog, $this->request->getData());
if ($productCatalogsTable->save($productCatalog)) {
$this->Flash->success(__('The product catalog has been saved.'));
return $this->redirect(['action' => 'index']);
@@ -81,10 +86,11 @@ class ProductCatalogsController extends AppController
*/
public function edit($id = null)
{
$productCatalog = $this->ProductCatalogs->get($id, contain: []);
$productCatalogsTable = $this->getProductCatalogsTable();
$productCatalog = $productCatalogsTable->get($id, contain: []);
if ($this->request->is(['patch', 'post', 'put'])) {
$productCatalog = $this->ProductCatalogs->patchEntity($productCatalog, $this->request->getData());
if ($this->ProductCatalogs->save($productCatalog)) {
$productCatalog = $productCatalogsTable->patchEntity($productCatalog, $this->request->getData());
if ($productCatalogsTable->save($productCatalog)) {
$this->Flash->success(__('The product catalog has been saved.'));
return $this->redirect(['action' => 'index']);
@@ -104,8 +110,9 @@ class ProductCatalogsController extends AppController
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$productCatalog = $this->ProductCatalogs->get($id);
if ($this->ProductCatalogs->delete($productCatalog)) {
$productCatalogsTable = $this->getProductCatalogsTable();
$productCatalog = $productCatalogsTable->get($id);
if ($productCatalogsTable->delete($productCatalog)) {
$this->Flash->success(__('The product catalog has been deleted.'));
} else {
$this->Flash->error(__('The product catalog could not be deleted. Please, try again.'));
@@ -113,4 +120,19 @@ class ProductCatalogsController extends AppController
return $this->redirect(['action' => 'index']);
}
/**
* Gets the users table instance
*
* @return Table
*/
public function getProductCatalogsTable()
{
if ($this->_productCatalogsTable instanceof Table) {
return $this->_productCatalogsTable;
}
$this->_productCatalogsTable = TableRegistry::getTableLocator()->get(Configure::read('ProductCatalogs.table', 'CakeProducts.ProductCatalogs'));
return $this->_productCatalogsTable;
}
}