简体   繁体   English

ZF2服务未找到数据库适配器

[英]ZF2 Service not found db adapter

I have a problem with my database connection. 我的数据库连接有问题。 I want to create a service as the Album howto. 我想创建一个服务作为“相册”方法。 When I create the service, ZF2 return me an error: 创建服务时,ZF2返回一个错误:

Error: 错误:

    Zend\ServiceManager\Exception\ServiceNotFoundException

File:
/UNXDEVCKT01/www/firewall/ZendFramework/vendor/ZF2/library/Zend/ServiceManager/ServiceManager.php:496
Message:
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for user-table
Stack trace:
#0 /UNXDEVCKT01/www/firewall/ZendFramework/module/Firewall/src/Firewall/Controller/ServiceController.php(28): Zend\ServiceManager\ServiceManager->get('user-table')
#1 /UNXDEVCKT01/www/firewall/ZendFramework/vendor/ZF2/library/Zend/Mvc/Controller/AbstractActionController.php(83): Firewall\Controller\ServiceController->addAction()
#2 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#3 /UNXDEVCKT01/www/firewall/ZendFramework/vendor/ZF2/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#4 /UNXDEVCKT01/www/firewall/ZendFramework/vendor/ZF2/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#5 /UNXDEVCKT01/www/firewall/ZendFramework/vendor/ZF2/library/Zend/Mvc/Controller/AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#6 /UNXDEVCKT01/www/firewall/ZendFramework/vendor/ZF2/library/Zend/Mvc/DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#7 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#8 /UNXDEVCKT01/www/firewall/ZendFramework/vendor/ZF2/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#9 /UNXDEVCKT01/www/firewall/ZendFramework/vendor/ZF2/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#10 /UNXDEVCKT01/www/firewall/ZendFramework/vendor/ZF2/library/Zend/Mvc/Application.php(309): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#11 /UNXDEVCKT01/www/firewall/ZendFramework/public/index.php(17): Zend\Mvc\Application->run()
#12 {main}

/module/Firewall/Module.php /module/Firewall/Module.php

namespace Firewall;

use Zend\Mvc\ModuleRouteListener,
    Zend\Mvc\MvcEvent,
    Firewall\Model\UserTable;

class Module
{
    // ...
    public function getServiceConfiguration()
    {
        return array(
        'factories' => array(
            'user-table' => function($sm) {
                $dbAdapter = $sm->get('db-adapter');
                $table = new UserTable($dbAdapter);
                return $table;
            },
        ),
        );
    }
}

/module/Application/Module.php /module/Application/Module.php

namespace Application;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\Db\Adapter\Adapter as DbAdapter;

class Module
{
    public function getServiceConfiguration()
    {
        return array(
            'factories' => array(
                'db-adapter' => function($sm) {
                    $config = $sm->get('config');
                    $config = $config['db'];
                    $dbAdapter = new DbAdapter($config);
                    return $dbAdapter;
                },
            ),
        );
    }
}

/module/Application/config/autoload/global.php /module/Application/config/autoload/global.php

return array(
    'db' => array(
        'driver' => 'Pdo',
        'dsn' => 'mysql:dbname=firewall;hostname=localhost',
        'username' => 'aaaaaa',
        'password' => 'aaaaaa',
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
    ),
);

my call in a controller: 我在控制器中的呼叫:

if (!$this->userTable) {
            $sm = $this->getServiceLocator();
            $this->userTable = $sm->get('user-table');
        }
        return $this->userTable;

Thanks you very very much for your helping... I'm blocked.. :/ 非常感谢您的帮助...我被屏蔽了..:/

尝试使用:

public function getServiceConfig() //instead of getServiceConfiguration()

I think the problem is you trying to use service to to configure your Db environment in global.php but the way you wrote your code is not using service manager in global.php. 我认为问题是您试图使用服务在global.php中配置您的Db环境,但是您编写代码的方式不是在global.php中使用服务管理器。

Try to change your global to this since you are using service manager and factory db. 由于您正在使用服务管理器和工厂数据库,请尝试将全局设置更改为此。

 return array(
'service_manager' => array(
    'factories' => array(
        'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
    ),
),
    'db' => array('driver' => 'Pdo',
    'dsn' => 'mysql:dbname=firewall;host=localhost;charset=utf8',
    'user' => 'root', 'pass' => 'aaaa' ),
     'driver_options' => array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''       
),

); );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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