Implemented __sleep in yii\db\Connection

to support serializing the connection object without exceptions.

- Added test for serializing an open connection.
- Added unserialize test

close #10149
This commit is contained in:
Sam Mousa
2015-11-12 17:01:43 +01:00
committed by Carsten Brandt
parent 61acf28338
commit 603a956b8d
3 changed files with 23 additions and 0 deletions

View File

@ -57,6 +57,7 @@ Yii Framework 2 Change Log
- Enh #10056: Allowed any callable to be passed to `ActionColumn::$urlCreator` (freezy-sk) - Enh #10056: Allowed any callable to be passed to `ActionColumn::$urlCreator` (freezy-sk)
- Enh #10061: `yii\helpers\BaseInflector::transliterate()` is now public. Introduced different levels of transliteration strictness (silverfire) - Enh #10061: `yii\helpers\BaseInflector::transliterate()` is now public. Introduced different levels of transliteration strictness (silverfire)
- Enh #10118: Allow easy extension of slug generation in `yii\behaviors\SluggableBehavior` (cebe, hesna) - Enh #10118: Allow easy extension of slug generation in `yii\behaviors\SluggableBehavior` (cebe, hesna)
- Enh #10149: Made `yii\db\Connection` serializable (Sam Mousa)
- Enh: Added last resort measure for `FileHelper::removeDirectory()` fail to unlink symlinks under Windows (samdark) - Enh: Added last resort measure for `FileHelper::removeDirectory()` fail to unlink symlinks under Windows (samdark)
- Chg #9369: `Yii::$app->user->can()` now returns `false` instead of erroring in case `authManager` component is not configured (creocoder) - Chg #9369: `Yii::$app->user->can()` now returns `false` instead of erroring in case `authManager` component is not configured (creocoder)
- Chg #9411: `DetailView` now automatically sets container tag ID in case it's not specified (samdark) - Chg #9411: `DetailView` now automatically sets container tag ID in case it's not specified (samdark)

View File

@ -945,4 +945,14 @@ class Connection extends Component
return null; return null;
} }
/**
* Close the connection before serializing.
* @return array
*/
public function __sleep()
{
$this->close();
return array_keys((array) $this);
}
} }

View File

@ -11,6 +11,7 @@ use yii\db\Transaction;
*/ */
class ConnectionTest extends DatabaseTestCase class ConnectionTest extends DatabaseTestCase
{ {
public function testConstruct() public function testConstruct()
{ {
$connection = $this->getConnection(false); $connection = $this->getConnection(false);
@ -42,6 +43,17 @@ class ConnectionTest extends DatabaseTestCase
$connection->open(); $connection->open();
} }
public function testSerialize()
{
$connection = $this->getConnection(false, false);
$connection->open();
$serialized = serialize($connection);
$unserialized = unserialize($serialized);
$this->assertInstanceOf('yii\db\Connection', $unserialized);
$this->assertEquals(123, $connection->createCommand("SELECT 123")->queryScalar());
}
public function testGetDriverName() public function testGetDriverName()
{ {
$connection = $this->getConnection(false, false); $connection = $this->getConnection(false, false);