diff --git a/src/Controller/ExternalProductCatalogsController.php b/src/Controller/ExternalProductCatalogsController.php index 04846f7..1ca09d2 100644 --- a/src/Controller/ExternalProductCatalogsController.php +++ b/src/Controller/ExternalProductCatalogsController.php @@ -8,6 +8,7 @@ use Cake\Log\Log; use Cake\ORM\Table; use Cake\ORM\TableRegistry; use CakeProducts\Controller\AppController; +use CakeProducts\Controller\Traits\OverrideTableTrait; /** * ExternalProductCatalogs Controller @@ -16,26 +17,16 @@ use CakeProducts\Controller\AppController; */ class ExternalProductCatalogsController extends AppController { - /** - * @var Table|null - */ - protected ?Table $_table = null; + use OverrideTableTrait; /** - * Gets the table instance - * - * @return Table + * @return void */ - public function getTable() + public function initialize(): void { - if ($this->_table instanceof Table) { - return $this->_table; - } - $this->_table = TableRegistry::getTableLocator()->get( - Configure::read('CakeProducts.ExternalProductCatalogs.table', 'CakeProducts.ExternalProductCatalogs') - ); - - return $this->_table; + parent::initialize(); // TODO: Change the autogenerated stub + $this->_defaultTable = 'CakeProducts.ExternalProductCatalogs'; + $this->_tableConfigKey = 'CakeProducts.ExternalProductCatalogs.table'; } /** diff --git a/src/Controller/ProductCatalogsController.php b/src/Controller/ProductCatalogsController.php index 3e14be5..2db3f6a 100644 --- a/src/Controller/ProductCatalogsController.php +++ b/src/Controller/ProductCatalogsController.php @@ -10,8 +10,8 @@ use Cake\Log\Log; use Cake\ORM\Table; use Cake\ORM\TableRegistry; use CakeProducts\Controller\AppController; +use CakeProducts\Controller\Traits\OverrideTableTrait; use CakeProducts\Model\Table\ProductCatalogsTable; -use CakeProducts\Service\CatalogManagerService; /** * ProductCatalogs Controller @@ -20,7 +20,17 @@ use CakeProducts\Service\CatalogManagerService; */ class ProductCatalogsController extends AppController { - protected ?Table $_table = null; + use OverrideTableTrait; + + /** + * @return void + */ + public function initialize(): void + { + parent::initialize(); // TODO: Change the autogenerated stub + $this->_defaultTable = 'CakeProducts.ProductCatalogs'; + $this->_tableConfigKey = 'CakeProducts.ProductCatalogs.table'; + } /** * Index method diff --git a/src/Controller/ProductCategoriesController.php b/src/Controller/ProductCategoriesController.php index 3fbdb0d..eaa4562 100644 --- a/src/Controller/ProductCategoriesController.php +++ b/src/Controller/ProductCategoriesController.php @@ -8,6 +8,7 @@ use Cake\Log\Log; use Cake\ORM\Table; use Cake\ORM\TableRegistry; use Cake\Utility\Text; +use CakeProducts\Controller\Traits\OverrideTableTrait; /** * ProductCategories Controller @@ -16,27 +17,18 @@ use Cake\Utility\Text; */ class ProductCategoriesController extends AppController { - /** - * @var Table|null - */ - protected ?Table $_table = null; + use OverrideTableTrait; /** - * Gets the table instance - * - * @return Table + * @return void */ - public function getTable() + public function initialize(): void { - if ($this->_table instanceof Table) { - return $this->_table; - } - $this->_table = TableRegistry::getTableLocator()->get( - Configure::read('CakeProducts.ProductCategories.table', 'CakeProducts.ProductCategories') - ); - - return $this->_table; + parent::initialize(); // TODO: Change the autogenerated stub + $this->_defaultTable = 'CakeProducts.ProductCategories'; + $this->_tableConfigKey = 'CakeProducts.ProductCategories.table'; } + /** * Index method * diff --git a/src/Controller/ProductCategoryAttributeOptionsController.php b/src/Controller/ProductCategoryAttributeOptionsController.php index e733cba..6db5b99 100644 --- a/src/Controller/ProductCategoryAttributeOptionsController.php +++ b/src/Controller/ProductCategoryAttributeOptionsController.php @@ -8,6 +8,7 @@ use Cake\Log\Log; use Cake\ORM\Table; use Cake\ORM\TableRegistry; use CakeProducts\Controller\AppController; +use CakeProducts\Controller\Traits\OverrideTableTrait; /** * ProductCategoryAttributeOptions Controller @@ -16,26 +17,16 @@ use CakeProducts\Controller\AppController; */ class ProductCategoryAttributeOptionsController extends AppController { - /** - * @var Table|null - */ - protected ?Table $_table = null; + use OverrideTableTrait; /** - * Gets the table instance - * - * @return Table + * @return void */ - public function getTable() + public function initialize(): void { - if ($this->_table instanceof Table) { - return $this->_table; - } - $this->_table = TableRegistry::getTableLocator()->get( - Configure::read('CakeProducts.ProductCategoryAttributeOptions.table', 'CakeProducts.ProductCategoryAttributeOptions') - ); - - return $this->_table; + parent::initialize(); // TODO: Change the autogenerated stub + $this->_defaultTable = 'CakeProducts.ProductCategoryAttributeOptions'; + $this->_tableConfigKey = 'CakeProducts.ProductCategoryAttributeOptions.table'; } /** diff --git a/src/Controller/ProductCategoryAttributesController.php b/src/Controller/ProductCategoryAttributesController.php index 6a90a6f..7795a37 100644 --- a/src/Controller/ProductCategoryAttributesController.php +++ b/src/Controller/ProductCategoryAttributesController.php @@ -10,9 +10,9 @@ use Cake\Log\Log; use Cake\ORM\Table; use Cake\ORM\TableRegistry; use CakeProducts\Controller\AppController; +use CakeProducts\Controller\Traits\OverrideTableTrait; use CakeProducts\Model\Enum\ProductCategoryAttributeTypeId; use CakeProducts\Model\Table\ProductCategoryAttributesTable; -use CakeProducts\Service\CatalogManagerService; /** * ProductCategoryAttributes Controller @@ -21,26 +21,16 @@ use CakeProducts\Service\CatalogManagerService; */ class ProductCategoryAttributesController extends AppController { - /** - * @var Table|null - */ - protected ?Table $_table = null; + use OverrideTableTrait; /** - * Gets the table instance - * - * @return Table + * @return void */ - public function getTable() + public function initialize(): void { - if ($this->_table instanceof Table) { - return $this->_table; - } - $this->_table = TableRegistry::getTableLocator()->get( - Configure::read('CakeProducts.ProductCategoryAttributes.table', 'CakeProducts.ProductCategoryAttributes') - ); - - return $this->_table; + parent::initialize(); // TODO: Change the autogenerated stub + $this->_defaultTable = 'CakeProducts.ProductCategoryAttributes'; + $this->_tableConfigKey = 'CakeProducts.ProductCategoryAttributes.table'; } /** diff --git a/src/Controller/ProductsController.php b/src/Controller/ProductsController.php index f026a52..496566f 100644 --- a/src/Controller/ProductsController.php +++ b/src/Controller/ProductsController.php @@ -8,6 +8,7 @@ use Cake\Log\Log; use Cake\ORM\Table; use Cake\ORM\TableRegistry; use CakeProducts\Controller\AppController; +use CakeProducts\Controller\Traits\OverrideTableTrait; /** * Products Controller @@ -16,26 +17,16 @@ use CakeProducts\Controller\AppController; */ class ProductsController extends AppController { - /** - * @var Table|null - */ - protected ?Table $_table = null; + use OverrideTableTrait; /** - * Gets the table instance - * - * @return Table + * @return void */ - public function getTable() + public function initialize(): void { - if ($this->_table instanceof Table) { - return $this->_table; - } - $this->_table = TableRegistry::getTableLocator()->get( - Configure::read('CakeProducts.Products.table', 'CakeProducts.Products') - ); - - return $this->_table; + parent::initialize(); // TODO: Change the autogenerated stub + $this->_defaultTable = 'CakeProducts.Products'; + $this->_tableConfigKey = 'CakeProducts.Products.table'; } /** diff --git a/src/Controller/Traits/OverrideTableTrait.php b/src/Controller/Traits/OverrideTableTrait.php new file mode 100644 index 0000000..1ff86ec --- /dev/null +++ b/src/Controller/Traits/OverrideTableTrait.php @@ -0,0 +1,55 @@ +_table instanceof Table) { + return $this->_table; + } + $table = $tableName; + if (!isset($table)) { + $table = $this->_tableConfigKey && Configure::read($this->_tableConfigKey) ? Configure::read($this->_tableConfigKey) : $this->_defaultTable; + } + $this->_table = TableRegistry::getTableLocator()->get($table); + + return $this->_table; + } + + /** + * Set the users table + * + * @param Table $table table + * @return void + */ + public function setTable(Table $table) + { + $this->_table = $table; + } +} \ No newline at end of file