You can not avoid calling it explicitly in onBootstrap.
General rule is for global/static state to be avoided. Explicitly inject db adapter in factories for you TDG objects instead.
If you still insist on using it, suggest to use delegator factory to make it a bit more flexible. See blogpost for more info about delegators.
Add this to your module config:
'service_manager' => array(
'aliases' => array(
'globalDbAdapter' => 'Zend\Db\Adapter\Adapter',
),
'delegators' => array(
// Use alias to make it easier to chose which adapter to set as global
'globalDbAdapter' => array(
'YourModule\Factory\GlobalDbAdapterDelegator',
),
),
)
and then delegator factory:
namespace YourModule\Factory;
use Zend\ServiceManager\DelegatorFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Db\TableGateway\Feature\GlobalAdapterFeature;
class GlobalDbAdapterDelegator implements DelegatorFactoryInterface
{
public function createDelegatorWithName(
ServiceLocatorInterface $serviceLocator,
$name,
$requestedName,
$callback
) {
$dbAdapter = $callback();
GlobalAdapterFeature::setStaticAdapter($dbAdapter);
return $dbAdapter;
}
}
and finally in onBootstrap method
// Force creation of service
$e->getApplication()->getServiceManager()->get('globalDbAdapter');