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

2 Answers 2

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();

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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?
No change, still nothing showing in my session database. :-(
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')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.