Zend Framework 2 database connection issue

1.1k views Asked by At

I am used ZF2 for creating new applications. We have some issue regarding db connection under controller and model file.

We have put all database credential in "global.php" and "db.local.php" and also fetch db adapter access in our "Module.php" file but we have not fetch database connectivity in controller and model files and don't run query under controller and model.

Here is my code :

**global.php :**

return array(
    'db' => array(
            'driver'=> 'Pdo',
            'dsn'=> 'mysql:dbname=pick_fire;host=localhost',
            'driver_options' => array(
                    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'
            ),
    ),
    'service_manager' => array(
            'factories' => array(
                    'Zend\Db\Adapter\Adapter'=> 'Zend\Db\Adapter\AdapterServiceFactory',
            ),
    ),
);

**db.local.php :** 

<?php
return array(
'db' => array(
    'driver'        => 'Pdo',
    'dsn'           => 'mysql:dbname=pick_fire;host=localhost',
    'username'      =>'root',
    'password'      =>'123456',
    'driver_options'=> array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'
    ),
),
'service_manager' => array(
    'aliases' => array(
        'db' => 'Zend\Db\Adapter\Adapter',
    ),
),);

----------------------------------------------

**Module.php :** 

public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'mail.transport' => function (ServiceManager $serviceManager) {
                    $config = $serviceManager->get('Config'); 
                    $transport = new Smtp();                
                    $transport->setOptions(new SmtpOptions($config['mail']['transport']['options']));

                    return $transport;
                },
            ),
        );
        return array(
            'factories' => array(
                'adapter' =>  function($serviceManager) {
                    $config = $serviceManager->get('config');
                    $dbAdapter = new \Zend\Db\Adapter\Adapter($config['db']);
                    return $dbAdapter;
                }
            ),
        );
    }

----------------------------------------------

Please suggest me, how we get fetch database connection and run queries in our controller and model files.

Thank You in Advanced.

1

There are 1 answers

0
goten4 On

In your getServiceConfig() you have two return statements ... the second is never called. This should work :

**Module.php :** 

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'mail.transport' => function (ServiceManager $serviceManager) {
                $config = $serviceManager->get('Config'); 
                $transport = new Smtp();                
                $transport->setOptions(new SmtpOptions($config['mail']['transport']['options']));

                return $transport;
            },
            'adapter' =>  function($serviceManager) {
                $config = $serviceManager->get('config');
                $dbAdapter = new \Zend\Db\Adapter\Adapter($config['db']);
                return $dbAdapter;
            }
        ),
    );
}