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: 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
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?
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).
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!
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?
UserMapper
around a lot in a few tutorials - is that just an example, or is it an actual ZF2 thing? In regards to #1: So if I registerManufacturing\Model\WorkflowTable' => function($sm) { .. }
, should I then just be able to call$this->getServiceLocator()->get('Manufacturing\Model\WorkflowTable')
in any other library? Would it be a more standard practice to have a base module with our models?? – Carbohydrate