简体   繁体   中英

Zend Framework 2 database connection issue

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.

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;
            }
        ),
    );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM