flexible table classes in controllers for rest of tables

This commit is contained in:
2025-03-28 23:34:21 -07:00
parent 3f461cabb8
commit b868cc11fb
6 changed files with 164 additions and 53 deletions

View File

@@ -3,7 +3,10 @@ declare(strict_types=1);
namespace CakeProducts\Controller;
use Cake\Core\Configure;
use Cake\Log\Log;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use CakeProducts\Controller\AppController;
/**
@@ -13,6 +16,28 @@ use CakeProducts\Controller\AppController;
*/
class ProductsController extends AppController
{
/**
* @var Table|null
*/
protected ?Table $_table = null;
/**
* Gets the table instance
*
* @return Table
*/
public function getTable()
{
if ($this->_table instanceof Table) {
return $this->_table;
}
$this->_table = TableRegistry::getTableLocator()->get(
Configure::read('CakeProducts.Products.table', 'CakeProducts.Products')
);
return $this->_table;
}
/**
* Index method
*
@@ -20,7 +45,7 @@ class ProductsController extends AppController
*/
public function index()
{
$query = $this->Products->find()
$query = $this->getTable()->find()
->contain(['ProductCategories']);
$products = $this->paginate($query);
@@ -36,7 +61,7 @@ class ProductsController extends AppController
*/
public function view($id = null)
{
$product = $this->Products->get($id, contain: ['ProductCategories']);
$product = $this->getTable()->get($id, contain: ['ProductCategories']);
$this->set(compact('product'));
}
@@ -47,10 +72,11 @@ class ProductsController extends AppController
*/
public function add()
{
$product = $this->Products->newEmptyEntity();
$productsTable = $this->getTable();
$product = $productsTable->newEmptyEntity();
if ($this->request->is('post')) {
$product = $this->Products->patchEntity($product, $this->request->getData());
if ($this->Products->save($product)) {
$product = $productsTable->patchEntity($product, $this->request->getData());
if ($productsTable->save($product)) {
$this->Flash->success(__('The product has been saved.'));
return $this->redirect(['action' => 'index']);
@@ -59,7 +85,7 @@ class ProductsController extends AppController
Log::debug(print_r($product->getErrors(), true));
$this->Flash->error(__('The product could not be saved. Please, try again.'));
}
$productCategories = $this->Products->ProductCategories->find('list', keyField: 'internal_id', valueField: 'name' )->all();
$productCategories = $productsTable->ProductCategories->find('list', keyField: 'internal_id', valueField: 'name' )->all();
$this->set(compact('product', 'productCategories'));
}
@@ -72,10 +98,11 @@ class ProductsController extends AppController
*/
public function edit($id = null)
{
$product = $this->Products->get($id, contain: []);
$productsTable = $this->getTable();
$product = $productsTable->get($id, contain: []);
if ($this->request->is(['patch', 'post', 'put'])) {
$product = $this->Products->patchEntity($product, $this->request->getData());
if ($this->Products->save($product)) {
$product = $productsTable->patchEntity($product, $this->request->getData());
if ($productsTable->save($product)) {
$this->Flash->success(__('The product has been saved.'));
return $this->redirect(['action' => 'index']);
@@ -84,7 +111,7 @@ class ProductsController extends AppController
Log::debug(print_r($product->getErrors(), true));
$this->Flash->error(__('The product could not be saved. Please, try again.'));
}
$productCategories = $this->Products->ProductCategories->find('list', limit: 200)->all();
$productCategories = $productsTable->ProductCategories->find('list', limit: 200)->all();
$this->set(compact('product', 'productCategories'));
}
@@ -98,8 +125,10 @@ class ProductsController extends AppController
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$product = $this->Products->get($id);
if ($this->Products->delete($product)) {
$productsTable = $this->getTable();
$product = $productsTable->get($id);
if ($productsTable->delete($product)) {
$this->Flash->success(__('The product has been deleted.'));
} else {
$this->Flash->error(__('The product could not be deleted. Please, try again.'));