0

I have ZF2 tried following, but it does not give any query results with ZF1 works, ZF2 not workings. Is ZF2 database adapters incomplete, left as bug not resolved by ZF2 itself? Cause documentation tells to do so but it simply not working. Does ZF2 adapter works ever?

TestController.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;
  }
}

Module.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;
              }
          ),
      );
  }
}

global.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',
//        ),
//    ),
);
Sam
  • 16,435
  • 6
  • 55
  • 89

2 Answers2

4

The $res is a result set. You can read more about how to extract data from a result set in the ZF manual.

kokx
  • 1,706
  • 13
  • 19
  • that does not explain how you get the results, while doing dump, it does not show a single records. –  Jan 15 '13 at 12:22
1

//I just started zf2 myself,but it seems like you can retrieve the data like this.

public function fetchAll() {

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

 return $resultSet;

}

Jennifer
  • 1,822
  • 2
  • 20
  • 45