Zend Framework中未包含的模型

时间:2012-08-31 18:57:27

标签: zend-framework model

我是Zend Framework的新手,并且正在常规的Apache 2 + PHP服务器上使用它。

我想使用扩展Zend_Table_Abstract的模型。我把它放在我的/ application / models目录中,但这些文件甚至没有被包含(它们有语法错误,但我的应用程序工作得很好)。将它们包含在内需要采取哪些措施?在Bootstrap中应该做什么以及应遵循哪些命名约定?我见过人们只使用常规名称,如用户文章

/application/models/Languages.php:

<?php
adasdasdadasd
echo 'hi';

class Languages extends Zend_Db_Table_Abstract {
    protected $_name = 'languages';
}

/application/controllers/LanguageController.php

<?php

class LanguagesController extends Zend_Controller_Action {
    public function indexAction() {
        $languages = new Languages();

        $this->view->languages = $languages->select();
    }
}

/application/Bootstrap.php(有些代码就是因为我从某个例子中复制了它或回答了它)

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
    protected function _initAutoload() {
        $moduleLoader = new Zend_Application_Module_Autoloader(array(
            'namespace' => '',
            'basePath' => APPLICATION_PATH)
        );

        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->registerNamespace(array('Application_'));

        return $moduleLoader;
    }

    protected function _initResourceAutoloader()
    {
        $autoloader = new Zend_Loader_Autoloader_Resource(array(
            'basePath'  => APPLICATION_PATH,
            'namespace' => 'Application_',
        ));

        return $autoloader;
    }

    protected function _initFrontController()
    {
        $controller = Zend_Controller_Front::getInstance();

        $controller->setControllerDirectory(APPLICATION_PATH . '/controllers');

        if ($controller->getParam('bootstrap') === null) {
            $controller->setParam('bootstrap', $this);
        }

        return $controller;
    }

    protected function _initApplication() {
    }

    protected function _initModuleConfig()
    {
    }

    protected function _initLayout(){ 
        return Zend_Layout::startMvc(); 
    }

    protected function _initViewHelpers() {
        $this->bootstrap('layout');
        $layout = $this->getResource('layout');
        $view = $layout->getView();
        $view->doctype('XHTML1_STRICT');
        $view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
        $view->headTitle()->setSeparator(' - ');
        $view->headTitle('My Application');
        $view->headLink()->appendStylesheet('css/general.css');
        $view->headLink()->appendStylesheet('css/navigation.css');
    }

    protected function _initNavigation() {
        $this->bootstrap('layout');
        $layout = $this->getResource('layout');
        $view = $layout->getView();

        $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');

        $navigation = new Zend_Navigation($config);
        $view->navigation($navigation);
    }
}

/application/configs/application.ini

[production]

; Models
includePaths.models = APPLICATION_PATH "/models/"

; Debug output
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0

; Bootstrap
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"

; Database
resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "localhost"
resources.db.params.dbname = "dabase"
resources.db.params.username = "root"
resources.db.params.password = "password"
resources.db.isDefaultTableAdapter = true

[development : production]

; Debug output
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

; Database
resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "localhost"
resources.db.params.dbname = "dabase"
resources.db.params.username = "root"
resources.db.params.password = "password"
resources.db.isDefaultTableAdapter = true

1 个答案:

答案 0 :(得分:1)

您需要更改模型命名的方式,因为Zend自动加载器无法简单地找到正确的文件ID来加载模型。所以不要像这样的模型:

class Language extends Zend_Db_Table_Abstract {
....
}

你应该这样命名:

class Application_Model_Language extends Zend_Db_Table_Abstract {
....
}

然后实例化:

$languages = new Application_Model_Language();

请注意,模型的映射方式不同,您的Application_Model_Language模型类必须放在application/models/Language.php中才能生效。