mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 14:46:19 +08:00 
			
		
		
		
	Added flags to ColumnSchema to disable JSON and Arrays support
				
					
				
			This commit is contained in:
		@ -4,6 +4,7 @@ Yii Framework 2 Change Log
 | 
			
		||||
2.0.14.1 under development
 | 
			
		||||
--------------------------
 | 
			
		||||
 | 
			
		||||
- Enh #15716: Added `disableJsonSupport` to MySQL and PgSQL `ColumnSchema`, `disableArraySupport` and `deserializeArrayColumnToArrayExpression` to PgSQL `ColumnSchema` (silverfire)
 | 
			
		||||
- Bug #15728, #15731: Fixed BC break in `Query::select()` method (silverfire)
 | 
			
		||||
- Bug #15692: Fix ExistValidator with targetRelation ignores filter (developeruz)
 | 
			
		||||
- Bug #15693: Fixed `yii\filters\auth\HttpHeaderAuth` to work correctly when pattern is set but was not matched (bboure)
 | 
			
		||||
 | 
			
		||||
@ -63,8 +63,15 @@ Upgrade from Yii 2.0.13
 | 
			
		||||
  introduced methods to retrieve the expression content.
 | 
			
		||||
 | 
			
		||||
* Added JSON support for PostgreSQL and MySQL as well as Arrays support for PostgreSQL in ActiveRecord layer.
 | 
			
		||||
  In case you already implemented such support yourself, please switch to Yii implementation. Active Record will
 | 
			
		||||
  return arrays instead of strings after data population and expects arrays to be assigned for further saving them into database.
 | 
			
		||||
  In case you already implemented such support yourself, please switch to Yii implementation.
 | 
			
		||||
  * For MySQL JSON and PgSQL JSON & JSONB columns Active Record will return decoded JSON (that can be either array or scalar) after data population
 | 
			
		||||
  and expects arrays or scalars to be assigned for further saving them into a database.
 | 
			
		||||
  * For PgSQL Array columns Active Record will return `yii\db\ArrayExpression` object that acts as an array
 | 
			
		||||
  (it implements `ArrayAccess`, `Traversable` and `Countable` interfaces) and expects array or `yii\db\ArrayExpression` to be
 | 
			
		||||
  assigned for further saving it into the database.
 | 
			
		||||
 | 
			
		||||
  In case this change makes the upgrade process to Yii 2.0.14 too hard in your project, you can [switch off the described behavior](https://github.com/yiisoft/yii2/issues/15716#issuecomment-368143206)
 | 
			
		||||
  Then you can take your time to change your code and then re-enable arrays or JSON support.
 | 
			
		||||
 | 
			
		||||
* `yii\db\PdoValue` class has been introduced to replace a special syntax that was used to declare PDO parameter type 
 | 
			
		||||
  when binding parameters to an SQL command, for example: `['value', \PDO::PARAM_STR]`.
 | 
			
		||||
 | 
			
		||||
@ -87,7 +87,7 @@ abstract class Schema extends BaseObject
 | 
			
		||||
        'SQLSTATE[23' => 'yii\db\IntegrityException',
 | 
			
		||||
    ];
 | 
			
		||||
    /**
 | 
			
		||||
     * @var string column schema class
 | 
			
		||||
     * @var string|array column schema class or class config
 | 
			
		||||
     * @since 2.0.11
 | 
			
		||||
     */
 | 
			
		||||
    public $columnSchemaClass = 'yii\db\ColumnSchema';
 | 
			
		||||
 | 
			
		||||
@ -11,13 +11,23 @@ use yii\db\ExpressionInterface;
 | 
			
		||||
use yii\db\JsonExpression;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Class ColumnSchema
 | 
			
		||||
 * Class ColumnSchema for MySQL database
 | 
			
		||||
 *
 | 
			
		||||
 * @author Dmytro Naumenko <d.naumenko.a@gmail.com>
 | 
			
		||||
 * @since 2.0.14.1
 | 
			
		||||
 */
 | 
			
		||||
class ColumnSchema extends \yii\db\ColumnSchema
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @var bool whether the column schema should OMIT using JSON support feature.
 | 
			
		||||
     * You can use this property to make upgrade to Yii 2.0.14 easier.
 | 
			
		||||
     * Default to `false`, meaning JSON support is enabled.
 | 
			
		||||
     *
 | 
			
		||||
     * @since 2.0.14.1
 | 
			
		||||
     * @deprecated Since 2.0.14.1 and will be removed in 2.1.
 | 
			
		||||
     */
 | 
			
		||||
    public $disableJsonSupport = false;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * {@inheritdoc}
 | 
			
		||||
     */
 | 
			
		||||
@ -27,7 +37,7 @@ class ColumnSchema extends \yii\db\ColumnSchema
 | 
			
		||||
            return $value;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ($this->dbType === Schema::TYPE_JSON) {
 | 
			
		||||
        if (!$this->disableJsonSupport && $this->dbType === Schema::TYPE_JSON) {
 | 
			
		||||
            return new JsonExpression($value, $this->type);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -43,7 +53,7 @@ class ColumnSchema extends \yii\db\ColumnSchema
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ($this->type === Schema::TYPE_JSON) {
 | 
			
		||||
        if (!$this->disableJsonSupport && $this->type === Schema::TYPE_JSON) {
 | 
			
		||||
            return json_decode($value, true);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -12,7 +12,7 @@ use yii\db\ExpressionInterface;
 | 
			
		||||
use yii\db\JsonExpression;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Class ColumnSchema
 | 
			
		||||
 * Class ColumnSchema for PostgreSQL database.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Dmytro Naumenko <d.naumenko.a@gmail.com>
 | 
			
		||||
 */
 | 
			
		||||
@ -23,6 +23,35 @@ class ColumnSchema extends \yii\db\ColumnSchema
 | 
			
		||||
     */
 | 
			
		||||
    public $dimension = 0;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @var bool whether the column schema should OMIT using JSON support feature.
 | 
			
		||||
     * You can use this property to make upgrade to Yii 2.0.14 easier.
 | 
			
		||||
     * Default to `false`, meaning JSON support is enabled.
 | 
			
		||||
     *
 | 
			
		||||
     * @since 2.0.14.1
 | 
			
		||||
     * @deprecated Since 2.0.14.1 and will be removed in 2.1.
 | 
			
		||||
     */
 | 
			
		||||
    public $disableJsonSupport = false;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @var bool whether the column schema should OMIT using PgSQL Arrays support feature.
 | 
			
		||||
     * You can use this property to make upgrade to Yii 2.0.14 easier.
 | 
			
		||||
     * Default to `false`, meaning Arrays support is enabled.
 | 
			
		||||
     *
 | 
			
		||||
     * @since 2.0.14.1
 | 
			
		||||
     * @deprecated Since 2.0.14.1 and will be removed in 2.1.
 | 
			
		||||
     */
 | 
			
		||||
    public $disableArraySupport = false;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @var bool whether the Array column value should be unserialized to an [[ArrayExpression]] object.
 | 
			
		||||
     * You can use this property to make upgrade to Yii 2.0.14 easier.
 | 
			
		||||
     * Default to `true`, meaning arrays are unserilized to [[ArrayExpression]] objects.
 | 
			
		||||
     *
 | 
			
		||||
     * @since 2.0.14.1
 | 
			
		||||
     * @deprecated Since 2.0.14.1 and will be removed in 2.1.
 | 
			
		||||
     */
 | 
			
		||||
    public $deserializeArrayColumnToArrayExpression = true;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * {@inheritdoc}
 | 
			
		||||
@ -33,10 +62,10 @@ class ColumnSchema extends \yii\db\ColumnSchema
 | 
			
		||||
            return $value;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ($this->dimension > 0) {
 | 
			
		||||
        if (!$this->disableArraySupport && $this->dimension > 0) {
 | 
			
		||||
            return new ArrayExpression($value, $this->dbType, $this->dimension);
 | 
			
		||||
        }
 | 
			
		||||
        if (in_array($this->dbType, [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {
 | 
			
		||||
        if (!$this->disableJsonSupport && in_array($this->dbType, [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) {
 | 
			
		||||
            return new JsonExpression($value, $this->type);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -48,7 +77,7 @@ class ColumnSchema extends \yii\db\ColumnSchema
 | 
			
		||||
     */
 | 
			
		||||
    public function phpTypecast($value)
 | 
			
		||||
    {
 | 
			
		||||
        if ($this->dimension > 0) {
 | 
			
		||||
        if (!$this->disableArraySupport && $this->dimension > 0) {
 | 
			
		||||
            if (!is_array($value)) {
 | 
			
		||||
                $value = $this->getArrayParser()->parse($value);
 | 
			
		||||
            }
 | 
			
		||||
@ -58,7 +87,9 @@ class ColumnSchema extends \yii\db\ColumnSchema
 | 
			
		||||
                });
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return new ArrayExpression($value, $this->dbType, $this->dimension);
 | 
			
		||||
            return $this->deserializeArrayColumnToArrayExpression
 | 
			
		||||
                ? new ArrayExpression($value, $this->dbType, $this->dimension)
 | 
			
		||||
                : $value;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $this->phpTypecastValue($value);
 | 
			
		||||
@ -88,7 +119,7 @@ class ColumnSchema extends \yii\db\ColumnSchema
 | 
			
		||||
                }
 | 
			
		||||
                return (bool) $value;
 | 
			
		||||
            case Schema::TYPE_JSON:
 | 
			
		||||
                return json_decode($value, true);
 | 
			
		||||
                return $this->disableJsonSupport ? $value : json_decode($value, true);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return parent::phpTypecast($value);
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user