Using PdoSessionStorage in Symfony2

13. April 2011 in Sessions, Symfony2

Update: I wrote a more detailed cookbook for the Symfony2 documentation which is online from today: Documentation

 

As I told you in my last blog entry, I had the problem that there is no documentation on how to use the PdoSessionStorage in Symfony2. I opened a question on Stackoverflow but there was no one who could help me. Yesterday I found some time to investigate this problem. After reading the Symfony2 sources I found my way. So here it is:

All you need to do is to change some parameters in the app/config/config.yml file (or one of the environment dependend config-files):

session:
    default_locale: %locale%
    lifetime:       3600
    auto_start:     true
    storage_id: pdo
    db_table: session
    db_id_col: session_id
    db_data_col: session_value
    db_time_col: session_time
  • db_table: The name of the session table in your database
  • db_id_col: The name of the id column in your session table (VARCHAR(255))
  • db_data_col: The name of the value column in your session table (TEXT or CLOB)
  • db_time_col: The name of the time column in your session table (INTEGER)

Now you have to define a PDO connection in the config.yml. For that create a new service:

services:
    pdo_connection:
        class:    PDO
        arguments:
            dsn:      "mysql:dbname=sf2demo"
            user:     root
            password:

That’s it :)

4 responses to Using PdoSessionStorage in Symfony2

  1. Nice post !
    I actually also found out how to use PdoSessionStorage yesterday.
    However I still can’t manage to get it working with the Security Component. Basically I’m logged in and then instantly logged out.
    Any idea ?

  2. Never tried it with Security. Need to check it.

  3. Felt so hopeless looking for answers to my qsueiotns…until now.

  4. hai,
    how to add another field like db_data_col.i want to insert url of the current page in session table using session_set_save_handler in pdosessionstorage class in symfony2.any one please help me

Leave a reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>