开发者

ZF: how to set own directory for mapper or models?

My application looks like:

/application/
     configs/
     layouts/
     modules/
        default/
            controllers/
            models/
               model1.php
            mappers/
       开发者_如何学JAVA        table1.php
            views/
        somemodule/
            controllers/
            models/
               model2.php
            mappers/
               table2.php
            views/

The question is how to set path for mappers like models have?

If we use Somemodule_Model_model2. It works. But if we use Somemodule_Mappers_table2. It doesn't.


Are you correcly bootstrapping your module?

<?php

class ModuleName_Boostrap extends Zend_Application_Module_Bootstrap {

    protected function _initAutoloader() {
        $default_loader = new Zend_Application_Module_Autoloader(array(
             'namespace' => $this->getModuleName().'_',
             'basePath'  => MODULE_PATH.'/'.$this->getModuleName()
        ));
    }

}

This will call initDefaultResourceTypes() which will set your mapper directory.


This is already done for you in Zend_Application_Module_Autoloader invoked by module bootstrapper. You can see here all resources being registered for autoloading:

/**
 * Initialize default resource types for module resource classes
 *
 * @return void
 */
public function initDefaultResourceTypes()
{
    $basePath = $this->getBasePath();
    $this->addResourceTypes(array(
        'dbtable' => array(
            'namespace' => 'Model_DbTable',
            'path'      => 'models/DbTable',
        ),
        'mappers' => array(
            'namespace' => 'Model_Mapper',
            'path'      => 'models/mappers',
        ),
        'form'    => array(
            'namespace' => 'Form',
            'path'      => 'forms',
        ),
        'model'   => array(
            'namespace' => 'Model',
            'path'      => 'models',
        ),
        'plugin'  => array(
            'namespace' => 'Plugin',
            'path'      => 'plugins',
        ),
        'service' => array(
            'namespace' => 'Service',
            'path'      => 'services',
        ),
        'viewhelper' => array(
            'namespace' => 'View_Helper',
            'path'      => 'views/helpers',
        ),
        'viewfilter' => array(
            'namespace' => 'View_Filter',
            'path'      => 'views/filters',
        ),
    ));
    $this->setDefaultResourceType('model');
}

All you needed is add "resources.modules[] =" to application.ini and put Bootstrap.php in module directory (catalog for example):

class Catalog_Bootstrap extends Zend_Application_Module_Bootstrap
{
   //just empty bootstrap class. resource init handled by parent 
}

Note: Resources will be prefixed with module name
Example: Catalog_Model_Mapper_Mapper1 will be in %app%/modules/catalog/models/mappers/Mapper1.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜