Issue
I want to put the autogenerated part of model into abstract sphere, for example if i generate model for table users
, \Model\Entity\User
and \Model\Table\UsersTable
, I dont want to write code in these classes, i want to create a implementation class, for example \Model\Table\UserTableImpl
which extends \Model\Table\UsersTable
, and write a queries code in there, so my question is:
Is there any posibility to make these implementation classes autoload with controller, in the way that loadModel
work?
Thanks for any help.
Solution
You could simply rename the generated classes (eg AbstractUsersTable
), mark them as abstract, and then create implementations with names that follow the CakePHP naming conventions (eg UsersTable extends AbstractUsersTable
), that way you don't have to deal with further configuration for the table locator, your associations, etc.
That being said, you can always use fully qualified classnames to load table classes, to configure classes for associations, to set a controllers default table class, etc.
// load model
$this->loadModel(\App\Model\Table\UnconventionalClassName::class);
// set default controller table class
$this->_setModelClass(\App\Model\Table\UnconventionalClassName::class);
// configure association class
$this->associationType('Alias', [
'className' => \App\Model\Table\UnconventionalClassName::class
]);
// set tables entity class
$this->setEntityClass(\App\Model\Entity\UnconventionalClassName::class);
...
You could even go further and create custom locators and associations that change the default Table
suffix to something different, but I'd rather go with method number 1 and follow the conventions.
Answered By - ndm
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.