views:

2772

answers:

8

Is there a way to have models for each module? I have 3 modules, one is a "contacts" module. I created a model for it in modules/contacts/models/Codes.php Codes Controller

class Contacts_CodesController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    $this->view->messages = $this->_helper->flashMessenger->getMessages(); 

    }

    public function indexAction()
    {

    $codesTable = new Contacts_Model_Codes();

    }

Codes Model:

class Contacts_Model_Codes extends Zend_Db_Table
{
    protected $_name = 'codes';
}

The error I get: Fatal error: Class 'Contacts_Model_Codes' not found in /Applications/MAMP/htdocs/zf_site/application/modules/contacts/controllers/CodesController.php on line 26

thanks

+1  A: 

You have to register the 'Contacts_' namespace with the auto loader. You can use Zend_Application_Module_Autoloader for this.

$autoloader = new Zend_Application_Module_Autoloader(array(
        'namespace' => 'Contacts_',
        'basePath'  => dirname(__FILE__) . '/modules/cotacts',
    ));

This will create the following mappings for your module inside the basePath you provide.

api/         => Api
forms/       => Form
models/      => Model
    DbTable/ => Model_DbTable
plugins/     => Plugin

If you are using Zend_Application to boostrap your application and it' modules you should not need this because the docs say that:

When using module bootstraps with Zend_Application, an instance of Zend_Application_Module_Autoloader will be created by default for each discrete module, allowing you to autoload module resources.

Goran Jurić
This is only true for ZF 1.8, in this version the module autoloader was added and Zend_Application uses it to bootstrap modules. In older versions you will need to use Zend_Application_Resource_Modules
Nicky De Maeyer
A: 

I'm using version 1.9. This is part of my bootstrap:

protected function _initAutoload() {
    $modelLoader = new Zend_Application_Module_Autoloader(array(
                  'namespace' => '',
                   'basePath' => APPLICATION_PATH.'/modules/default')

        );
}

    protected function _initAutoloaders()
    {
        $this->getApplication()->setAutoloaderNamespaces(array('Eric_'));
        return $this;
    }

    protected function _initPlugins()
    {
        $this->bootstrap('autoloaders');
        $this->bootstrap('frontController');

        // register the plugin for setting layouts per module
        $plugin = new Eric_Plugin_Modularlayout();
        $this->frontController->registerPlugin($plugin);
            return $modelLoader;
    }

The plugin Eric_Plugin_Modularlayout sets the correct layout for each module.

I have 3 modules: default, ez, contacts. The funny thing is, In a contacts action I can call the models in the ez/models dir. without a problem.

EricP
+4  A: 

I found the problem. I forgot to put a bootstrap file in with my contacts module. Now it all works and I can have my modules use their own models.

class Contacts_Bootstrap extends Zend_Application_Module_Bootstrap
{

}
EricP
A: 

Thanks! It helped me a lot. I had Bootstrap file in my module, but mine extended Zend_Application_Bootstrap_Bootstrap instead of Zend_Application_Module_Bootstrap

Wiktor
A: 

Oh, many thanks! I had the same problem like you. I forgot to create the module specific Bootstrap class. Creating the file solved my issue.

WernerW
A: 

Have to tried with 1.10 ? Does not work anymore.

Rob
+1  A: 

add

resources.modules[] =

To your config ini

Davek