模块间共享的 ZF2 模型 [英] ZF2 Models shared between Modules

查看:24
本文介绍了模块间共享的 ZF2 模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始为一个新项目设置一个新的 ZF2 应用程序,基于 ZF2 框架,正在查看他们关于 模型.

I've just started setting up a new ZF2 application for a new project, based off the ZF2 skeleton, and am looking at their tutorial on Models.

tl;dr:我应该如何在多个不同模块之间共享模型,将其放在更高级别(/模块之外)的某个位置?

tl;dr: how should I share a Model between multiple different modules, by putting it somewhere in a higher level (outside /module)?

我们有几个模块设置如下:

We have several modules setup like so:

/
/module/ModuleName
/module/ModuleName/config
/module/ModuleName/src
/module/ModuleName/src/ModuleName

我正要设置一个文件夹 /module/ModuleName/src/ModuleName/Model/TableName.php,但后来我意识到:该表也需要在其他模块中访问.那我该怎么办?

I was just about to setup a folder /module/ModuleName/src/ModuleName/Model/TableName.php, but then I realised: that table will need to be accessed in other Modules as well. So what should I do?

我应该将 Models 文件夹放在 /module/Model 中还是会导致它被视为一个模块,即 site.com/model(根据我们当前的配置,它会).

Should I put the Models folder in /module/Model or will that be result in it being treated as a module i.e. site.com/model (based on our current config, it would).

我应该在不同位置之间复制和粘贴模型吗?我应该将模型放回 /vendor/library/Company/Model 某处吗?不太确定是否有最佳做法!

Should I copy and paste the models between places? Should I stick the models back in /vendor/library/Company/Model somewhere? Not quite sure if there's a best practice for this!

问题2:本教程还建议使用ServiceManager 来实例化数据库模型以使用相同的实例.如果我有一个带有 5 个控制器的模块,每个控制器访问完全独立的表(比如每个 4 个表)怎么办?在我看来,它会在每个页面加载时冗余初始化 16 个表(对于该模块中的其他控制器).单个表初始化为页面加载增加了 55 毫秒.有没有解决的办法??我不确定如何根据教程初始化 tablegateway 的操作将配置移动到控制器的操作?

Question 2: The tutorial also suggests to use ServiceManager to instantiate the database models to use the same instance. What if I have a module with 5 controllers, with each controller accessing completely separate tables (say 4 tables each)? It seems to me that it would redundantly initialise 16 tables on each page load (for the other controllers in that module). A single table initialisation adds 55ms to the pageload. Is there a way around this?? I'm not sure how I'd move the config to the controller's actions based on what the tutorial does to initialise the tablegateway?

推荐答案

1)

您可以使用应用程序中任何模块中的任何模型,因此它们可以共享".例如,您使用 ServiceManager 来帮助您获取项目中模型(和其他类)的实例.

You can use any model from any module in your application, so they can be "shared". Fro example you use the ServiceManager to help you grab instances of models (and other classes) in your project.

服务管理器配置:

'factories' => array(
    'AuthService' => function($sm) {
        $auth = new \Zend\Authentication\AuthenticationService();

        return $auth;
    },
    'RoleMapper' => function($sm) {
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
        $mapper = new \Application\Mapper\RoleMapper;
        $mapper->setDbAdapter($dbAdapter);
        $mapper->setEntityPrototype(new \Application\Model\Role);
        $mapper->setHydrator(new \Application\Model\RoleHydrator);

        return $mapper;
    },
    'UserMapper' => function($sm) {
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
        $mapper = new \MyModule\Mapper\UserMapper;
        $mapper->setDbAdapter($dbAdapter);
        $mapper->setEntityPrototype(new \MyModule\Model\User);
        $mapper->setHydrator($sm->get('UserHydrator'));

        return $mapper;
    },
    ....

如您所见,您可以在其中定义来自多个模块的类.通常,您只需为每个模块定义一个单独的 ServiceManager 配置.

As you can see you can have classes from multiple modules defined in there. Usually you would just define a separate ServiceManager config for each module though.

没有什么可以阻止您像这样在应用程序模块中创建UserMapper"的实例:

There's nothing to stop you from making an instance of "UserMapper" inside your Application module like this:

一些控制器:

$this->getServiceLocator()->get('UserMapper');
// Or even grab Zend libraries like this
$this->getServiceLocator()->get('AuthService');

ServiceManager 将允许您从存在问题的任何其他模块中的任何模块中获取类的实例.

The ServiceManager will allow you to grab instances of classes from any of your modules inside any of the others with a problem.

2)

在您请求之前,服务管理器实际上不会创建任何实例,因此没有您建议的开销.

The service manager doesn't actually make an instance of anything until you request it, so there's no overhead as you suggest.

从上面的例子来看,在你第一次请求一个实例之前,并没有实际创建实例:

From the example above, there's no instances actually created until you first ask for one:

$this->getServiceLocator()->get('UserMapper'); // instance now created.

例如,如果您从不向服务管理器询问RoleMapper",则不会创建任何实例.

If you never ask the service manager for a "RoleMapper" for example, then no instance is created.

这篇关于模块间共享的 ZF2 模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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