Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

martinym's avatar

3 category tables with same structure, how to manager them with one Controller, Model, and view etc at admin?

same structure:

categories for posts

categories2 for businesses

categories3 for profile

How to manager(add, edit) them with one Controller, Model, and view etc at admin? thanks for any help

Is it possible to change table name with one Controller, Model, and view etc at admin?

Like ---------------- PHP code

switch (expression ) {

case categories1:

$table = 'categories1' ;

break;

case categories2:

$table = 'categories2' ;

break;

case categories3:

$table = 'categories3' ;

break;

.........

default;

$table = 'categories' ;

}

0 likes
8 replies
design_studio's avatar

Not sure what you mean exactly since you didn't gave much data on what exactly yon need help with, but it looks like what you need is a Polymorphic relationship. Check the docs to see if this is what you wanted.

martinym's avatar

Thanks for your reply and I have add more info above

robrogers3's avatar

Give us a use case on how to manage them.

Like when viewing Business1 you want to manage the business categories only?

and similar posts etc. ?

So do you mean in say one controller one method you want to be able to manage the categories for the associated 'category' being handled?

and also, no that switch is not what you want. or at least I'm not sure why you'd want it. You want to figure out which Model you are working with right?

One thing to remember is that if you have small controllers: BusinessCategoryController.php PostCategoryController.php ...

Then it's really not a big deal to manage the code. It makes it simplier as your rules grow.

Then in the background you could have a Repository class or something that provides the implementation details for any category type regardless of type

Give us an example of what you mean by manage?

ps. yeah, one model will do. I don't know about views. probably at least some with some partials for different aspects.

1 like
Robstar's avatar

Firstly, you have a bad database structure :)

As for your issue (caused by a bad database structure) you could create a factory class that returns a model instance based upon the provided table name.

I personally just have a Polymorphic type setup so categories can be shared across models. Still, that may get a little more complicated depending upon how (or if)you store hierarchy data (i.e. parents/children etc.)

martinym's avatar

Thanks for all of reply and so that I have to code repeatedly for admin at back

categories add, edit

categories2 add, edit

categoriese3 add, edit

... so on.

martinym's avatar

@Robstar Thanks for your help, now I understood the

each controller corresponds to one table only.

robrogers3's avatar

no. it doesn't the controller should JUST process the request and return a response.

ideally you delegate to other classes and methods to do the actual work.

Please or to participate in this conversation.