将模型放在Zend Framework的库目录中 [英] Putting models in library directory in Zend Framework

查看:72
本文介绍了将模型放在Zend Framework的库目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将模型放在Zend Framework的模块目录之外.确切地说,在/library文件夹中

I want to put models outside module directory in Zend Framework. To be precise, in /library folder

library/
  models/
    actors/
      ActorsMapper.php
      Actor.php
    books/
      BooksMapper.php
      Book.php

代替

application/
modules/
   models/
    actors/
      ActorsMapper.php
      Actor.php
    books/
      BooksMapper.php
      Book.php

这样做是为了使我不必为我创建的每个模块创建单独的模型. 我必须更改哪些配置? 如果您需要更多详细信息,请询问.

This is done so that I don't have to create separate model for each of the module I create. What configurations will I have to change? If you need more details, please ask.

谢谢:)

推荐答案

第一个答案有效,但是如果您要在引导程序中注册自动加载功能,我会给您另一个答案.

First answer works, but I will give you another answer in case if you want to register autoload in bootstrap.

1)将"models"文件夹和所有Table.php文件放入库中.

1) put 'models' folder into library with all Table.php files.

每个模型/类都应具有:

Every model/class should have:

class Model_Table extends Zend_Db_Table_Abstract{ ... }

2)在bootstrap.php中放:

2) In bootstrap.php put:

protected function _initAutoLoad() {
    // Add autoloader empty namespace
    $autoLoader = Zend_Loader_Autoloader::getInstance();
    $resourceLoader = new Zend_Loader_Autoloader_Resource(
            array(
                'basePath'      => APPLICATION_PATH,
                'namespace'     => '',
                'resourceTypes' => array(
                'model'         => array(
                        'path'      => '../library/models/',
                        'namespace' => 'Model_'
                    ),
                ),
            )
    );
    return $resourceLoader;
}

就是这样.现在,您可以在像这样的控制器中使用模型:

That's it. Now you can use your models in controllers like this:

$model = new Model_Table();

这篇关于将模型放在Zend Framework的库目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆