creating sub-folder in Model

Hello, Just started Yii 4 days ago, im a very good fan of zend and i wish to use some of it conventions.

Please can i create sub folders in my model folder so that i can sort out my model classes for easy of accessibility and usage?

You need to add some more import paths to your config:




'import'=>array(

        'application.models.*',

	'application.models.forms.*',



If you have so many models, then you probably need to create a module (e.g. NewsModule with news-related models, etc.).

@andy_s:

Do you allow me to replace “need” with “can” in your statement? :)

I actually never use any modules in my applications unless i would be really sure i have to reuse this module in another application. Mostly i don’t. I see no reason to make things more complicated than really required. Modules introduce another level of complexity that could simply be avoided in many applications. Having different controllers for different areas of an application is enough separation for me. So: creating another subfolder for models can also be a very viable solution.

It’s also matter of opinion, of course.

thanks very much. it worked. but for best practice which is preferred

  1. adding the sub-folder in my main.php configuration.

or

  1. creating Modules

Modules are used when there are some models, view and controller that are supposed to work toghether, like user module.

If you have just a lot of models and all contollers will use more or less all of them, you cannot create a model, so you have to use sub-folders.

It looks like we solve different problems :) I have some modules (news, articles, etc.) and I always reuse them in my projects with minor changes.

In this case modules make perfect sense then, of course. :)

I just want to avoid that newbies get the impression, you always should use modules. IMO you really only should create a module, if you know, you’ll reuse something. Otherwhise i would suggest to avoid them for the sake of simplicity.

That’s where Yii might have a little different understanding of what a module is for. Some other frameworks use modules for everything, whereas in Yii it’s enough to create controllers to get good separation. In Yii a module is more like a sub-application that you can plug into other apps easily.

Just a simple question about Yii but interesting talks. For me I always create a sub-folder models/base and generate module class with Yii gen tool into that folder. For a table tbl_articles I will generate model models/base/ArticleBase and then create models/Article extends ArticleBase. This way helps me to regenerate to base class anytime I want.

thanks every one for all the effort. im grateful

I need more of this kind of conversation… thank you all