0

I'm trying to use DbTableGateway to store my session information in a MySQL database--but my "sessions" table is remaining empty. It never contains any rows. Here's my code (more or less copy/pasted from here):

$dbAdapter = new Zend\Db\Adapter\Adapter(array(
    'driver' => 'pdo_mysql',
    'database' => 'db-name',
    'username' => 'username',
    'password' => 'password!'
));
$tableGateway = new \Zend\Db\TableGateway\TableGateway('session', $dbAdapter);
$saveHandler = new \Zend\Session\SaveHandler\DbTableGateway($tableGateway, new \Zend\Session\SaveHandler\DbTableGatewayOptions());
$manager = new \Zend\Session\SessionManager();
$manager->setSaveHandler($saveHandler);

$someContainer = new Container('SomeSessionNamespace');
$someContainer->aBitOfData = 'tasty morsel of data';

And here's a video demonstration of me using this code: http://screencast.com/t/UDDUs6OZOib

As you can see in the video, session information is preserved between requests, but it's not being stored in the database.

I added breakpoints to every function in Zend\Session\SaveHandler\DbTableGateway, and the only one that's getting hit is in __constructor. So the constructor is getting called, but apparently it never gets used for anything else.

What am I missing?

I'm using Zend Framework 2.2.2 on PHP 5.3.

-Josh

Josh
  • 7,232
  • 8
  • 48
  • 75

2 Answers2

1

I found some modules to do that if you need to implement this quickly

  1. https://github.com/Nitecon/DBSessionStorage
  2. https://github.com/gabriel403/G403SessionDb

To use your current code, please check:

  • options of ** DbTableGatewayOptions** (id, data, lifetime, etc..)

    $options = new \Zend\Session\SaveHandler\DbTableGatewayOptions();
    $options->setDataColumn('data');
    $options->setIdColumn('id');
    $options->setLifetimeColumn('lifetime');
    $options->setNameColumn('name');
    $options->setModifiedColumn('modified');
    
  • the start of you SessionManager $manager->start();

Remi Thomas
  • 1,528
  • 1
  • 15
  • 25
  • Appreciate the suggestions, and I may got that route, but I would really like to understand why `Zend\Session\SaveHandler\DbTableGateway` is not working as I have it configured. – Josh Aug 02 '13 at 11:57
  • I tried adding `$manager->start()`. No change. Then I tried `$manager->start(true)`. Still no change. And I am not passing any options to DbTableGatewayOptions. I'm doing the same as shown in the ZF documentation, linked to in my question. But maybe the documentation is incomplete. How am I supposed to setup the DbTableGatewayOptions object? – Josh Aug 02 '13 at 12:42
  • No change, still nothing showing in my session database. :-( – Josh Aug 02 '13 at 21:18
1

Check in application.config.php and make sure the Application module is at the top level

Also make sure that in 'vendor/composer/autoload_namespaces.php' and 'vendor/composer/autoload_static.php' zend and zendxml library path added or not

eg : 'Zend' => array(vendorDir . '/ZF2/library'), 'ZendXml' => array(vendorDir . '/ZF2/library')

Rijesh Np
  • 21
  • 2