diff --git a/src/Controller/ProductCategoryVariantsController.php b/src/Controller/ProductCategoryVariantsController.php index 8e2e9eb..d48259e 100644 --- a/src/Controller/ProductCategoryVariantsController.php +++ b/src/Controller/ProductCategoryVariantsController.php @@ -4,6 +4,10 @@ declare(strict_types=1); namespace CakeProducts\Controller; use App\Controller\AppController; +use Cake\Core\Configure; +use Cake\ORM\Table; +use Cake\ORM\TableRegistry; +use CheeseCake\Controller\Traits\OverrideTableTrait; /** * ProductCategoryVariants Controller @@ -12,6 +16,18 @@ use App\Controller\AppController; */ class ProductCategoryVariantsController extends AppController { + use OverrideTableTrait; + + /** + * @return void + */ + public function initialize(): void + { + parent::initialize(); // TODO: Change the autogenerated stub +// $this->_defaultTable = 'CakeProducts.ProductCategoryVariants'; +// $this->_tableConfigKey = 'CakeProducts.ProductCategoryVariants.table'; + } + /** * Index method * @@ -19,7 +35,7 @@ class ProductCategoryVariantsController extends AppController */ public function index() { - $query = $this->ProductCategoryVariants->find() + $query = $this->getTable()->find() ->contain(['ProductCategories', 'Products']); $productCategoryVariants = $this->paginate($query); @@ -35,7 +51,7 @@ class ProductCategoryVariantsController extends AppController */ public function view($id = null) { - $productCategoryVariant = $this->ProductCategoryVariants->get($id, contain: ['ProductCategories', 'Products']); + $productCategoryVariant = $this->getTable()->get($id, contain: ['ProductCategories', 'Products']); $this->set(compact('productCategoryVariant')); } @@ -46,18 +62,20 @@ class ProductCategoryVariantsController extends AppController */ public function add() { - $productCategoryVariant = $this->ProductCategoryVariants->newEmptyEntity(); + $productCategoryVariantsTable = $this->getTable(); + + $productCategoryVariant = $productCategoryVariantsTable->newEmptyEntity(); if ($this->request->is('post')) { - $productCategoryVariant = $this->ProductCategoryVariants->patchEntity($productCategoryVariant, $this->request->getData()); - if ($this->ProductCategoryVariants->save($productCategoryVariant)) { + $productCategoryVariant = $productCategoryVariantsTable->patchEntity($productCategoryVariant, $this->request->getData()); + if ($productCategoryVariantsTable->save($productCategoryVariant)) { $this->Flash->success(__('The product category variant has been saved.')); return $this->redirect(['action' => 'index']); } $this->Flash->error(__('The product category variant could not be saved. Please, try again.')); } - $productCategories = $this->ProductCategoryVariants->ProductCategories->find('list', limit: 200)->all(); - $products = $this->ProductCategoryVariants->Products->find('list', limit: 200)->all(); + $productCategories = $productCategoryVariantsTable->ProductCategories->find('list')->all(); + $products = $productCategoryVariantsTable->Products->find('list')->all(); $this->set(compact('productCategoryVariant', 'productCategories', 'products')); } @@ -70,18 +88,19 @@ class ProductCategoryVariantsController extends AppController */ public function edit($id = null) { - $productCategoryVariant = $this->ProductCategoryVariants->get($id, contain: []); + $productCategoryVariantsTable = $this->getTable(); + $productCategoryVariant = $productCategoryVariantsTable->get($id, contain: []); if ($this->request->is(['patch', 'post', 'put'])) { - $productCategoryVariant = $this->ProductCategoryVariants->patchEntity($productCategoryVariant, $this->request->getData()); - if ($this->ProductCategoryVariants->save($productCategoryVariant)) { + $productCategoryVariant = $productCategoryVariantsTable->patchEntity($productCategoryVariant, $this->request->getData()); + if ($productCategoryVariantsTable->save($productCategoryVariant)) { $this->Flash->success(__('The product category variant has been saved.')); return $this->redirect(['action' => 'index']); } $this->Flash->error(__('The product category variant could not be saved. Please, try again.')); } - $productCategories = $this->ProductCategoryVariants->ProductCategories->find('list', limit: 200)->all(); - $products = $this->ProductCategoryVariants->Products->find('list', limit: 200)->all(); + $productCategories = $productCategoryVariantsTable->ProductCategories->find('list', limit: 200)->all(); + $products = $productCategoryVariantsTable->Products->find('list', limit: 200)->all(); $this->set(compact('productCategoryVariant', 'productCategories', 'products')); } @@ -95,8 +114,10 @@ class ProductCategoryVariantsController extends AppController public function delete($id = null) { $this->request->allowMethod(['post', 'delete']); - $productCategoryVariant = $this->ProductCategoryVariants->get($id); - if ($this->ProductCategoryVariants->delete($productCategoryVariant)) { + $productCategoryVariantsTable = $this->getTable(); + + $productCategoryVariant = $productCategoryVariantsTable->get($id); + if ($productCategoryVariantsTable->delete($productCategoryVariant)) { $this->Flash->success(__('The product category variant has been deleted.')); } else { $this->Flash->error(__('The product category variant could not be deleted. Please, try again.')); @@ -104,4 +125,21 @@ class ProductCategoryVariantsController extends AppController return $this->redirect(['action' => 'index']); } + + /** + * Gets the users table instance + * + * @return Table + */ + public function getTable() + { + if ($this->_table instanceof Table) { + return $this->_table; + } + $this->_table = TableRegistry::getTableLocator()->get( + Configure::read('CakeProducts.ProductCategoryVariants.table', 'CakeProducts.ProductCategoryVariants') + ); + + return $this->_table; + } }