mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-10 02:13:17 +08:00
Added typecasting for SQL insertion and update.
This commit is contained in:
@ -88,6 +88,9 @@ class ColumnSchema extends \yii\base\Component
|
||||
if ($value === null || gettype($value) === $this->phpType || $value instanceof Expression) {
|
||||
return $value;
|
||||
}
|
||||
if ($value === '' && $this->type !== Schema::TYPE_TEXT && $this->type !== Schema::TYPE_STRING) {
|
||||
return null;
|
||||
}
|
||||
switch ($this->phpType) {
|
||||
case 'string':
|
||||
return (string)$value;
|
||||
|
||||
@ -94,6 +94,11 @@ class QueryBuilder extends \yii\base\Object
|
||||
*/
|
||||
public function insert($table, $columns, &$params)
|
||||
{
|
||||
if (($tableSchema = $this->db->getTableSchema($table)) !== null) {
|
||||
$columnSchemas = $tableSchema->columns;
|
||||
} else {
|
||||
$columnSchemas = array();
|
||||
}
|
||||
$names = array();
|
||||
$placeholders = array();
|
||||
foreach ($columns as $name => $value) {
|
||||
@ -106,7 +111,7 @@ class QueryBuilder extends \yii\base\Object
|
||||
} else {
|
||||
$phName = self::PARAM_PREFIX . count($params);
|
||||
$placeholders[] = $phName;
|
||||
$params[$phName] = $value;
|
||||
$params[$phName] = isset($columnSchemas[$name]) ? $columnSchemas[$name]->typecast($value) : $value;
|
||||
}
|
||||
}
|
||||
|
||||
@ -164,6 +169,12 @@ class QueryBuilder extends \yii\base\Object
|
||||
*/
|
||||
public function update($table, $columns, $condition, &$params)
|
||||
{
|
||||
if (($tableSchema = $this->db->getTableSchema($table)) !== null) {
|
||||
$columnSchemas = $tableSchema->columns;
|
||||
} else {
|
||||
$columnSchemas = array();
|
||||
}
|
||||
|
||||
$lines = array();
|
||||
foreach ($columns as $name => $value) {
|
||||
if ($value instanceof Expression) {
|
||||
@ -174,7 +185,7 @@ class QueryBuilder extends \yii\base\Object
|
||||
} else {
|
||||
$phName = self::PARAM_PREFIX . count($params);
|
||||
$lines[] = $this->db->quoteColumnName($name) . '=' . $phName;
|
||||
$params[$phName] = $value;
|
||||
$params[$phName] = isset($columnSchemas[$name]) ? $columnSchemas[$name]->typecast($value) : $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -161,6 +161,12 @@ class QueryBuilder extends \yii\db\QueryBuilder
|
||||
*/
|
||||
public function batchInsert($table, $columns, $rows)
|
||||
{
|
||||
if (($tableSchema = $this->db->getTableSchema($table)) !== null) {
|
||||
$columnSchemas = $tableSchema->columns;
|
||||
} else {
|
||||
$columnSchemas = array();
|
||||
}
|
||||
|
||||
foreach ($columns as $i => $name) {
|
||||
$columns[$i] = $this->db->quoteColumnName($name);
|
||||
}
|
||||
@ -168,7 +174,10 @@ class QueryBuilder extends \yii\db\QueryBuilder
|
||||
$values = array();
|
||||
foreach ($rows as $row) {
|
||||
$vs = array();
|
||||
foreach ($row as $value) {
|
||||
foreach ($row as $i => $value) {
|
||||
if (isset($columnSchemas[$columns[$i]])) {
|
||||
$value = $columnSchemas[$columns[$i]]->typecast($value);
|
||||
}
|
||||
$vs[] = is_string($value) ? $this->db->quoteValue($value) : $value;
|
||||
}
|
||||
$values[] = '(' . implode(', ', $vs) . ')';
|
||||
|
||||
Reference in New Issue
Block a user