|
|||
|
I'm interested to find out how everyone else is loading / naming their model classes?
There's a distinct lack of 'magic' in the framework for handling this. It seems you have to do one of the following. 1 - Add your model directory(s) to the include path. 2 - include/require your model class before using it. I'm using register autoload in my app so really want to avoid having to manually include model class files, but I also don't really like having to add my model directories to the include path, because I'm using modules and have many model directories. I'm almost tempted to just put them all in the library folder. I've considered writing my own loader class and registering it with Zend_Load but surely there's a better way? |
|
|||
|
That sounds good. So you have a base dir, and you wrote a function to scan all children dirs recursively and if one matches the name 'models', it's added to the include path?
That definitely works for me. One other thing though, do you have any issue with giving your models names without a suffix or prefix? Shouldn't there be a better way than just new Nodes() for example? I like how controllers have a naming convention of NameController. I guess I should just use Model as a suffix and deal with the lack of magic. Ideally I'd like to have... (default module) NodeModel.php class NodeModel extends Zend_Db_Table_Abstract {} (news module) News_ArticleModel.php class News_ArticleModel If we kept things too generic there may be clashes of class names? Particularly amongst different modules if the idea is to make modules able to be placed in different applications. |
|
||||
|
Quote:
PHP Code:
Quote:
The only conflict you might have is that an external lib is using the same classname. But using a suffix of Model isn't a bad idea anyway! |
|
|||
|
I'd be interested in some code. Thanks.
|
|
||||
|
The plugin:
PHP Code:
PHP Code:
|
|
|||
|
Thanks, that code looks very interesting.
Last night I actually did something myself, completely different way though. I made my own loading class that extends Zend_Loader. PHP Code:
PHP Code:
For example inside my IndexController I can do... $news = new NewsModel(); It will automatically search for NewsModel.php in /application/models/ If I want to use modules, I can just add the module name to the start. $news = new BlogNewsModel(); It will automatically search for BlogNewsModel.php in /application/modules/blog/models/ It's obviously based on camel casing. This is a working draft that could be improved upon by handling directories better. Note I used camel casing so as not to conflict with Zend's use of underscores to match directories. Interested in feedback. |
|
||||
|
I"d go ahead an put it in line with Zend's naming convention so you don't have to follow two conventions. There shouldn't be a conflict and if there is you should get your class to play nice with the ZF conventions. Just my two cents tho...
__________________
Zend Framework Resources: Zend Webinars | Reference Manual | API Docs | Books | FreeNode: #zftalk Getting Started Tutorials: Getting started with ZF | Getting started with Zend Auth |
|
|||
|
You're right. I only noticed yesterday that Zend treats module controllers as Module_NamedController
Even though that obviously doesn't map to /modules/module/controllers/NamedController.php So I'll look at how they've done that, and apply the same to models and forms. Thanks. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|