diff --git a/src/Annotation/Property/Field.php b/src/Annotation/Property/Field.php index 3a6382c..3f97dfb 100644 --- a/src/Annotation/Property/Field.php +++ b/src/Annotation/Property/Field.php @@ -7,23 +7,23 @@ class Field implements \Ulmus\Annotation\Annotation { public string $type; public string $name; - + # public string $column; - + public int $length; public int $precision; public array $attributes = []; - public bool $nullable = false; + public bool $nullable; public function __construct(? string $type = null, ? int $length = null) { if ( $type !== null ) { $this->type = $type; } - + if ( $length !== null ) { $this->length = $length; } diff --git a/src/Common/EntityField.php b/src/Common/EntityField.php index 8a0eb26..781f580 100644 --- a/src/Common/EntityField.php +++ b/src/Common/EntityField.php @@ -58,7 +58,7 @@ class EntityField return implode(" ", [ $definition->getSqlName(), $definition->getSqlType(), - + $definition->getSqlParams(), ]); } diff --git a/src/Migration/FieldDefinition.php b/src/Migration/FieldDefinition.php index c96215c..c129c04 100644 --- a/src/Migration/FieldDefinition.php +++ b/src/Migration/FieldDefinition.php @@ -28,11 +28,12 @@ class FieldDefinition { $this->builtIn = $data['builtin']; $this->tags = $data['tags']; + $field = $this->getFieldTag(); $this->type = $field->type ?? $data['type']; $this->length = $field->length ?? null; $this->precision = $field->precision ?? null; - $this->nullable = $field->nullable ?? $data['nullable']; + $this->nullable = isset($field->nullable) ? $field->nullable : $data['nullable']; $this->update = $field->attributes['update'] ?? null; } @@ -41,11 +42,10 @@ class FieldDefinition { return $this->getColumnName(); } - public function getSqlType() : string + public function getSqlType(bool $typeOnly = false) : string { $type = $this->type; $length = $this->length; - $default = $this->getDefault(); switch($type) { case "bool": @@ -86,8 +86,14 @@ class FieldDefinition { break; } + return $typeOnly ? $type : $type . ( $length ? "($length" . ( $precision ? ",$precision" : "" ) . ")" : "" ); + } + + public function getSqlParams() : string + { + $default = $this->getDefault(); + return implode(' ', array_filter([ - $type . ( $length ? "($length" . ( $precision ? ",$precision" : "" ) . ")" : "" ), $this->isUnsigned() ? "UNSIGNED" : null, $this->nullable ? "NULL" : "NOT NULL", $default ? "DEFAULT $default" : null, @@ -112,7 +118,7 @@ class FieldDefinition { protected function getDefault() : ? string { - return $this->getFieldTag()->attributes['default'] ?? null; + return $this->getFieldTag()->attributes['default'] ?? ( $this->nullable ? "NULL" : null ) ; } protected function isPrimaryKey() : bool