4
4
How to Use PdoSessionHandler to Store Sessions in the Database
5
5
==============================================================
6
6
7
+ .. caution ::
8
+
9
+ There was a backwards-compatibility 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
+
7
13
The default Symfony session storage writes the session information to
8
14
file(s). Most medium to large websites use a database to store the session
9
15
values instead of files, because databases are easier to use and scale in a
@@ -24,18 +30,11 @@ configuration format of your choice):
24
30
# ...
25
31
handler_id : session.handler.pdo
26
32
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
-
35
33
services :
36
34
pdo :
37
35
class : PDO
38
36
arguments :
37
+ # see below for how to use your existing DB config
39
38
dsn : " mysql:dbname=mydatabase"
40
39
user : myuser
41
40
password : mypassword
@@ -44,7 +43,7 @@ configuration format of your choice):
44
43
45
44
session.handler.pdo :
46
45
class : Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
47
- arguments : ["@pdo", "%pdo.db_options%" ]
46
+ arguments : ["@pdo"]
48
47
49
48
.. code-block :: xml
50
49
@@ -53,16 +52,6 @@ configuration format of your choice):
53
52
<framework : session handler-id =" session.handler.pdo" cookie-lifetime =" 3600" auto-start =" true" />
54
53
</framework : config >
55
54
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
-
66
55
<services >
67
56
<service id =" pdo" class =" PDO" >
68
57
<argument >mysql:dbname=mydatabase</argument >
@@ -76,7 +65,6 @@ configuration format of your choice):
76
65
77
66
<service id =" session.handler.pdo" class =" Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler" >
78
67
<argument type =" service" id =" pdo" />
79
- <argument >%pdo.db_options%</argument >
80
68
</service >
81
69
</services >
82
70
@@ -94,14 +82,6 @@ configuration format of your choice):
94
82
),
95
83
));
96
84
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
-
105
85
$pdoDefinition = new Definition('PDO', array(
106
86
'mysql:dbname=mydatabase',
107
87
'myuser',
@@ -112,15 +92,74 @@ configuration format of your choice):
112
92
113
93
$storageDefinition = new Definition('Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler', array(
114
94
new Reference('pdo'),
115
- '%pdo.db_options%',
116
95
));
117
96
$container->setDefinition('session.handler.pdo', $storageDefinition);
118
97
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
+ <services >
122
+ <service id =" session.handler.pdo"
123
+ class =" Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler" >
124
+ <argument type =" service" id =" pdo" />
125
+ <argument type =" collection" >
126
+ <argument key =" db_table" >sessions</argument >
127
+ </argument >
128
+ </service >
129
+ </services >
130
+
131
+ .. code-block :: php
132
+
133
+ // app/config/config.php
134
+
135
+ use Symfony\Component\DependencyInjection\Definition;
136
+ // ...
137
+
138
+ $storageDefinition = new Definition(
139
+ 'Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler',
140
+ array(
141
+ new Reference('pdo'),
142
+ array('db_table' => 'session')
143
+ )
144
+ );
145
+ $container->setDefinition('session.handler.pdo', $storageDefinition);
146
+
147
+ .. versionadded :: 2.6
148
+ The ``db_lifetime_col `` was introduced in Symfony 2.6. Prior to 2.6,
149
+ this column did not exist.
150
+
151
+ The following things can be configured:
152
+
153
+ * ``db_table ``: (default ``session ``) The name of the session table in your
154
+ database;
155
+ * ``db_id_col ``: (default ``sess_id ``) The name of the id column in your
156
+ session table (VARCHAR(128));
157
+ * ``db_data_col ``: (default ``sess_data ``) The name of the value column in
158
+ your session table (BLOB);
159
+ * ``db_time_col ``: (default ``sess_time ``) The name of the time column in
160
+ your session table (INTEGER);
161
+ * ``db_lifetime_col ``: (default ``sess_lifetime ``) The name of the lifetime
162
+ column in your session table (INTEGER).
124
163
125
164
Sharing your Database Connection Information
126
165
--------------------------------------------
@@ -164,6 +203,24 @@ of your project's data, you can use the connection settings from the
164
203
Example SQL Statements
165
204
----------------------
166
205
206
+ .. _pdo-session-handle-26-changes :
207
+
208
+ .. sidebar :: Schema Changes needed when Upgrading to Symfony 2.6
209
+
210
+ If you use the ``PdoSessionHandler `` prior to Symfony 2.6 and upgrade, you'll
211
+ need to make a few changes to your session table:
212
+
213
+ * A new session lifetime (``sess_lifetime `` by default) integer column
214
+ needs to be added;
215
+ * The data column (``sess_data `` by default) needs to be changed to a
216
+ BLOG type.
217
+
218
+ Check the SQL statements below for more details.
219
+
220
+ To keep the old (2.5 and earlier) functionality, change your class name
221
+ to use ``LegacyPdoSessionHandler `` instead of ``PdoSessionHandler `` (the
222
+ legacy class was added in Symfony 2.6.2).
223
+
167
224
MySQL
168
225
~~~~~
169
226
@@ -173,10 +230,10 @@ following (MySQL):
173
230
.. code-block :: sql
174
231
175
232
CREATE TABLE `session` (
176
- `session_id ` VARBINARY(128) NOT NULL PRIMARY KEY,
177
- `session_data ` BLOB NOT NULL,
178
- `session_time ` INTEGER UNSIGNED NOT NULL,
179
- `session_lifetime ` MEDIUMINT NOT NULL
233
+ `sess_id ` VARBINARY(128) NOT NULL PRIMARY KEY,
234
+ `sess_data ` BLOB NOT NULL,
235
+ `sess_time ` INTEGER UNSIGNED NOT NULL,
236
+ `sess_lifetime ` MEDIUMINT NOT NULL
180
237
) COLLATE utf8_bin, ENGINE = InnoDB;
181
238
182
239
PostgreSQL
@@ -187,10 +244,10 @@ For PostgreSQL, the statement should look like this:
187
244
.. code-block :: sql
188
245
189
246
CREATE TABLE session (
190
- session_id VARCHAR(128) NOT NULL PRIMARY KEY,
191
- session_data BYTEA NOT NULL,
192
- session_time INTEGER NOT NULL,
193
- session_lifetime INTEGER NOT NULL
247
+ sess_id VARCHAR(128) NOT NULL PRIMARY KEY,
248
+ sess_data BYTEA NOT NULL,
249
+ sess_time INTEGER NOT NULL,
250
+ sess_lifetime INTEGER NOT NULL
194
251
);
195
252
196
253
Microsoft SQL Server
@@ -201,12 +258,12 @@ For MSSQL, the statement might look like the following:
201
258
.. code-block :: sql
202
259
203
260
CREATE TABLE [dbo].[session](
204
- [session_id ] [nvarchar](255) NOT NULL,
205
- [session_data ] [ntext] NOT NULL,
206
- [session_time ] [int] NOT NULL,
207
- [session_lifetime ] [int] NOT NULL,
261
+ [sess_id ] [nvarchar](255) NOT NULL,
262
+ [sess_data ] [ntext] NOT NULL,
263
+ [sess_time ] [int] NOT NULL,
264
+ [sess_lifetime ] [int] NOT NULL,
208
265
PRIMARY KEY CLUSTERED(
209
- [session_id ] ASC
266
+ [sess_id ] ASC
210
267
) WITH (
211
268
PAD_INDEX = OFF,
212
269
STATISTICS_NORECOMPUTE = OFF,
0 commit comments