4
4
How to Use PdoSessionHandler to Store Sessions in the Database
5
5
==============================================================
6
6
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
+
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,70 @@ 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
+
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).
124
159
125
160
Sharing your Database Connection Information
126
161
--------------------------------------------
@@ -163,6 +198,20 @@ of your project's data, you can use the connection settings from the
163
198
Example SQL Statements
164
199
----------------------
165
200
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
+
166
215
MySQL
167
216
~~~~~
168
217
0 commit comments