views:

846

answers:

4

For some frustrating reason, I configured some Modules, which seemed to work fine, However I cannot load a Modules Models. If I move the Models to the Default they load, but I just can't get the Framework to find them locally..

Example:

My Modules Directory is:

application\modules\books\models\books.php (books is my Model)

class Application_Module_Books_Model_Books extends Zend_Db_Table_Abstract {}

I also tried..

Books_Model_Books, Model_Books, books, Modules_.. you name it I tried it :)

My controller is in the Books Module, and is an Index Controller, and it can never find the Local Model.

I'm using Application.ini and it is configured this way:

resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 1
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"

I have a BootStrap in the Modules Directory:

class Admin_Bootstrap extends Zend_Application_Module_Bootstrap { }

I'm on Zend Framework 1.10, and ideas.. ?

A: 

1-feel free to delete this cuz you don't need it any more :

resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"

and place this code inside you Bootstrap.php file [application bootstrap ] not the module bootstrap

public function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => '' , 
'basePath' => dirname(__FILE__) . '/modules/'));
return $autoloader;
}

getting back to config you need also to add

   resources.modules[] = ""    
   resources.frontController.defaultModule = "admin" 

here is my complete config file :

phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.defaultModule = "news"
resources.frontController.prefixDefaultModule = 1
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] = ""
;resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 1
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
autoloaderNamespaces[] = "xxxxxx"

resources.db.adapter = "Mysqli"
resources.db.isdefaulttableadapter = true
resources.db.params.host = "localhost"
resources.db.params.dbname = "xxxxx"
resources.db.params.username = "root"
resources.db.params.password = "root"
resources.db.params.charset = "utf8"

hopefully i didn't miss any thing

tawfekov
A: 

The correct class name would be Books_Model_Books, but the filename of that class would need to be Books.php (note the capital 'B').

You shouldn't have a bootstrap in the modules directory, but you probably do want a bootstrap for each module directory, so you'd need a class:

class Books_Bootstrap extends Zend_Application_Module_Bootstrap
{

}

at: application/modules/books/Bootstrap.php (again note the capital 'B').

Check the section on the module resource autloader at http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html for more info.

Tim Fountain
It turns out the problem was my Module Name did not start with a Capital 'M'.. Yikes..
A: 

I started with a new Project, and still getting the same Error, and I think I have made all the suggested changes.

Here is my Application.ini file:

[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 1
resources.modules[] = ""
resources.frontController.params.displayExceptions = 1
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"

My Application directory bootstrap is configured as below:

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    public function _initAutoload() {
        $autoloader = new Zend_Application_Module_Autoloader(array(
                        'namespace' => '' ,
                        'basePath' => dirname(__FILE__) . '/modules/'));
        return $autoloader;
    }


}

The Bootstrap in my Module is:

<?php

class Module1_Bootstrap extends Zend_Application_Module_Bootstrap
{

}

My books Model: Books.php

<?php

class module1_Model_Books
{

    private $_name="";

}

and My testController..

<?php

class Module1_TestController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body

    }

    public function booksAction()
    {
        // action body

        $myBooks= new module1_Model_Books();


    }


}

Here is my file layout, project was built using Zend Tools:

C:\wamp\www\modules\.zfproject.xml
C:\wamp\www\modules\application
C:\wamp\www\modules\docs
C:\wamp\www\modules\library
C:\wamp\www\modules\nbproject
C:\wamp\www\modules\output.txt
C:\wamp\www\modules\public
C:\wamp\www\modules\tests
C:\wamp\www\modules\application\Bootstrap.php
C:\wamp\www\modules\application\configs
C:\wamp\www\modules\application\controllers
C:\wamp\www\modules\application\models
C:\wamp\www\modules\application\modules
C:\wamp\www\modules\application\views
C:\wamp\www\modules\application\configs\application.ini
C:\wamp\www\modules\application\controllers\ErrorController.php
C:\wamp\www\modules\application\controllers\IndexController.php
C:\wamp\www\modules\application\modules\module1
C:\wamp\www\modules\application\modules\module1\controllers
C:\wamp\www\modules\application\modules\module1\models
C:\wamp\www\modules\application\modules\module1\views
C:\wamp\www\modules\application\modules\module1\controllers\TestController.php
C:\wamp\www\modules\application\modules\module1\models\Books.php
C:\wamp\www\modules\application\modules\module1\views\filters
C:\wamp\www\modules\application\modules\module1\views\helpers
C:\wamp\www\modules\application\modules\module1\views\scripts
C:\wamp\www\modules\application\modules\module1\views\scripts\test
C:\wamp\www\modules\application\modules\module1\views\scripts\test\Books.phtml
C:\wamp\www\modules\application\modules\module1\views\scripts\test\index.phtml
C:\wamp\www\modules\application\views\helpers
C:\wamp\www\modules\application\views\scripts
C:\wamp\www\modules\application\views\scripts\error
C:\wamp\www\modules\application\views\scripts\index
C:\wamp\www\modules\application\views\scripts\test
C:\wamp\www\modules\application\views\scripts\error\error.phtml
C:\wamp\www\modules\application\views\scripts\index\index.phtml
C:\wamp\www\modules\application\views\scripts\test\books.phtml
C:\wamp\www\modules\docs\README.txt
C:\wamp\www\modules\nbproject\private
C:\wamp\www\modules\nbproject\project.properties
C:\wamp\www\modules\nbproject\project.xml
C:\wamp\www\modules\nbproject\private\private.properties
C:\wamp\www\modules\public\.htaccess
C:\wamp\www\modules\public\index.php
C:\wamp\www\modules\tests\application
C:\wamp\www\modules\tests\library
C:\wamp\www\modules\tests\phpunit.xml
C:\wamp\www\modules\tests\application\bootstrap.php
C:\wamp\www\modules\tests\application\controllers
C:\wamp\www\modules\tests\application\controllers\TestControllerTest.php
C:\wamp\www\modules\tests\library\bootstrap.php

And my LOVELY Error brought to me by WAMP:

[Tue Mar 30 07:19:05 2010] [error] [client 127.0.0.1] PHP Fatal error:  Class 'module1_Model_Books' not found in C:\\wamp\\www\\modules\\application\\modules\\module1\\controllers\\TestController.php on line 21

I'm going to read up more on the AutoLoader, but it really seems like Zend should be able to find this Model that is in the Module I'm working on..

And, I appreciate everyone's help

+1  A: 

In the application.ini, add a simple line:

resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"

and in the method _initAutoload() inside the Bootstrap put:

$front = $this->bootstrap("frontController")->frontController;
$modules = $front->getControllerDirectory();
$default = $front->getDefaultModule();

foreach (array_keys($modules) as $module) {
    if ($module === $default) {
        continue;
    }

    $moduleloader = new Zend_Application_Module_Autoloader(array(
        'namespace' => $module,
        'basePath'  => $front->getModuleDirectory($module)));
}

make sure the name of models inside each module are like

[name_module]Model[name_model]

in you case, like

class Books_Model_Books {
}

and that's all :D

armandfp