44How to Use PdoSessionHandler to Store Sessions in the Database
55==============================================================
66
7+ .. caution ::
8+
9+ There was a backwards-compatability break in Symfony 2.6: the database
10+ schema changed slightly. See :ref: `Symfony 2.6 Changes <pdo-session-handle-26-changes >`
11+ for details.
12+
713The default Symfony session storage writes the session information to
814file(s). Most medium to large websites use a database to store the session
915values instead of files, because databases are easier to use and scale in a
@@ -24,18 +30,11 @@ configuration format of your choice):
2430 # ...
2531 handler_id : session.handler.pdo
2632
27- parameters :
28- pdo.db_options :
29- db_table : session
30- db_id_col : session_id
31- db_data_col : session_data
32- db_time_col : session_time
33- db_lifetime_col : session_lifetime
34-
3533 services :
3634 pdo :
3735 class : PDO
3836 arguments :
37+ # see below for how to use your existing DB config
3938 dsn : " mysql:dbname=mydatabase"
4039 user : myuser
4140 password : mypassword
@@ -44,7 +43,7 @@ configuration format of your choice):
4443
4544 session.handler.pdo :
4645 class : Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
47- arguments : ["@pdo", "%pdo.db_options%" ]
46+ arguments : ["@pdo"]
4847
4948 .. code-block :: xml
5049
@@ -53,16 +52,6 @@ configuration format of your choice):
5352 <framework : session handler-id =" session.handler.pdo" cookie-lifetime =" 3600" auto-start =" true" />
5453 </framework : config >
5554
56- <parameters >
57- <parameter key =" pdo.db_options" type =" collection" >
58- <parameter key =" db_table" >session</parameter >
59- <parameter key =" db_id_col" >session_id</parameter >
60- <parameter key =" db_data_col" >session_data</parameter >
61- <parameter key =" db_time_col" >session_time</parameter >
62- <parameter key =" db_lifetime_col" >session_lifetime</parameter >
63- </parameter >
64- </parameters >
65-
6655 <services >
6756 <service id =" pdo" class =" PDO" >
6857 <argument >mysql:dbname=mydatabase</argument >
@@ -76,7 +65,6 @@ configuration format of your choice):
7665
7766 <service id =" session.handler.pdo" class =" Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler" >
7867 <argument type =" service" id =" pdo" />
79- <argument >%pdo.db_options%</argument >
8068 </service >
8169 </services >
8270
@@ -94,14 +82,6 @@ configuration format of your choice):
9482 ),
9583 ));
9684
97- $container->setParameter('pdo.db_options', array(
98- 'db_table' => 'session',
99- 'db_id_col' => 'session_id',
100- 'db_data_col' => 'session_data',
101- 'db_time_col' => 'session_time',
102- 'db_lifetime_col' => 'session_lifetime',
103- ));
104-
10585 $pdoDefinition = new Definition('PDO', array(
10686 'mysql:dbname=mydatabase',
10787 'myuser',
@@ -112,15 +92,70 @@ configuration format of your choice):
11292
11393 $storageDefinition = new Definition('Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler', array(
11494 new Reference('pdo'),
115- '%pdo.db_options%',
11695 ));
11796 $container->setDefinition('session.handler.pdo', $storageDefinition);
11897
119- * ``db_table ``: The name of the session table in your database
120- * ``db_id_col ``: The name of the id column in your session table (VARCHAR(128))
121- * ``db_data_col ``: The name of the value column in your session table (BLOB)
122- * ``db_time_col ``: The name of the time column in your session table (INTEGER)
123- * ``db_lifetime_col ``: The name of the lifetime column in your session table (INTEGER)
98+ Configuring the Table and Column Names
99+ --------------------------------------
100+
101+ This will expect a ``sessions `` table with a number of different columns.
102+ The table name, and all of the column names, can be configured by passing
103+ a second array argument to ``PdoSessionHandler ``:
104+
105+ .. configuration-block ::
106+
107+ .. code-block :: yaml
108+
109+ # app/config/config.yml
110+ services :
111+ # ...
112+ session.handler.pdo :
113+ class : Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
114+ arguments :
115+ - " @pdo"
116+ - { 'db_table': 'sessions'}
117+
118+ .. code-block :: xml
119+
120+ <!-- app/config/config.xml -->
121+
122+ <services >
123+
124+ <service id =" session.handler.pdo" class =" Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler" >
125+ <argument type =" service" id =" pdo" />
126+ <argument type =" collection" >
127+ <argument key =" db_table" >sessions</argument >
128+ </argument >
129+ </service >
130+ </services >
131+
132+ .. code-block :: php
133+
134+ // app/config/config.php
135+ // ...
136+
137+ $storageDefinition = new Definition('Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler', array(
138+ new Reference('pdo'),
139+ array('db_table' => 'session')
140+ ));
141+ $container->setDefinition('session.handler.pdo', $storageDefinition);
142+
143+ .. versionadded :: 2.6
144+ The ``db_lifetime_col `` was introduced in Symfony 2.6 This column did
145+ not exist previously.
146+
147+ The following things can be configured:
148+
149+ * ``db_table ``: (default ``session ``) The name of the session table in your
150+ database;
151+ * ``db_id_col ``: (default ``sess_id ``) The name of the id column in your
152+ session table (VARCHAR(128));
153+ * ``db_data_col ``: (default ``sess_data ``) The name of the value column in
154+ your session table (BLOB);
155+ * ``db_time_col ``: (default ``sess_time ``) The name of the time column in
156+ your session table (INTEGER);
157+ * ``db_lifetime_col ``: (default ``sess_lifetime ``) The name of the lifetime
158+ column in your session table (INTEGER).
124159
125160Sharing your Database Connection Information
126161--------------------------------------------
@@ -163,6 +198,20 @@ of your project's data, you can use the connection settings from the
163198 Example SQL Statements
164199----------------------
165200
201+ .. _pdo-session-handle-26-changes :
202+
203+ .. sidebar :: Schema Changes needed when Upgrading to Symfony 2.6
204+
205+ If you use the `PdoSessionHandler ` prior to Symfony 2.6 and upgrade, you'll
206+ need to make a few changes to your session table:
207+
208+ * A new session lifetime (``sess_lifetime `` by default) integer column
209+ needs to be added;
210+ * The data column (``sess_data `` by default) needs to be changed to a
211+ BLOG type.
212+
213+ Check the SQL statements below for more details.
214+
166215MySQL
167216~~~~~
168217
0 commit comments