zend框架模型定位和自动加载

时间:2021-09-14 11:11:01

I've started to document myself regarding Zend Framework as I will soon start using it in production. Everything was working fine, until I started to use and work with models :).

我已经开始记录自己关于Zend Framework的内容,因为我很快就会开始在生产中使用它。一切都很好,直到我开始使用并使用模型:)。

The default location for models, based on Zend recommendations, is application/models/DbTable, where all the models will be thrown in. This location forces me to name a model like Application_Model_DbTable_Actors. For me, this is a very long name for a model and not a easy to use one.

基于Zend建议的模型的默认位置是application / models / DbTable,其中将抛出所有模型。这个位置迫使我命名像Application_Model_DbTable_Actors这样的模型。对我来说,这是一个非常长的模型名称,而不是一个易于使用的名称。

The directory structure that I want to obtain looks something like this:

我想要获取的目录结构如下所示:

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

etc.

等等

So all my models will reside under the models directory, but grouped in their own directories.

所以我的所有模型都将驻留在models目录下,但是在它们自己的目录中分组。

Naming for each class should be ActorsMapper or Actor (They will both extend Zend_Db_Table or Zend_Db_Row).

每个类的命名应该是ActorsMapper或Actor(它们都将扩展Zend_Db_Table或Zend_Db_Row)。

I am aware of the fact that in my controllers if I instantiate a model using something like $actors = new ActorsMapper() I will get a Fatal error: Class not found and that why I'm asking for your help with this.

我知道在我的控制器中,如果我使用$ actors = new ActorsMapper()之类的东西实例化一个模型,我会得到一个致命错误:找不到类,这就是为什么我要求你提供帮助。

To solve this issue I tried to add my models directory to the include_path:

为了解决这个问题,我尝试将我的models目录添加到include_path:

  • first try

    第一次尝试

    added includePaths.models = APPLICATION_PATH "/models" to application.ini

    将includePaths.models = APPLICATION_PATH“/ models”添加到application.ini

but this one doesn't even add it to the include path

但是这个甚至没有将它添加到包含路径中

  • second try:

    第二次尝试:

    explicitely added the path using set_include_path(implode(PATH_SEPARATOR, array( realpath(APPLICATION_PATH . '/../library'), realpath(APPLICATION_PATH . '/models'), get_include_path(), )));

    使用set_include_path(implode(PATH_SEPARATOR,array(realpath(APPLICATION_PATH。'/ / / library'),realpath(APPLICATION_PATH。'/ models'),get_include_path(),)))明确地添加了路径。

but even if this adds that path among the included ones, the error still persists.

但即使这在包含的路径中添加了该路径,错误仍然存​​在。

I've seen this naming of models in the official documentation of the Zend_Db_Table, but I couldn't find anything related to autoloading them.

我在Zend_Db_Table的官方文档中看到了这个模型的命名,但我找不到与自动加载相关的任何内容。

Thank you all for any solutions.

谢谢大家的任何解决方案。

p.s. zend framework version is 1.11.1

附: zend框架版本是1.11.1

2 个解决方案

#1


0  

Zend Framework has a build in autoloader. It works by mapping the class name to the directory tree, so Application_Model_Actors_Actor will get mapped to

Zend Framework有一个内置的自动加载器。它通过将类名映射到目录树来工作,因此Application_Model_Actors_Actor将映射到

Application\
 Models\
  Actors\
   Actor.php

Unfortunately, you cannot change this. The feature you are looking for is namespacing but it is only supported (and is in fact one of the major features) of Zend Framework 2 which is still being developed.

不幸的是,你无法改变这一点。您正在寻找的功能是命名空间,但它仅支持(并且实际上是Zend Framework 2的一个主要功能),它仍在开发中。

#2


0  

Try to extend Zend_Application_Bootstrap_Bootstrap then you can try this one

尝试扩展Zend_Application_Bootstrap_Bootstrap然后你可以试试这个

$loader = $this->getResourceLoader();

$ loader = $ this-> getResourceLoader();

$loader->addResourceType('books', 'models/books', 'Model_Book'); $loader->addResourceType('actors','models/actors','Model_Actor');

$ loader-> addResourceType('books','models / books','Model_Book'); $装载机> addResourceType( '角色', '型号/演员', 'Model_Actor');

I am also trying to implement this kind of implementation in a observer pattern.

我也试图以观察者模式实现这种实现。

#1


0  

Zend Framework has a build in autoloader. It works by mapping the class name to the directory tree, so Application_Model_Actors_Actor will get mapped to

Zend Framework有一个内置的自动加载器。它通过将类名映射到目录树来工作,因此Application_Model_Actors_Actor将映射到

Application\
 Models\
  Actors\
   Actor.php

Unfortunately, you cannot change this. The feature you are looking for is namespacing but it is only supported (and is in fact one of the major features) of Zend Framework 2 which is still being developed.

不幸的是,你无法改变这一点。您正在寻找的功能是命名空间,但它仅支持(并且实际上是Zend Framework 2的一个主要功能),它仍在开发中。

#2


0  

Try to extend Zend_Application_Bootstrap_Bootstrap then you can try this one

尝试扩展Zend_Application_Bootstrap_Bootstrap然后你可以试试这个

$loader = $this->getResourceLoader();

$ loader = $ this-> getResourceLoader();

$loader->addResourceType('books', 'models/books', 'Model_Book'); $loader->addResourceType('actors','models/actors','Model_Actor');

$ loader-> addResourceType('books','models / books','Model_Book'); $装载机> addResourceType( '角色', '型号/演员', 'Model_Actor');

I am also trying to implement this kind of implementation in a observer pattern.

我也试图以观察者模式实现这种实现。