Search code examples
phpmodelszend-framework

Putting models in library directory in Zend Framework


I want to put models outside module directory in Zend Framework. To be precise, in /library folder

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

INSTEAD OF

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

This is done so that I don't have to create separate model for each of the module I create. What configurations will I have to change? If you need more details, please ask.

Thank you :)


Solution

  • First answer works, but I will give you another answer in case if you want to register autoload in bootstrap.

    1) put 'models' folder into library with all Table.php files.

    Every model/class should have:

    class Model_Table extends Zend_Db_Table_Abstract{ ... }
    

    2) In bootstrap.php put:

    protected function _initAutoLoad() {
        // Add autoloader empty namespace
        $autoLoader = Zend_Loader_Autoloader::getInstance();
        $resourceLoader = new Zend_Loader_Autoloader_Resource(
                array(
                    'basePath'      => APPLICATION_PATH,
                    'namespace'     => '',
                    'resourceTypes' => array(
                    'model'         => array(
                            'path'      => '../library/models/',
                            'namespace' => 'Model_'
                        ),
                    ),
                )
        );
        return $resourceLoader;
    }
    

    That's it. Now you can use your models in controllers like this:

    $model = new Model_Table();