Fixes #10422, #9574: New proposal fix for ColumnSchemaBuilder not null column

* Added ability to explicitly set column nullability #10422

* Removed isNull property.

* add since tag.
This commit is contained in:
Thiago
2016-06-18 09:03:55 -03:00
committed by Alexander Makarov
parent 56308eab1d
commit bafc8b3325
3 changed files with 26 additions and 8 deletions

View File

@ -5,6 +5,7 @@ Yii Framework 2 Change Log
2.0.9 under development
-----------------------
- Enh #10422: Added `null` method on `yii\db\ColumnSchemaBuilder` to explicitly set column nullability (nevermnd)
- Enh #8795: Refactored `yii\web\User::loginByCookie()` in order to make it easier to override (maine-mike, silverfire)
- Enh #9948: `yii\rbac\PhpManager` now invalidates script file cache performed by 'OPCache' or 'APC' on file saving (klimov-paul)
- Enh #11195: Added ability to append custom string to schema builder column definition (df2, samdark)

View File

@ -40,9 +40,10 @@ class ColumnSchemaBuilder extends Object
*/
protected $length;
/**
* @var boolean whether the column is not nullable. If this is `true`, a `NOT NULL` constraint will be added.
* @var boolean|null whether the column is or not nullable. If this is `true`, a `NOT NULL` constraint will be added.
* If this is `false`, a `NULL` constraint will be added.
*/
protected $isNotNull = false;
protected $isNotNull = null;
/**
* @var boolean whether the column values should be unique. If this is `true`, a `UNIQUE` constraint will be added.
*/
@ -76,7 +77,6 @@ class ColumnSchemaBuilder extends Object
*/
protected $isFirst;
/**
* @var array mapping of abstract column types (keys) to type categories (values).
* @since 2.0.8
@ -141,6 +141,17 @@ class ColumnSchemaBuilder extends Object
return $this;
}
/**
* Adds a `NULL` constraint to the column
* @return $this
* @since 2.0.9
*/
public function null()
{
$this->isNotNull = false;
return $this;
}
/**
* Adds a `UNIQUE` constraint to the column.
* @return $this
@ -286,11 +297,18 @@ class ColumnSchemaBuilder extends Object
/**
* Builds the not null constraint for the column.
* @return string returns 'NOT NULL' if [[isNotNull]] is true, otherwise it returns an empty string.
* @return string returns 'NOT NULL' if [[isNotNull]] is true,
* 'NULL' if [[isNotNull]] is false or an empty string otherwise.
*/
protected function buildNotNullString()
{
return $this->isNotNull ? ' NOT NULL' : '';
if ($this->isNotNull === true) {
return ' NOT NULL';
} elseif ($this->isNotNull === false) {
return ' NULL';
} else {
return '';
}
}
/**

View File

@ -9,7 +9,6 @@ namespace yiiunit\framework\db;
use yii\db\ColumnSchemaBuilder;
use yii\db\Exception;
use yii\db\Expression;
use yii\db\Schema;
use yiiunit\TestCase;
@ -35,8 +34,8 @@ class ColumnSchemaBuilderTest extends TestCase
public function typesProvider()
{
return [
['integer', Schema::TYPE_INTEGER, null, [
['unsigned'],
['integer NULL', Schema::TYPE_INTEGER, null, [
['unsigned'], ['null'],
]],
['integer(10)', Schema::TYPE_INTEGER, 10, [
['unsigned'],