MSSQL Access From ZF2 via Linux

For programming and general questions on Zend Framework
Post Reply
Posts: 4
Joined: Thu Aug 25, 2011 10:04 pm

MSSQL Access From ZF2 via Linux

Post by derrickbetts » Thu Feb 07, 2013 3:34 pm

I'm upgrading an application that presently runs on ZF1 to ZF2. I'm having trouble getting results to return from the ZF2 connection.

In ZF1 this test works perfectly:

Code: Select all

$db = Zend_Db::factory('Pdo_Mssql', array(
            'host'     => 'ServerNameFromFreeTdsConfig',
            'charset'	=> 'UTF-8',
            'username' => 'myUsername',
            'password' => 'myPassword',
            'dbname'   => 'database_name',
            'pdoType'  => 'dblib'

        $stmt = $db->prepare("select * from Products");
        $result = $stmt->fetchAll();
I've been trying this in ZF2 but I'm not really getting anywhere. In my config\autoload\global.php I have:

Code: Select all

return array(
    'db' => array(
        'host'  => 'ServerNameFromFreeTdsConfig',
        'charset'   => 'UTF-8',
        'dbname'  => 'database_name',
        'username'  => 'myUsername',
        'password'  => 'myPassword',
        'driver'    => 'pdo',
        'pdodriver' => 'dblib',
And in the Module.php file:

Code: Select all

public function onBootstrap(MvcEvent $e)
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();

        $config = $e->getApplication()->getServiceManager()->get('Configuration');
        $dbAdapter = new Adapter($config['db']);

Then in the Model\Products.php

Code: Select all

class Products extends AbstractTableGateway
    protected $table;
    protected $featureSet;

    public function __construct($table = 'Products') {
        $this->table = $table;
        $this->featureSet = new FeatureSet();
        $this->featureSet->addFeature(new GlobalAdapterFeature());

    //Test the connection.
    public function getProducts() {   
        $result = $this->getAdapter()->query("select * from Products", Adapter::QUERY_MODE_EXECUTE);
It looks like it is connecting because the "var_dump" above returns a ["fieldCount":protected]=> int(7) which is correct (there are 7 columns in that table). However, it is not returning any results.
What might I need to do to get this to work in ZF2? Do I need to somehow extend Zend\Db\Adapter\Adapter using code from the ZF1 Zend_Db_Adapter_Pdo_Mssql.php file? Or is there some simple solution I'm missing?

Thanks for any insight.

Posts: 5
Joined: Thu Dec 20, 2012 6:44 pm

Re: MSSQL Access From ZF2 via Linux

Post by denozawr » Mon Feb 25, 2013 6:15 am

try this

Code: Select all

$statement = $this->getAdapter()->query("select * from Products", Adapter::QUERY_MODE_EXECUTE);
$result = $statement->execute();

Posts: 2
Joined: Thu Feb 28, 2013 6:29 pm

Re: MSSQL Access From ZF2 via Linux

Post by fern5596 » Thu Feb 28, 2013 6:30 pm

Hey, I don't want to steal your thunder but let me know if you are willing to share how you got Zend 1 to work on linux with mssql.
We keep getting errors when we try to get it to work.

That or a link to a tutorial you followed!

Post Reply