Zend Framework 2 model database adapter depedency injection
Typically most of the model classes depend on database adapter. First way to get database adapter instance is to request it via service manager every time you need it. Second, and more convenient one, is to let Zend\Di do all the magic for you. To achieve this you need to implement Zend\Db\Adapter\AdapterAwareInterface in your model class. Or write an abstract class that implements this interface and extend it with your model class and Zend\Di will inject database adapter dependency. Note: to make this work you should instantiate model classes via ServiceManager . How to setup a database adapter is explained in Getting Started with Zend Framework 2 . Abstract class example: <?php //module/<YOUR_MODULE>/src/<YOUR_MODULE>/Model/AbstractAdapterAware.php namespace <YOUR_MODULE>\Model; use Zend\Db\Adapter\Adapter; use Zend\Db\Adapter\AdapterAwareInterface; abstract class AbstractAdapterAware implements AdapterAwareInterface { /** * Database instan...