Question

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:

  • first try

    added includePaths.models = APPLICATION_PATH "/models" to application.ini

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

  • second try:

    explicitely added the path using set_include_path(implode(PATH_SEPARATOR, array( realpath(APPLICATION_PATH . '/../library'), realpath(APPLICATION_PATH . '/models'), get_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

Was it helpful?

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.

OTHER TIPS

Try to extend Zend_Application_Bootstrap_Bootstrap then you can try this one

$loader = $this->getResourceLoader();

$loader->addResourceType('books', 'models/books', 'Model_Book'); $loader->addResourceType('actors','models/actors','Model_Actor');

I am also trying to implement this kind of implementation in a observer pattern.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top