Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit a934859

Browse files
committed
Fix-Nullable (#99)
fixes #99
1 parent 7babecd commit a934859

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,52 @@ $this->update('{{%company}}', ['name' => 'No name']);
248248
$this->alterColumn('{{%company}}', 'name', $this->string(128)->notNull());
249249
```
250250

251+
### Handling of `NOT NULL` constraints (#required, #nullable)
252+
253+
e.g. attribute = 'alpha'.
254+
255+
1) If you define attribute neither "required" nor via "nullable", then it is by default:
256+
ALLOW 'NOT NULL'
257+
```yaml
258+
test_table:
259+
required:
260+
properties:
261+
alpha:
262+
type: string
263+
```
264+
265+
2) If you define attribute by "required", then it is:
266+
FORBIDDEN 'NOT NULL'
267+
```yaml
268+
test_table:
269+
required:
270+
- alpha
271+
properties:
272+
alpha:
273+
type: string
274+
```
275+
276+
3) If you define attribute via "nullable", then it has the highest priority.
277+
ALLOW 'NOT NULL'
278+
```yaml
279+
test_table:
280+
required:
281+
- alpha
282+
properties:
283+
alpha:
284+
type: string
285+
nullable: true
286+
```
287+
288+
FORBIDDEN 'NOT NULL'
289+
```yaml
290+
test_table:
291+
required:
292+
properties:
293+
alpha:
294+
type: string
295+
nullable: false
296+
```
251297
252298
## Screenshots
253299

src/lib/AttributeResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ protected function resolveProperty(PropertySchema $property, bool $isRequired):v
204204
->setReadOnly($property->isReadonly())
205205
->setDefault($property->guessDefault())
206206
->setXDbType($property->getAttr('x-db-type', null))
207+
->setNullable($property->getProperty()->getSerializableData()->nullable ?? null)
207208
->setIsPrimary($property->isPrimaryKey());
208209
if ($property->isReference()) {
209210
if ($property->isVirtual()) {

src/lib/items/Attribute.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,16 @@ class Attribute extends BaseObject
4848

4949
/**
5050
* Custom db type
51+
* string | null
5152
*/
5253
public $xDbType;
5354

55+
/**
56+
* nullable
57+
* bool | null
58+
*/
59+
public $nullable;
60+
5461
/**
5562
* @var string
5663
*/
@@ -128,9 +135,10 @@ public function setXDbType($xDbType):Attribute
128135
return $this;
129136
}
130137

131-
public function getXDbType($xDbType)
138+
public function setNullable($nullable):Attribute
132139
{
133-
return $this->xDbType;
140+
$this->nullable = $nullable;
141+
return $this;
134142
}
135143

136144
public function setDescription(string $description):Attribute
@@ -263,7 +271,7 @@ public function toColumnSchema():ColumnSchema
263271
'phpType'=>$this->phpType,
264272
'dbType' => strtolower($this->dbType),
265273
'type' => $this->dbTypeAbstract($this->dbType),
266-
'allowNull' => !$this->isRequired(),
274+
'allowNull' => $this->allowNull(),
267275
'size' => $this->size > 0 ? $this->size : null,
268276
]);
269277
$column->isPrimaryKey = $this->primary;
@@ -313,4 +321,12 @@ private function dbTypeAbstract(string $type):string
313321
}
314322
return $type;
315323
}
324+
325+
private function allowNull()
326+
{
327+
if (is_bool($this->nullable)){
328+
return $this->nullable;
329+
}
330+
return !$this->isRequired();
331+
}
316332
}

0 commit comments

Comments
 (0)