Skip to content

Commit

Permalink
#11498: Fixed unable to saved serialized object into PostgreSQL binar…
Browse files Browse the repository at this point in the history
…y column (#11499)

#11498: Fixed unable to saved serialized object into PostgreSQL binary column
  • Loading branch information
klimov-paul committed May 6, 2016
1 parent 622676b commit 6b607d0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Yii Framework 2 Change Log
- Enh #11432: Added HTTP status 421 "Misdirected Request" to list of statuses in `yii\web\Response` (dasmfm)
- Enh #11438: Configurable `yii\helpers\Markdown` default flavor (mdmunir)
- Bug #11459: Fixed flash messages not destroyed when `session.auto_start = 1` set in php.ini (cartmanchen)
- Bug #11498: Fixed inability to save serialized object into PostgreSQL binary column (klimov-paul)


2.0.8 April 28, 2016
Expand Down
36 changes: 36 additions & 0 deletions framework/db/pgsql/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,42 @@ public function alterColumn($table, $column, $type)
. $this->db->quoteColumnName($column) . ' ' . $type;
}

/**
* @inheritdoc
*/
public function insert($table, $columns, &$params)
{
return parent::insert($table, $this->normalizeTableRowData($table, $columns), $params);
}

/**
* @inheritdoc
*/
public function update($table, $columns, $condition, &$params)
{
return parent::update($table, $this->normalizeTableRowData($table, $columns), $condition, $params);
}

/**
* Normalizes data to be saved into the table, performing extra preparations and type converting, if necessary.
* @param string $table the table that data will be saved into.
* @param array $columns the column data (name => value) to be saved into the table.
* @return array normalized columns
* @since 2.0.9
*/
private function normalizeTableRowData($table, $columns)
{
if (($tableSchema = $this->db->getSchema()->getTableSchema($table)) !== null) {
$columnSchemas = $tableSchema->columns;
foreach ($columns as $name => $value) {
if (isset($columnSchemas[$name]) && $columnSchemas[$name]->type === Schema::TYPE_BINARY && is_string($value)) {
$columns[$name] = [$value, \PDO::PARAM_LOB]; // explicitly setup PDO param type for binary column
}
}
}
return $columns;
}

/**
* @inheritdoc
*/
Expand Down
22 changes: 22 additions & 0 deletions tests/framework/db/pgsql/PostgreSQLCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,26 @@ public function testLastInsertId()
$command->execute();
$this->assertEquals(3, $db->getSchema()->getLastInsertID('schema1.profile_id_seq'));
}

/**
* @see https://github.com/yiisoft/yii2/issues/11498
*/
public function testSaveSerializedObject()
{
$db = $this->getConnection();

$command = $db->createCommand()->insert('type', [
'int_col' => 1,
'char_col' => 'serialize',
'float_col' => 5.6,
'bool_col' => true,
'blob_col' => serialize($db),
]);
$this->assertEquals(1, $command->execute());

$command = $db->createCommand()->update('type', [
'blob_col' => serialize($db),
], ['char_col' => 'serialize']);
$this->assertEquals(1, $command->execute());
}
}

0 comments on commit 6b607d0

Please sign in to comment.