From 9862ee35b2aeed2763e386a8c2ab1e1658d735ee Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Sun, 3 Nov 2013 18:51:16 -0500 Subject: [PATCH] Fixes #1115: fixed the issue with PDO boolean value binding with pgsql --- framework/yii/db/pgsql/Schema.php | 22 ++++++++++++++++++++++ .../db/pgsql/PostgreSQLActiveRecordTest.php | 10 ---------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/framework/yii/db/pgsql/Schema.php b/framework/yii/db/pgsql/Schema.php index c01ea52..4925984 100644 --- a/framework/yii/db/pgsql/Schema.php +++ b/framework/yii/db/pgsql/Schema.php @@ -131,6 +131,28 @@ class Schema extends \yii\db\Schema } /** + * Determines the PDO type for the given PHP data value. + * @param mixed $data the data whose PDO type is to be determined + * @return integer the PDO type + * @see http://www.php.net/manual/en/pdo.constants.php + */ + public function getPdoType($data) + { + // php type => PDO type + static $typeMap = [ + // https://github.com/yiisoft/yii2/issues/1115 + // Cast boolean to integer values to work around problems with PDO casting false to string '' https://bugs.php.net/bug.php?id=33876 + 'boolean' => \PDO::PARAM_INT, + 'integer' => \PDO::PARAM_INT, + 'string' => \PDO::PARAM_STR, + 'resource' => \PDO::PARAM_LOB, + 'NULL' => \PDO::PARAM_NULL, + ]; + $type = gettype($data); + return isset($typeMap[$type]) ? $typeMap[$type] : \PDO::PARAM_STR; + } + + /** * Returns all table names in the database. * @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. * If not empty, the returned table names will be prefixed with the schema name. diff --git a/tests/unit/framework/db/pgsql/PostgreSQLActiveRecordTest.php b/tests/unit/framework/db/pgsql/PostgreSQLActiveRecordTest.php index d41a837..1fffad7 100644 --- a/tests/unit/framework/db/pgsql/PostgreSQLActiveRecordTest.php +++ b/tests/unit/framework/db/pgsql/PostgreSQLActiveRecordTest.php @@ -11,14 +11,4 @@ use yiiunit\framework\db\ActiveRecordTest; class PostgreSQLActiveRecordTest extends ActiveRecordTest { protected $driverName = 'pgsql'; - - public function testBooleanAttribute() - { - $this->markTestSkipped('Storing boolean values does not work in PostgreSQL right now. See https://github.com/yiisoft/yii2/issues/1115 for details.'); - } - - public function testStoreEmpty() - { - // as this test attempts to store data with invalid type it is okay for postgres to fail, skipping silently. - } }