zend framework2 - How to access database for custom files -
apart controller, forms, models , views, want create file contains classes common functionality. eg common.php
so need make other connection file custom file of mine or there other way use zend database files (config/local.php , config/global.php)
and under folder should create file.
wrong concept. create yournamespace\stdlib
-library classes contain common functionality. classes later injected dependencies need. example:
getserviceconfig() { return array( 'factories' => array( 'stdlib-dbstuff' => function ($sm) { $dbstuff = new \yournamespace\stdlib\dbstuff(); $dbstuff->setadapter($sm->get('zend\db\adapter\adapter')); return $dbstuff; } )); }
see classes below namespace zend\stdlib
examples ;)
to use stdlib
call classes need servicemanager
. alternatively skip servicemanager
, inject dependencies on controller-level (which sucks, that's not controllers responsibility).
$dbstuff = $this->getservicelocator()->get('stdlib-dbstuff'); // alternatively $adapter = $this->getservicelocator()->get('zend\db\adapter\adapter'); $dbstuff = new \yournamespace\stdlib\dbstuff(); $dbstuff->setadapter($adapter);
Comments
Post a Comment