简体   繁体   中英

How to get dbadapter out of the servicemanager

I try to get the databaseadapter with the following statement:

$dbAdapter =  $this->getServiceLocator()->get('db');

I get an error "A plugin by the name "getServiceLocator" was not found in the plugin manager Zend\\Mvc\\Controller\\PluginManager"

While searching in my module.php (is this the right one file to look at?) I think it might be an understanding problem. I read the article from Ralph Eggert and the zend documentation. I understood that I can get any config info with the servicemanager. But all documentations I found are always for Zend2.

So in my module.php I see something like this (snippet):

public function getServiceConfig()
{
    return [
            'factories' => [
                    Model\ImportTable::class => function($container) {
                        $tableGateway = $container->get(Model\ImportTableGateway::class);
                        return new Model\ImportTable($tableGateway);
                    },
                    Model\ImportTableGateway::class => function ($container) {
                        $dbAdapter = $container->get(AdapterInterface::class);
                        $resultSetPrototype = new ResultSet();
                        $resultSetPrototype->setArrayObjectPrototype(new Model\Import());
                        return new TableGateway('t_dcl', $dbAdapter, null, $resultSetPrototype);
                    },
                    Model\DclimportTable::class => function($container) {
                        $tableGateway = $container->get(Model\DclimportTableGateway::class);
                        return new Model\DclimportTable($tableGateway);
                    },
                    Model\DclimportTableGateway::class => function ($container) {
                        $dbAdapter = $container->get(AdapterInterface::class);
                        $resultSetPrototype = new ResultSet();
                        $resultSetPrototype->setArrayObjectPrototype(new Model\Dclimport());
                        return new TableGateway('t_dcl_import', $dbAdapter, null, $resultSetPrototype);
                    },

There I see a variable $dbAdapter, but how can I get this variable? The error above might be because I use ZEND3 now? Is this method deprecated? I couldn't find any migration info.

Anyway, can somebody explain to me, how to get these keys out of the module.php and in which case create own factories there? I know it is a really basic question, but I think if I won't get this right it will always overtake me again.

The service locator inside controllers was deprecated in version 2.7 and removed in version 3.0.

To fix your code:

  • Find all cases where you call getServiceLocator(), and identify the services they retrieve.
  • Update your controller to accept these services via the constructor.
  • If you have not already, create a factory class for your controller.
  • In the factory, pull the appropriate services and pass them to the controller's constructor.

More detailed info can be found in the migration docs .

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