0

我已经尝试过 ZF2,但它没有给出 ZF1 工作的任何查询结果,ZF2 不工作。ZF2 数据库适配器是否不完整,是否保留为 ZF2 本身未解决的错误?原因文档告诉这样做,但它根本不起作用。ZF2 适配器是否可以正常工作?

测试控制器.php

<?php
namespace Application\Controller;
//namespace Application\Model;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Db\Adapter\Adapter;
use Zend\Debug\Debug;

class TestController extends AbstractActionController {

  public function indexAction() {
    $sql = "SELECT * FROM stackoverflow";
    $statement = $this->adapter->query($sql);
    $res =  $statement->execute();
    Debug::dump( $res );
    exit;
  }
}

模块.php

<?php
namespace Application;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\Http;
class Module {

  public function getServiceConfiguration()
  {
      return array(
          'factories' => array(
              'adapter' =>  function($sm) {
                  $config = $sm->get('config');
                  $dbAdapter = new \Zend\Db\Adapter\Adapter($config['db']);
                  return $dbAdapter;
              }
          ),
      );
  }
}

全局.php

<?php
return array(
//    'di' =>array(
//        'instance' =>array(
//            'PDO' => array(
//                'parameters' => array(
//                  'dsn'            => 'mysql:dbname=mydb;host=localhost',
//                  'username'       => 'mydb',
//                  'password'       => '',
//                )
//            ),
//            
//            'User' => array(
//                'parameters' => array(
//                    'pdo' => 'PDO'
//                )
//            )
//        )
//    ),


    'db' => array(
      'driver' => 'Pdo',
      'dsn'            => 'mysql:dbname=mydb;host=localhost',
      'username'       => 'mydb',
      'password'       => '',
//      'driver_options' => array(
//          PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
//      ),
    ),


//    'service_manager' => array(
//        'factories' => array(
//            'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
//        ),
//    ),
);
4

2 回答 2

4

是一个结果$res集。您可以在ZF 手册中阅读有关如何从结果集中提取数据的更多信息。

于 2013-01-14T18:26:23.353 回答
1

//我自己刚启动zf2,但是好像可以这样检索数据。

公共函数 fetchAll() {

 $resultSet = $this->tableGateway->select();

 return $resultSet;

}

于 2014-03-24T03:57:11.630 回答