Skip to content
This repository has been archived by the owner on Jan 8, 2021. It is now read-only.

Multiple Database Configuration #47

Closed
sarahman opened this issue May 20, 2014 · 2 comments
Closed

Multiple Database Configuration #47

sarahman opened this issue May 20, 2014 · 2 comments

Comments

@sarahman
Copy link

Hi!
Is it possible to configure multiple databases for databases profiling?
If not possible for now, then it might be a good feature to develop.
Thank you!

@bjyoungblood
Copy link
Owner

You could do so by essentially copying the config for Zend\Db\Adapter\Adapter and renaming the array keys to something descriptive for your database.

For example:

return array(
    'service_manager' => array(
        'factories' => array(
            'db-master' => function ($sm) use ($dbParams) {
               //setup
            },
            'db-slave' => function ($sm) use ($dbParams) {
              //setup
            }
        )
    )
);

@sarahman
Copy link
Author

Hello, @bjyoungblood .

My configuration files are as follows:

// config/autoload/global.php
<?php return array(
    'db' => array(
        'driver' => 'Pdo',
        'dsn' => 'mysql:dbname=spinfo_blog;host=localhost',
        'username'       => 'root',
        'password'       => '123456',
        //other adapters...
        'adapters' => array(
            'user-db-driver' => array(
                'driver'         => 'Pdo',
                'dsn'            => 'mysql:dbname=spinfo_users;host=localhost',
                'username'       => 'root',
                'password'       => '123456',
            ),
            'image-db-driver' => array(
                'driver'         => 'Pdo',
                'dsn'            => 'mysql:dbname=spinfo_images;host=localhost',
                'username'       => 'root',
                'password'       => '123456',
            )
        )
    ),
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
        // to allow other adapters based on the adapters config.
        'abstract_factories' => array(
            'Zend\Db\Adapter\AdapterAbstractServiceFactory'
        )
    )
);

And

<?php
//config/autoload/bjyprofiler.local.php
$prepareAdapter = function($dbParams) {
    $adapter = new BjyProfiler\Db\Adapter\ProfilingAdapter($dbParams);

    if (php_sapi_name() == 'cli') {
        $logger = new Zend\Log\Logger();
        // write queries profiling info to stdout in CLI mode
        $writer = new Zend\Log\Writer\Stream('php://output');
        $logger->addWriter($writer, Zend\Log\Logger::DEBUG);
        $adapter->setProfiler(new BjyProfiler\Db\Profiler\LoggingProfiler($logger));
    } else {
        $adapter->setProfiler(new BjyProfiler\Db\Profiler\Profiler());
    }

    if (isset($dbParams['options']) && is_array($dbParams['options'])) {
        $options = $dbParams['options'];
    } else {
        $options = array();
    }
    $adapter->injectProfilingStatementPrototype($options);
    return $adapter;
};

return array(
    'service_manager' => array(
        'factories' => array(
            'db' => function (\Zend\ServiceManager\ServiceManager $sm) use ($prepareAdapter) {
                $config = $sm->get('Config');
                $dbParams = $config['db'];

                return $prepareAdapter($dbParams);
            },
            'user-db-driver' => function (\Zend\ServiceManager\ServiceManager $sm) use ($prepareAdapter) {
                $config = $sm->get('Config');
                $dbParams = $config['db']['adapters']['user-db-driver'];

                return $prepareAdapter($dbParams);
            },
            'image-db-driver' => function (\Zend\ServiceManager\ServiceManager $sm) use ($prepareAdapter) {
                $config = $sm->get('Config');
                $dbParams = $config['db']['adapters']['image-db-driver'];

                return $prepareAdapter($dbParams);
            }
        ),
    ),
);

But it shows, "You have to install or enable @bjyoungblood's Zend\Db Profiler to use this feature." like this. Please help me use this nice module.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants