如何在zend框架2中设置适配器?

时间:2014-12-29 13:55:53

标签: database zend-framework2 adapter

这是autoload / global.php文件的内容:

return array(
    'db' => array(
        'driver'         => 'Pdo',
        'dsn'            => 'mysql:dbname=web_builder;host=localhost',
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter'
                    => 'Zend\Db\Adapter\AdapterServiceFactory',
        ),
    ),
 );

这是autoload / local.php文件的内容:

return array(
    'db' => array(
        'username' => 'DB_User_Name',
        'password' => 'DB_Password',
 ,
);   )

这是module / Module.php的内容的一部分:

namespace Application;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\Db\Adapter\Adapter;
.........
 public function getServiceConfig() {
        return array(
            'factories' => array(
                'Application\Controller\UserController' => function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table = new Model\StickyNotesTable($dbAdapter);
                    return $table;
                },
            ),
        );
    }

在这里,我真的不明白这个功能是做什么的,我只是从一个例子中复制粘贴。如果你能解释一下getServiceConfig函数做了什么,我真的会说服它。

最后是控制器内容:

    namespace Application\Controller;

    use Zend\Mvc\Controller\AbstractActionController;
    use Zend\View\Model\ViewModel;
    use Zend\Db\Adapter\Adapter;
    use Zend\Db\TableGateway\AbstractTableGateway;
    use Zend\Db\Sql\Select;


    class UserController extends AbstractActionController{

        public function __construct(Adapter $adapter) {
            $this->adapter = $adapter;
        }


        public function loginAction(){
            // here i just want to a simple select  and yes I know queries will be executed 
//in Model, but I want to here a simple query. 
//For example in Codeigniter I can't do in model, controller, or view as well.
            return new ViewModel();
        }

所有这些代码的结果显然是一个错误: 捕获致命错误:传递给Application \ Controller \ UserController :: __ construct()的参数1必须是Zend \ Db \ Adapter \ Adapter的实例,没有给出,在C:\ xampp \ htdocs \ zf2 \ vendor \中调用第170行的zendframework \ zendframework \ library \ Zend \ ServiceManager \ AbstractPluginManager.php,第21行的C:\ xampp \ htdocs \ zf2 \ module \ Application \ src \ Application \ Controller \ UserController.php

有人发贴答案可以使这个数据库或查询工作正常吗? THX

0 个答案:

没有答案