Skip to content

Commit fae4523

Browse files
committed
merged branch vicb/session_pdo_cleanup (PR #4246)
Commits ------- 51b753a [Session] cleanup of the PDO storage Discussion ---------- [Session] cleanup of the PDO storage and adding what was missing from #4244 --------------------------------------------------------------------------- by travisbot at 2012-05-10T10:40:33Z This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1293901) (merged 51b753a into 6259429).
2 parents 6259429 + 51b753a commit fae4523

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/PdoSessionHandler.php

+24-23
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,24 @@
2020
class PdoSessionHandler implements \SessionHandlerInterface
2121
{
2222
/**
23-
* PDO instance.
24-
*
25-
* @var \PDO
23+
* @var \PDO PDO instance.
2624
*/
2725
private $pdo;
2826

2927
/**
30-
* Database options.
31-
*
32-
*
33-
* @var array
28+
* @var array Database options.
3429
*/
3530
private $dbOptions;
3631

3732
/**
3833
* Constructor.
3934
*
35+
* List of available options:
36+
* * db_table: The name of the table [required]
37+
* * db_id_col: The column where to store the session id [default: sess_id]
38+
* * db_data_col: The column where to store the session data [default: sess_data]
39+
* * db_time_col: The column where to store the timestamp [default: sess_time]
40+
*
4041
* @param \PDO $pdo A \PDO instance
4142
* @param array $dbOptions An associative array of DB options
4243
*
@@ -166,20 +167,20 @@ public function write($id, $data)
166167
//session data can contain non binary safe characters so we need to encode it
167168
$encoded = base64_encode($data);
168169

169-
if ('mysql' === $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
170-
// MySQL would report $stmt->rowCount() = 0 on UPDATE when the data is left unchanged
171-
// it could result in calling createNewSession() whereas the session already exists in
172-
// the DB which would fail as the id is unique
173-
$stmt = $this->pdo->prepare(
174-
"INSERT INTO $dbTable ($dbIdCol, $dbDataCol, $dbTimeCol) VALUES (:id, :data, :time) " .
175-
"ON DUPLICATE KEY UPDATE $dbDataCol = VALUES($dbDataCol), $dbTimeCol = VALUES($dbTimeCol)"
176-
);
177-
$stmt->bindParam(':id', $id, \PDO::PARAM_STR);
178-
$stmt->bindParam(':data', $encoded, \PDO::PARAM_STR);
179-
$stmt->bindValue(':time', time(), \PDO::PARAM_INT);
180-
$stmt->execute();
181-
} else {
182-
try {
170+
try {
171+
if ('mysql' === $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
172+
// MySQL would report $stmt->rowCount() = 0 on UPDATE when the data is left unchanged
173+
// it could result in calling createNewSession() whereas the session already exists in
174+
// the DB which would fail as the id is unique
175+
$stmt = $this->pdo->prepare(
176+
"INSERT INTO $dbTable ($dbIdCol, $dbDataCol, $dbTimeCol) VALUES (:id, :data, :time) " .
177+
"ON DUPLICATE KEY UPDATE $dbDataCol = VALUES($dbDataCol), $dbTimeCol = VALUES($dbTimeCol)"
178+
);
179+
$stmt->bindParam(':id', $id, \PDO::PARAM_STR);
180+
$stmt->bindParam(':data', $encoded, \PDO::PARAM_STR);
181+
$stmt->bindValue(':time', time(), \PDO::PARAM_INT);
182+
$stmt->execute();
183+
} else {
183184
$stmt = $this->pdo->prepare("UPDATE $dbTable SET $dbDataCol = :data, $dbTimeCol = :time WHERE $dbIdCol = :id");
184185
$stmt->bindParam(':id', $id, \PDO::PARAM_STR);
185186
$stmt->bindParam(':data', $encoded, \PDO::PARAM_STR);
@@ -191,9 +192,9 @@ public function write($id, $data)
191192
// session_regenerate_id()
192193
$this->createNewSession($id, $data);
193194
}
194-
} catch (\PDOException $e) {
195-
throw new \RuntimeException(sprintf('PDOException was thrown when trying to write the session data: %s', $e->getMessage()), 0, $e);
196195
}
196+
} catch (\PDOException $e) {
197+
throw new \RuntimeException(sprintf('PDOException was thrown when trying to write the session data: %s', $e->getMessage()), 0, $e);
197198
}
198199

199200
return true;

0 commit comments

Comments
 (0)