I'm creating abstract models for managing database entities - I already have EntityAbstract
, EntitySetAbstract
and a ManagerAbstract
models. In my ManagerAbstract model I need a Zend/Db/Adapter
instance in order to create a Zend\Db\TableGateway
.
How could I pull the main instance of the adapter to my ManagerAbstract
? In ZF1 I could have achieved this with Zend_Registry.
If this isn't the right way of doing things in ZF2, I would love to hear the correct way to this kind of things.
Thanks!
Use the Dependency Injection Container,
Zend\Di
. The ZfcUser project does this if you want to poke around in some working code.Alternatively, the basic approach is something like this (code untested!):
Firstly: configure the DI to inject the database connection information:
config/autoload/local.config.php:
Secondly, within your module's
module.config.php
file, inject the adapter into the mapper:module/My/config/module.config.php:
Finally, ensure that your ManagerAbstract class can receive the injection:
module/My/src/My/Model/ManagerAbstract.php:
Note that to use any sub-class, you need to retrieve it via the DIC or inject the mapper into the service and then inject the service into the controller (or other service) where you want to use it.