ZF2 - 使用DB \ Adapter连接到Db

时间:2013-09-03 12:13:54

标签: php zend-framework2

我真的很困惑!

我是ZF2的初学者。现在开始发现它。

我已经在Zend手册中关注了启动Skeleton应用程序。 问题是,对于创建Album模块,它只使用一个在现实世界中不可能的表。开发时至少会有几个表。

现在我正在阅读Michael Romer的ZF2 Web开发。 问题是我无法理解他在哪里放置他的代码。 根据这本书 - 他把他的代码放在module.config.php

<?php

$dbParams = array(
    'database'  => 'gott',
    'username'  => 'root',
    'password'  => '',
    'hostname'  => 'localhost',
);

return array(
    'service_manager' => array(
        'factories' => array(
            'Zend\Db\Adapter\Adapter' => function ($sm) use ($dbParams) {
                return new Zend\Db\Adapter\Adapter(array(
                    'driver'    => 'pdo',
                    'dsn'       => 'mysql:dbname='.$dbParams['database'].';host='.$dbParams['hostname'],
                    'database'  => $dbParams['database'],
                    'username'  => $dbParams['username'],
                    'password'  => $dbParams['password'],
                    'hostname'  => $dbParams['hostname'],
                ));
            },
        ),
    ),
);

当我查看GitHub上的代码时,它说它应该在config / autoload中的global.php中。

据我了解,我的想法是 - 我们在global.php中有params和一些设置,然后我们检测由module.config.php中的global.php启动的服务(带有下面的代码)并将其分配给controller:< / p>

'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
            'Portfolio\Mapper\Category' => function($sm){
                return new \Portfolio\Mapper\Category($sm->get('Zend\Db\Adapter\Adapter'));
            }
        ),
    ),

据我所知,我的控制器应该能够检测到我的数据库连接。\

这是我的控制器代码

    public function addCategoryAction(){

        $form = new \Portfolio\Form\CategoryAdd();

        if($this->getRequest()->isPost()){
            $form->setHydrator(new \Zend\Stdlib\Hydrator\Reflection());
            $form->bind(new \Portfolio\Entity\Category());
            $form->setData($this->getRequest()->getPost());

            if($form->isValid()) {
                $newEntity = $form->getData();

                $mapper = $this->getServiceLocator()->get('Portfolio\Mapper\Category');
                $mapper->insert($newEntity);

                $form = new \Portfolio\Form\CategoryAdd();

                return new ViewModel(array(
                    'form' => $form,
                    'success' =>true
                ));
            } else {
                return new ViewModel(array(
                        'form' => $form
                ));
            }
        } else {
            return new ViewModel(array(
                    'form' => $form
            ));
        }

//         $viewObject = new ViewModel(array(
//             'form' => $form
//         ));

//         return $viewObject; 

    }

这是我使用TableGateway的Mapper

<?php
namespace Portfolio\Mapper;

use Portfolio\Entity\Category as CategoryEntity;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\TableGateway\Feature\RowGatewayFeature;

class Category extends TableGateway {

    protected $tableName = 'portfolio_categories';
    protected $idCol = 'categoryId';
    protected $entityPrototype = null;
    protected $hydrator = null;

    public function __construct($adapter){

        parent::__construct($this->tableName, $adapter, new RowGatewayFeature($this->idCol));

        $this->entityPrototype = new CategoryEntity();
        $this->hydrator = new \Zend\Stdlib\Hydrator\Reflection;

    }    

    public function insert($entity){
        return parent::insert($this->hydrator->extract($entity));
    }

}

它不能正常工作。

An error occurred
An error occurred during execution; please try again later.
Additional information:
Zend\Db\Adapter\Exception\InvalidQueryException
File:
F:\Server\htdocs\gott\vendor\ZF2\library\Zend\Db\Adapter\Driver\Pdo\Statement.php:245
Message:
Statement could not be executed

你能告诉我正确的做法以及它应该如何运作的想法吗?

谢谢!

0 个答案:

没有答案
相关问题