Where's The Best Place For The Models

Hi guys! Happy New Year!

I have the Admin module at my application and within in I have for now two another modules. Each of them have bunch of models ( Article module has 5 models and Shop module has 10 models ). Now all these models placing at protected/models folder, so it looks pretty redundant. By the way, the module main principle is corrupted, because the modules become not portable as I have all the models separated at another directory.

I find out the most methods I use in model classes at admin panel I dont using at the site itself. I mean dozens of filters, validators etc. So should I really separate 'em at protected/models folder or I should place 'em at proper protected/modules/admin/modules/article/models and protected/modules/admin/modules/shop/models folders ?

I worry because I imagine if I have for example 10 modules then it might be 50 models at one folder.

Current application I am working on has 46 database tables (and growing), thus I have as many models in /protected/models/ I don’t see a problem at all with this.

I am also running an admin module, with its own controllers within the module directory, however I have decided to not have models here also. The question for me is: “Portable to where?”. For me running a module is for the convenience of keeping the controllers separate. I don’t mind at all if my models contain code used by the admin. At least it saves on a whole bunch of repetition (DRY). There are many things used by both back and front end.

Nobody can turn around and say "Oh! You are doing it wrong!"

The models belong on the catwalk! :)

I’m currently rewriting my apps to use the YiiBoilerplate code where it’s divided into frontend, backend and common.

I prefer that over using an admin module, especially since the backend is using Boostrap (YiiBooster) for quick and easy GUI where the frontend can use anything (for that special look). ;)

Shared models, components, etc goes in ‘common’.

Thanks for these advices… I’ll take them into account!

Following from a module oriented perspective all models should be inside a module that responsible for a required functionality. That way you wouldn’t have to worry about number of models. Also, separation between frontend/backend could be performed inside a module, so finally you’ll have all needed functionality (e.g. articles) in one place.

Not true at all.

Would you mind to clarify what is true then?

I already have: You can build you application as it suits you - whatever you judge to be the best for your own situation.

DRY - do you know what this is?

If my main application uses a table "People" and my module also uses the same table, do you really think its a good thing to follow the "rule" and have two "People" classes?

I’ve sad something against DRY?

You can use module models anywhere you need, so a solution for your example would be to have only one model People inside module Users. It gives clear understanding which module is responsible for which functionality.

From your point:

If it’s only a one reason for using modules, why you just don’t have sub directories in your main controllers directory?

check this link, it might help you :)