diff --git a/extensions/sphinx/ActiveRecord.php b/extensions/sphinx/ActiveRecord.php index 9a0593b5b7..4c743d0f91 100644 --- a/extensions/sphinx/ActiveRecord.php +++ b/extensions/sphinx/ActiveRecord.php @@ -621,9 +621,9 @@ abstract class ActiveRecord extends BaseActiveRecord if (isset($columns[$name])) { if ($columns[$name]->isMva) { $mvaValue = explode(',', $value); - $row[$name] = array_map(array($columns[$name], 'typecast'), $mvaValue); + $row[$name] = array_map(array($columns[$name], 'phpTypecast'), $mvaValue); } else { - $row[$name] = $columns[$name]->typecast($value); + $row[$name] = $columns[$name]->phpTypecast($value); } } } diff --git a/extensions/sphinx/CHANGELOG.md b/extensions/sphinx/CHANGELOG.md index df9dd354b6..8a4d45fd80 100644 --- a/extensions/sphinx/CHANGELOG.md +++ b/extensions/sphinx/CHANGELOG.md @@ -8,6 +8,7 @@ Yii Framework 2 sphinx extension Change Log - Bug #4018: AR relation eager loading does not work with db models (klimov-paul) - Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue) - Enh #3520: Added `unlinkAll()`-method to active record to remove all records of a model relation (NmDimas, samdark, cebe) +- Chg #2287: Split `yii\sphinx\ColumnSchema::typecast()` into two methods `phpTypecast()` and `dbTypecast()` to allow specifying PDO type explicitly (cebe) 2.0.0-beta April 13, 2014 diff --git a/extensions/sphinx/ColumnSchema.php b/extensions/sphinx/ColumnSchema.php index a30c19864a..240d41fe7c 100644 --- a/extensions/sphinx/ColumnSchema.php +++ b/extensions/sphinx/ColumnSchema.php @@ -55,12 +55,12 @@ class ColumnSchema extends Object public $isMva; /** - * Converts the input value according to [[phpType]]. + * Converts the input value according to [[phpType]] after retrieval from the database. * If the value is null or an [[Expression]], it will not be converted. * @param mixed $value input value * @return mixed converted value */ - public function typecast($value) + public function phpTypecast($value) { if ($value === null || gettype($value) === $this->phpType || $value instanceof Expression) { return $value; @@ -70,13 +70,29 @@ class ColumnSchema extends Object } switch ($this->phpType) { case 'string': - return (string) $value; + return is_resource($value) ? $value : (string) $value; case 'integer': return (integer) $value; case 'boolean': return (boolean) $value; + case 'double': + return (double) $value; } return $value; } + + /** + * Converts the input value according to [[type]] and [[dbType]] for use in a db query. + * If the value is null or an [[Expression]], it will not be converted. + * @param mixed $value input value + * @return mixed converted value. This may also be an array containing the value as the first element + * and the PDO type as the second element. + */ + public function dbTypecast($value) + { + // the default implementation does the same as casting for PHP but it should be possible + // to override this with annotation of explicit PDO type. + return $this->phpTypecast($value); + } } diff --git a/extensions/sphinx/QueryBuilder.php b/extensions/sphinx/QueryBuilder.php index bdf5399330..48c7d553fc 100644 --- a/extensions/sphinx/QueryBuilder.php +++ b/extensions/sphinx/QueryBuilder.php @@ -973,14 +973,14 @@ class QueryBuilder extends Object } else { $phName = self::PARAM_PREFIX . count($params); $lineParts[] = $phName; - $params[$phName] = (isset($columnSchema)) ? $columnSchema->typecast($subValue) : $subValue; + $params[$phName] = (isset($columnSchema)) ? $columnSchema->dbTypecast($subValue) : $subValue; } } return '(' . implode(',', $lineParts) . ')'; } else { $phName = self::PARAM_PREFIX . count($params); - $params[$phName] = (isset($columnSchema)) ? $columnSchema->typecast($value) : $value; + $params[$phName] = (isset($columnSchema)) ? $columnSchema->dbTypecast($value) : $value; return $phName; } diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index ef912a5925..82884be721 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -131,6 +131,7 @@ Yii Framework 2 Change Log - Enh: Improved `yii\helpers\Inflector::slug` to support more cases for Russian, Hebrew and special characters (samdark) - Enh #3926: `yii\widgets\Breadcrumbs::$links`. Allows individual link to have its own `template` (creocoder, umneeq) - Enh #4028: Added ability to `yii\widgets\Menu` to encode each item's label separately (creocoder, umneeq) +- Chg #2287: Split `yii\db\ColumnSchema::typecast()` into two methods `phpTypecast()` and `dbTypecast()` to allow specifying PDO type explicitly (cebe) - Chg #2898: `yii\console\controllers\AssetController` is now using hashes instead of timestamps (samdark) - Chg #2913: RBAC `DbManager` is now initialized via migration (samdark) - Chg #3036: Upgraded Twitter Bootstrap to 3.1.x (qiangxue) diff --git a/framework/db/ActiveRecord.php b/framework/db/ActiveRecord.php index c511b653c6..8726a2b4b0 100644 --- a/framework/db/ActiveRecord.php +++ b/framework/db/ActiveRecord.php @@ -357,7 +357,7 @@ class ActiveRecord extends BaseActiveRecord $columns = static::getTableSchema()->columns; foreach ($row as $name => $value) { if (isset($columns[$name])) { - $row[$name] = $columns[$name]->typecast($value); + $row[$name] = $columns[$name]->phpTypecast($value); } } parent::populateRecord($record, $row); @@ -454,7 +454,7 @@ class ActiveRecord extends BaseActiveRecord if ($table->sequenceName !== null) { foreach ($table->primaryKey as $name) { if ($this->getAttribute($name) === null) { - $id = $table->columns[$name]->typecast($db->getLastInsertID($table->sequenceName)); + $id = $table->columns[$name]->phpTypecast($db->getLastInsertID($table->sequenceName)); $this->setAttribute($name, $id); $values[$name] = $id; break; diff --git a/framework/db/ColumnSchema.php b/framework/db/ColumnSchema.php index 64dd65d22f..b094103bcb 100644 --- a/framework/db/ColumnSchema.php +++ b/framework/db/ColumnSchema.php @@ -33,7 +33,7 @@ class ColumnSchema extends Object public $type; /** * @var string the PHP type of this column. Possible PHP types include: - * string, boolean, integer, double. + * `string`, `boolean`, `integer`, `double`. */ public $phpType; /** @@ -79,12 +79,12 @@ class ColumnSchema extends Object public $comment; /** - * Converts the input value according to [[phpType]]. + * Converts the input value according to [[phpType]] after retrieval from the database. * If the value is null or an [[Expression]], it will not be converted. * @param mixed $value input value * @return mixed converted value */ - public function typecast($value) + public function phpTypecast($value) { if ($value === '' && $this->type !== Schema::TYPE_TEXT && $this->type !== Schema::TYPE_STRING && $this->type !== Schema::TYPE_BINARY) { return null; @@ -106,4 +106,18 @@ class ColumnSchema extends Object return $value; } + + /** + * Converts the input value according to [[type]] and [[dbType]] for use in a db query. + * If the value is null or an [[Expression]], it will not be converted. + * @param mixed $value input value + * @return mixed converted value. This may also be an array containing the value as the first element + * and the PDO type as the second element. + */ + public function dbTypecast($value) + { + // the default implementation does the same as casting for PHP but it should be possible + // to override this with annotation of explicit PDO type. + return $this->phpTypecast($value); + } } diff --git a/framework/db/QueryBuilder.php b/framework/db/QueryBuilder.php index da1c2707b9..df174420dc 100644 --- a/framework/db/QueryBuilder.php +++ b/framework/db/QueryBuilder.php @@ -147,7 +147,7 @@ class QueryBuilder extends \yii\base\Object } else { $phName = self::PARAM_PREFIX . count($params); $placeholders[] = $phName; - $params[$phName] = !is_array($value) && isset($columnSchemas[$name]) ? $columnSchemas[$name]->typecast($value) : $value; + $params[$phName] = !is_array($value) && isset($columnSchemas[$name]) ? $columnSchemas[$name]->dbTypecast($value) : $value; } } @@ -188,7 +188,7 @@ class QueryBuilder extends \yii\base\Object $vs = []; foreach ($row as $i => $value) { if (!is_array($value) && isset($columnSchemas[$columns[$i]])) { - $value = $columnSchemas[$columns[$i]]->typecast($value); + $value = $columnSchemas[$columns[$i]]->dbTypecast($value); } if (is_string($value)) { $value = $this->db->quoteValue($value); @@ -247,7 +247,7 @@ class QueryBuilder extends \yii\base\Object } else { $phName = self::PARAM_PREFIX . count($params); $lines[] = $this->db->quoteColumnName($name) . '=' . $phName; - $params[$phName] = !is_array($value) && isset($columnSchemas[$name]) ? $columnSchemas[$name]->typecast($value) : $value; + $params[$phName] = !is_array($value) && isset($columnSchemas[$name]) ? $columnSchemas[$name]->dbTypecast($value) : $value; } } diff --git a/framework/db/cubrid/Schema.php b/framework/db/cubrid/Schema.php index 235fb6f4e1..e42dfaba74 100644 --- a/framework/db/cubrid/Schema.php +++ b/framework/db/cubrid/Schema.php @@ -251,7 +251,7 @@ class Schema extends \yii\db\Schema } elseif (isset($type) && $type === 'bit') { $column->defaultValue = hexdec(trim($info['Default'],'X\'')); } else { - $column->defaultValue = $column->typecast($info['Default']); + $column->defaultValue = $column->phpTypecast($info['Default']); } return $column; diff --git a/framework/db/mssql/Schema.php b/framework/db/mssql/Schema.php index adc1308f2c..5a72595921 100644 --- a/framework/db/mssql/Schema.php +++ b/framework/db/mssql/Schema.php @@ -222,7 +222,7 @@ class Schema extends \yii\db\Schema $info['column_default'] = null; } if (!$column->isPrimaryKey && ($column->type !== 'timestamp' || $info['column_default'] !== 'CURRENT_TIMESTAMP')) { - $column->defaultValue = $column->typecast($info['column_default']); + $column->defaultValue = $column->phpTypecast($info['column_default']); } return $column; diff --git a/framework/db/mysql/Schema.php b/framework/db/mysql/Schema.php index 9becb4de26..56d072ce54 100644 --- a/framework/db/mysql/Schema.php +++ b/framework/db/mysql/Schema.php @@ -177,7 +177,7 @@ class Schema extends \yii\db\Schema } elseif (isset($type) && $type === 'bit') { $column->defaultValue = bindec(trim($info['Default'],'b\'')); } else { - $column->defaultValue = $column->typecast($info['Default']); + $column->defaultValue = $column->phpTypecast($info['Default']); } } diff --git a/framework/db/oci/Schema.php b/framework/db/oci/Schema.php index 7de0709990..1fde5ca2ff 100644 --- a/framework/db/oci/Schema.php +++ b/framework/db/oci/Schema.php @@ -223,7 +223,7 @@ EOD; if (stripos($column['DATA_DEFAULT'], 'timestamp') !== false) { $c->defaultValue = null; } else { - $c->defaultValue = $c->typecast($column['DATA_DEFAULT']); + $c->defaultValue = $c->phpTypecast($column['DATA_DEFAULT']); } } diff --git a/framework/db/pgsql/Schema.php b/framework/db/pgsql/Schema.php index 5af73c6463..8db814ad74 100644 --- a/framework/db/pgsql/Schema.php +++ b/framework/db/pgsql/Schema.php @@ -416,9 +416,9 @@ SQL; } elseif (preg_match("/^'(.*?)'::/", $column->defaultValue, $matches)) { $column->defaultValue = $matches[1]; } elseif (preg_match("/^(.*?)::/", $column->defaultValue, $matches)) { - $column->defaultValue = $column->typecast($matches[1]); + $column->defaultValue = $column->phpTypecast($matches[1]); } else { - $column->defaultValue = $column->typecast($column->defaultValue); + $column->defaultValue = $column->phpTypecast($column->defaultValue); } } } diff --git a/framework/db/sqlite/QueryBuilder.php b/framework/db/sqlite/QueryBuilder.php index d97dbc9b05..34ebb523ae 100644 --- a/framework/db/sqlite/QueryBuilder.php +++ b/framework/db/sqlite/QueryBuilder.php @@ -80,7 +80,7 @@ class QueryBuilder extends \yii\db\QueryBuilder $vs = []; foreach ($row as $i => $value) { if (!is_array($value) && isset($columnSchemas[$columns[$i]])) { - $value = $columnSchemas[$columns[$i]]->typecast($value); + $value = $columnSchemas[$columns[$i]]->dbTypecast($value); } if (is_string($value)) { $value = $this->db->quoteValue($value); diff --git a/framework/db/sqlite/Schema.php b/framework/db/sqlite/Schema.php index e915d257e9..c7e13330db 100644 --- a/framework/db/sqlite/Schema.php +++ b/framework/db/sqlite/Schema.php @@ -252,7 +252,7 @@ class Schema extends \yii\db\Schema $column->defaultValue = new Expression('CURRENT_TIMESTAMP'); } else { $value = trim($info['dflt_value'], "'\""); - $column->defaultValue = $column->typecast($value); + $column->defaultValue = $column->phpTypecast($value); } } diff --git a/tests/unit/extensions/sphinx/ColumnSchemaTest.php b/tests/unit/extensions/sphinx/ColumnSchemaTest.php index 88965fc64b..fb0e67a3f2 100644 --- a/tests/unit/extensions/sphinx/ColumnSchemaTest.php +++ b/tests/unit/extensions/sphinx/ColumnSchemaTest.php @@ -50,6 +50,6 @@ class ColumnSchemaTest extends SphinxTestCase $columnSchema = new ColumnSchema(); $columnSchema->type = $type; $columnSchema->phpType = $phpType; - $this->assertEquals($expectedResult, $columnSchema->typecast($value)); + $this->assertEquals($expectedResult, $columnSchema->phpTypecast($value)); } }