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

时间:2013-12-02 16:32:50

标签: mysql database zend-framework2

我的数据库连接有问题。我想创建一个服务作为专辑howto。 当我创建服务时,ZF2给我一个错误:

错误:

    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

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

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

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\''
        ),
    ),
);

我在控制器中的电话:

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

非常感谢你的帮助......我被阻止了..:/

2 个答案:

答案 0 :(得分:3)

尝试使用:

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

答案 1 :(得分:0)

我认为问题是您尝试使用服务来配置global.php中的Db环境,但您编写代码的方式不是在global.php中使用服务管理器。

尝试将全局更改为此,因为您正在使用服务管理器和工厂数据库。

 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\''       
),

);

相关问题