English 中文(简体)
Zend框架中的模型
原标题:Models in Zend Framework

我是zend框架的新手。我已经在应用程序目录中创建了“模型”文件夹。在模型文件夹中,我创建了一个类Application_models_Albums,它扩展了Zend_Db_Table_Abstract

现在,当我在IndexController中使用以下代码时。我得到错误:

$albums = new Application_Models_Albums();
$this->view->albums = $albums->fetchAll();     

Error: Fatal error: Class Application_Models_Albums not found in H:DocumentsIIS Server Rootlocalhostlearningzf1applicationcontrollersIndexController.php on line 13

请帮忙。如何在zend框架中加载模型?

最佳回答

Your model class name should be Application_Model_Albums - with Model in singular.
See http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html for more info on Zend Autoloader.

问题回答

你需要命名你的模型相册。并保存在models/albums.php

自动加载程序将根据名称找出从何处加载它。因此,即使该模型被命名为albums,您也可以通过new Application_Models_albums()调用它

自动加载器在application/models/albums.php中找到它

类似地,Zend_Db_Table_Abstract类位于Zend/Db/Table/Abstract.php中

编辑

如果您无法获得自动加载器要工作,只需将models/添加到包含路径中,就可以以快速简单的方式完成。我以前做过,效果很好。

类似于index.php中的<code>set_include_path().path_SEPERATOR.“models/”)

你需要你的模型类-

require_once  PathToModelApplication_Models_Albums.php ;

一些人创建了自动加载器类来处理这个问题,YMMV

听起来您的Bootstrapapplication.ini需要将命名空间指定为application_。

在<code>Bootstrap.php</code>中:

protected function _initAutoloader()
{
    $autoloader = new Zend_Application_Module_Autoloader(array(
         basePath  => APPLICATION_PATH,
         namespace  =>  Kwis_ ,
    ));
    return $autoloader;
}

警告地,在configs/application.ini中:

appnamespace = "Application_"

您还可以扩展Zend_Controller_Action并添加这样的方法:

protected $_tables = array();

protected function _getTable($table)
{
    if (false === array_key_exists($table, $this->_tables)) {
        require_once(APPLICATION_PATH. /modules/ .$this->_request->getModuleName(). /models/ .$table. .php );
        $this->_tables[$table] = new $table();
    }
    return $this->_tables[$table];
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签