Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2.     Raw code pastes from my post at:
  3.     http://www.1stbyte.com/2013/01/03/zend-framework-2-global-database-adapter-object-and-config-variables/
  4.  
  5.  
  6. */
  7.  
  8. #  config/autoload/things.config.local.php
  9. return array(
  10.     'things' => array(
  11.         'avalue' => 'avalue1',
  12.         'boolvalue' => true,
  13.         ),
  14.  
  15. );
  16.  
  17.  
  18. # calling things, assuming $this->things is defined in preDispatch below:
  19. $this->config['things']['boolvalue'];
  20.  
  21. # or, also with creating instance in the action:
  22. $config = $this->getServiceLocator()->get('Config');
  23. echo $config['things']['boolvalue'];
  24.  
  25.  
  26. #2 getting access to service manager config:
  27. $this->getServiceLocator()->get('Config');
  28.  
  29. # add controller action class method:
  30. protected function attachDefaultListeners()
  31. {
  32.     parent::attachDefaultListeners();
  33.     $events = $this->getEventManager();
  34.     $this->events->attach('dispatch', array($this, 'preDispatch'), 100);
  35.     $this->events->attach('dispatch', array($this, 'postDispatch'), -100);
  36. }
  37.  
  38. # and the pre and post methods for dispatch above
  39. public function preDispatch (MvcEvent $e)
  40. {
  41.     // this is a db convenience class I setup in global.php
  42.     // under the service_manager factories (will show below)
  43.     $this->db = $this->getServiceLocator()->get('FBDb');
  44.     // this is just standard config loaded from ServiceManager
  45.     // set your property in your class for $config  (protected $config;)
  46.     // then have access in entire controller
  47.     $this->config = $this->getServiceLocator()->get('Config');
  48.     // this comes from the things.config.local.php file
  49.     echo "things boolvalue: " . $this->config['things']['boolvalue'];
  50. }
  51.  
  52. public function postDispatch (MvcEvent $e)
  53. {
  54.     // Called after actions
  55. }
  56.  
  57.  
  58. # 3 in the global.php config array for service_manager and db adapter access
  59. 'db' => array(
  60.     'driver' => 'Pdo',
  61.     'dsn'   => 'mysql:dbname=mydb;host=localhost;',
  62.     'username' => 'root',
  63.     'password' => '',
  64.     'driver_options' => array(
  65.         PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
  66.     ),
  67.  
  68. ),
  69. 'service_manager' => array(
  70.     'factories' => array(
  71.         'Zend\Db\Adapter\Adapter'
  72.                 => 'Zend\Db\Adapter\AdapterServiceFactory',
  73.         'db-adapter' => function($sm) {
  74.                 $config = $sm->get('config');
  75.                 $config = $config['db'];
  76.                 $dbAdapter = new Zend\Db\Adapter\Adapter($config);
  77.                 return $dbAdapter;
  78.              },
  79.         'FBDb'  => function($sm) {
  80.                 $dba= $sm->get('db-adapter');
  81.                 $db = new FBDb\Db($dba);
  82.                 return $db;
  83.              },
  84.        
  85.     ),
  86. ),
  87.  
  88. #using the preDispatch above we have this line:
  89. $this->db = $this->getServiceLocator()->get('FBDb');
  90.  
  91. # when we want to call up a query and return a record set:
  92. $sql = 'select * from customer where cust_nbr between ? and ?';
  93. $rs = $this->db->fetchAll($sql, array('120400', '125250'));