Fixes #13019: Support JSON in SchemaBuilderTrait

This commit is contained in:
Zhukov Roman
2018-02-06 16:35:49 +03:00
committed by Alexander Makarov
parent 46848f0051
commit 40b038379f
15 changed files with 107 additions and 15 deletions

View File

@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.14 under development
------------------------
- Enh #13019: Support JSON in SchemaBuilderTrait (zhukovra, undefinedor)
- Enh #15047: `yii\db\Query::select()` and `yii\db\Query::addSelect()` now check for duplicate column names (wapmorgan)
- Enh #14643: Added `yii\web\ErrorAction::$layout` property to conveniently set layout from error action config (swods, cebe, samdark)
- Enh #13465: Added `yii\helpers\FileHelper::findDirectory()` method (ArsSirek, developeruz)

View File

@ -114,13 +114,33 @@ class ColumnSchema extends BaseObject
*/
protected function typecast($value)
{
if ($value === '' && !in_array($this->type, [Schema::TYPE_TEXT, Schema::TYPE_STRING, Schema::TYPE_BINARY, Schema::TYPE_CHAR], true)) {
if ($value === ''
&& !in_array(
$this->type,
[
Schema::TYPE_TEXT,
Schema::TYPE_STRING,
Schema::TYPE_BINARY,
Schema::TYPE_CHAR
],
true)
) {
return null;
}
if ($value === null || gettype($value) === $this->phpType || $value instanceof ExpressionInterface || $value instanceof Query) {
if ($value === null
|| gettype($value) === $this->phpType
|| $value instanceof ExpressionInterface
|| $value instanceof Query
) {
return $value;
}
if (is_array($value) && count($value) === 2 && isset($value[1]) && in_array($value[1], $this->getPdoParamTypes(), true)) {
if (is_array($value)
&& count($value) === 2
&& isset($value[1])
&& in_array($value[1], $this->getPdoParamTypes(), true)
) {
return new PdoValue($value[0], $value[1]);
}

View File

@ -272,4 +272,25 @@ trait SchemaBuilderTrait
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_MONEY, $length);
}
/**
* Creates a JSON column.
* @return ColumnSchemaBuilder the column instance which can be further customized.
* @since 2.0.14
* @throws \yii\base\Exception
*/
public function json()
{
/*
* TODO Remove in Yii 2.1
*
* Disabled due to bug in MySQL extension
* @link https://bugs.php.net/bug.php?id=70384
*/
if (version_compare(PHP_VERSION, '5.6', '<') && $this->getDb()->getDriverName() === 'mysql') {
throw new \yii\base\Exception('JSON column type is not supported in PHP < 5.6');
}
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_JSON);
}
}

View File

@ -44,6 +44,7 @@ class QueryBuilder extends \yii\db\QueryBuilder
Schema::TYPE_BINARY => 'blob',
Schema::TYPE_BOOLEAN => 'tinyint(1)',
Schema::TYPE_MONEY => 'decimal(19,4)',
Schema::TYPE_JSON => 'json'
];
/**

View File

@ -67,6 +67,7 @@ class Schema extends \yii\db\Schema
'timestamp' => self::TYPE_TIMESTAMP,
'enum' => self::TYPE_STRING,
'varbinary' => self::TYPE_BINARY,
'json' => self::TYPE_JSON,
];
/**

View File

@ -71,7 +71,7 @@ class QueryBuilder extends \yii\db\QueryBuilder
Schema::TYPE_BINARY => 'bytea',
Schema::TYPE_BOOLEAN => 'boolean',
Schema::TYPE_MONEY => 'numeric(19,4)',
Schema::TYPE_JSON => 'jsonb'
Schema::TYPE_JSON => 'jsonb',
];
/**