简体   繁体   English

模块之间共享的ZF2模型

[英]ZF2 Models shared between Modules

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 . 我刚刚开始基于ZF2骨架为新项目设置一个新的ZF2应用程序,我正在查看他们关于模型的教程。

tl;dr: how should I share a Model between multiple different modules, by putting it somewhere in a higher level (outside /module)? tl; dr:我应该如何在多个不同的模块之间共享模型,将它放在更高级别(外部/模块)中?

We have several modules setup like so: 我们有几个模块设置如下:

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

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. 我正要设置一个文件夹/module/ModuleName/src/ModuleName/Model/TableName.php ,但后来我意识到:该表也需要在其他模块中访问。 So what should I do? 所以我该怎么做?

Should I put the Models folder in /module/Model or will that be result in it being treated as a module ie site.com/model (based on our current config, it would). 我应该将Models文件夹放在/module/Model否则会导致它被视为一个模块,即site.com/model(基于我们当前的配置,它会)。

Should I copy and paste the models between places? 我应该在地方之间复制和粘贴模型吗? Should I stick the models back in /vendor/library/Company/Model somewhere? 我应该将模型重新放在/vendor/library/Company/Model吗? Not quite sure if there's a best practice for this! 不太确定是否有最好的做法!

Question 2: The tutorial also suggests to use ServiceManager to instantiate the database models to use the same instance. 问题2:本教程还建议使用ServiceManager实例化数据库模型以使用相同的实例。 What if I have a module with 5 controllers, with each controller accessing completely separate tables (say 4 tables each)? 如果我有一个带有5个控制器的模块,每个控制器访问完全独立的表(比如每个4个表)怎么办? It seems to me that it would redundantly initialise 16 tables on each page load (for the other controllers in that module). 在我看来,它会在每个页面加载上冗余初始化16个表(对于该模块中的其他控制器)。 A single table initialisation adds 55ms to the pageload. 单个表初始化为页面加载增加了55ms。 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? 我不确定如何根据教程初始化tablegateway的操作将配置移动到控制器的操作?

1) 1)

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. 例如,您使用ServiceManager来帮助您获取项目中的模型(和其他类)的实例。

Service Manager Config: 服务管理器配置:

'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;
    },
    ....

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. 通常,您只需为每个模块定义一个单独的ServiceManager配置。

There's nothing to stop you from making an instance of "UserMapper" inside your Application module like this: 没有什么可以阻止你在你的应用程序模块中创建一个“UserMapper”实例,如下所示:

Some Controller: 一些控制器:

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

The ServiceManager will allow you to grab instances of classes from any of your modules inside any of the others with a problem. ServiceManager允许您从有问题的任何其他模块中获取类的实例。

2) 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.

If you never ask the service manager for a "RoleMapper" for example, then no instance is created. 例如,如果您从未向服务经理询问“RoleMapper”,则不会创建任何实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM