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

Cannot create a custom adapter into Module.php #37

Closed
e-belair opened this issue Aug 21, 2013 · 0 comments
Closed

Cannot create a custom adapter into Module.php #37

e-belair opened this issue Aug 21, 2013 · 0 comments
Labels

Comments

@e-belair
Copy link

if i want to create a custom adapter into Module.php i get an error: cannot create service with the same name.
It's because BjyProfiler contain a custom config who create a factory by default.
I suggest to clear the config file and just make its content optionnal.
There is an example who can generate the error:
i'm building a multi website application. Each website is a module and need to get it's own adapter, so i did that:

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $eventManager = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);

        $this->bootstrapDb($e);
    }

    public function bootstrapDb(MvcEvent $e)
    {
        $e->getApplication()->getEventManager()->getSharedManager()
            ->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function ($e) {
// Comment this line to get the error
                $e->getApplication()->getServiceManager()->setAllowOverride(true);

                /** @var AbstractActionController $controller */
                $controller = $e->getTarget();
                $controllerClass = get_class($controller);
                $moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));

                $dbConfig = $e->getApplication()->getServiceManager()->get('config')['db'];

                // Global Application config
                $config = array_merge(
                    array_intersect_key(
                        $dbConfig,
                        array_flip(array('username', 'password', 'driver', 'dsn', 'options'))
                    ),
                    $dbConfig[strtolower($moduleNamespace)]
                );

                $controller->getServiceLocator()->setFactory(
                    'Zend\Db\Adapter\Adapter',
                    function ($sm) use ($config) {
                        $adapter = new \BjyProfiler\Db\Adapter\ProfilingAdapter($config);
                        $adapter->setProfiler(new \BjyProfiler\Db\Profiler\Profiler);
                        if (isset($config['options']) && is_array($config['options'])) {
                            $options = $config['options'];
                        } else {
                            $options = array();
                        }
                        $adapter->injectProfilingStatementPrototype($options);

                        return $adapter;
                    }
                );

            }, 100);
    }
}

So i have to set AllowOverride to true in serviceManager to get it working

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

No branches or pull requests

2 participants