PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 9, 2022

[FIXED] Is there any posibility to put model classes in abstract sphere?

 January 09, 2022     cakephp, cakephp-3.0, php     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing