0

我真的很困惑!

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

我在 Zend 手册中关注了 Starting Skeleton application。存在的问题是,为了创建专辑模块,它只使用一张在现实世界中不可能的表。开发时至少会有几张桌子。

现在我正在阅读 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 中有参数和一些设置,然后我们在 module.config.php 中检测由 global.php 启动的服务(使用下面的代码)并将其分配给控制器:

'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 映射器

<?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

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

谢谢!

4

0 回答 0