Skip to content

Commit aa88f99

Browse files
committed
feature #4609 Adding details about the changes to the PdoSessionHandler in 2.6 (weaverryan)
This PR was merged into the 2.6 branch. Discussion ---------- Adding details about the changes to the PdoSessionHandler in 2.6 Hi guys! | Q | A | ------------- | --- | Doc fix? | yes | New docs? | yes symfony/symfony#12833 | Applies to | 2.6+ | Fixed tickets | n/a This follows #4557. This adds more details about the table schema changes to the PdoSessionHandler in 2.6. I also made some improvements (not showing all the extra configuration by default), which I'll backport to 2.3 after this is accepted. Thanks! Commits ------- 1e34823 Fixes thanks to comments and a new note about the LegacyPdoSessionHandler 2f974bb Updating statements, now that we're not overriding the names fd8b552 Adding details about the changes to the PdoSessionHandler in 2.6
2 parents 91f6f41 + 1e34823 commit aa88f99

File tree

1 file changed

+104
-47
lines changed

1 file changed

+104
-47
lines changed

cookbook/configuration/pdo_session_storage.rst

+104-47
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
How to Use PdoSessionHandler to Store Sessions in the Database
55
==============================================================
66

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+
713
The default Symfony session storage writes the session information to
814
file(s). Most medium to large websites use a database to store the session
915
values 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,74 @@ 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+
<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).
124163

125164
Sharing your Database Connection Information
126165
--------------------------------------------
@@ -164,6 +203,24 @@ of your project's data, you can use the connection settings from the
164203
Example SQL Statements
165204
----------------------
166205

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+
167224
MySQL
168225
~~~~~
169226

@@ -173,10 +230,10 @@ following (MySQL):
173230
.. code-block:: sql
174231
175232
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
180237
) COLLATE utf8_bin, ENGINE = InnoDB;
181238
182239
PostgreSQL
@@ -187,10 +244,10 @@ For PostgreSQL, the statement should look like this:
187244
.. code-block:: sql
188245
189246
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
194251
);
195252
196253
Microsoft SQL Server
@@ -201,12 +258,12 @@ For MSSQL, the statement might look like the following:
201258
.. code-block:: sql
202259
203260
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,
208265
PRIMARY KEY CLUSTERED(
209-
[session_id] ASC
266+
[sess_id] ASC
210267
) WITH (
211268
PAD_INDEX = OFF,
212269
STATISTICS_NORECOMPUTE = OFF,

0 commit comments

Comments
 (0)