phpzend-frameworkmodelszend-autoloader

zend framework models location and autoloading


I've started to document myself regarding Zend Framework as I will soon start using it in production. Everything was working fine, until I started to use and work with models :).

The default location for models, based on Zend recommendations, is application/models/DbTable, where all the models will be thrown in. This location forces me to name a model like Application_Model_DbTable_Actors. For me, this is a very long name for a model and not a easy to use one.

The directory structure that I want to obtain looks something like this:

application/
  models/
    actors/
      ActorsMapper.php
      Actor.php
    books/
      BooksMapper.php
      Book.php

etc.

So all my models will reside under the models directory, but grouped in their own directories.

Naming for each class should be ActorsMapper or Actor (They will both extend Zend_Db_Table or Zend_Db_Row).

I am aware of the fact that in my controllers if I instantiate a model using something like $actors = new ActorsMapper() I will get a Fatal error: Class not found and that why I'm asking for your help with this.

To solve this issue I tried to add my models directory to the include_path:

but this one doesn't even add it to the include path

but even if this adds that path among the included ones, the error still persists.

I've seen this naming of models in the official documentation of the Zend_Db_Table, but I couldn't find anything related to autoloading them.

Thank you all for any solutions.

p.s. zend framework version is 1.11.1


Solution

  • Zend Framework has a build in autoloader. It works by mapping the class name to the directory tree, so Application_Model_Actors_Actor will get mapped to

    Application\
     Models\
      Actors\
       Actor.php
    

    Unfortunately, you cannot change this. The feature you are looking for is namespacing but it is only supported (and is in fact one of the major features) of Zend Framework 2 which is still being developed.