SchemaMysql
in package
Database Schema Management Class
This class provides a fluent interface for creating and modifying MySQL or SQLite tables programmatically. It allows for defining fields, indexes, and table properties in a clean, object-oriented way.
Tags
Table of Contents
Properties
- $last_error : string|null
- $table : string
Methods
- __construct() : mixed
- boolean() : self
- create() : bool
- date() : self
- datetime() : self
- decimal() : self
- drop() : bool
- exists() : bool
- get_fields() : array<string|int, mixed>
- get_last_error() : mixed
- id() : self
- Creates an auto-incrementing primary key field
- index() : self
- int() : self
- Creates an integer field
- longtext() : self
- modify() : bool
- ALTER TABLE METHODS - Versione migliorata per MySQL
- set_primary_key() : self
- string() : self
- Creates a string (VARCHAR) field
- text() : self
- Creates a text field
- time() : self
- timestamp() : self
- tinyint() : self
- Creates a tiny integer field
Properties
$last_error
public
string|null
$last_error
= null
$table
public
string
$table
Methods
__construct()
public
__construct(string $table[, mixed $db = null ]) : mixed
Parameters
- $table : string
- $db : mixed = null
boolean()
public
boolean(string $name[, bool $null = false ][, bool|null $default = false ][, string|null $after = null ]) : self
Parameters
- $name : string
- $null : bool = false
- $default : bool|null = false
- $after : string|null = null
Return values
selfcreate()
public
create() : bool
Return values
booldate()
public
date(string $name[, bool $null = false ][, string|null $default = null ][, string|null $after = null ]) : self
Parameters
- $name : string
- $null : bool = false
- $default : string|null = null
- $after : string|null = null
Return values
selfdatetime()
public
datetime(string $name[, bool $null = false ][, string|null $default = null ][, string|null $after = null ]) : self
Parameters
- $name : string
- $null : bool = false
- $default : string|null = null
- $after : string|null = null
Return values
selfdecimal()
public
decimal(string $name[, int $precision = 10 ][, int $scale = 2 ][, bool $null = false ][, float|null $default = null ][, string|null $after = null ]) : self
Parameters
- $name : string
- $precision : int = 10
- $scale : int = 2
- $null : bool = false
- $default : float|null = null
- $after : string|null = null
Return values
selfdrop()
public
drop() : bool
Return values
boolexists()
public
exists() : bool
Return values
boolget_fields()
public
get_fields() : array<string|int, mixed>
Return values
array<string|int, mixed>get_last_error()
public
get_last_error() : mixed
id()
Creates an auto-incrementing primary key field
public
id([string $name = 'id' ]) : self
This method defines a primary key field that automatically increments with each new record. By default, the field is named 'id', but you can specify a different name if needed.
Parameters
- $name : string = 'id'
-
The name of the primary key field (default: 'id')
Tags
Return values
self —Returns the Schema instance for method chaining
index()
public
index(string $name, array<string|int, mixed> $columns[, bool $unique = false ]) : self
Parameters
- $name : string
- $columns : array<string|int, mixed>
- $unique : bool = false
Return values
selfint()
Creates an integer field
public
int(string $name[, bool $null = false ][, int|null $default = null ][, string|null $after = null ]) : self
This method defines an INT field in the database table.
Parameters
- $name : string
-
The name of the field
- $null : bool = false
-
Whether the field can be NULL (default: false)
- $default : int|null = null
-
The default value for the field (default: null)
- $after : string|null = null
-
The field after which this field should be positioned (default: null)
Tags
Return values
self —Returns the Schema instance for method chaining
longtext()
public
longtext(string $name[, bool $null = false ][, string|null $after = null ]) : self
Parameters
- $name : string
- $null : bool = false
- $after : string|null = null
Return values
selfmodify()
ALTER TABLE METHODS - Versione migliorata per MySQL
public
modify([mixed $force_update = false ]) : bool
Parameters
- $force_update : mixed = false
Return values
boolset_primary_key()
public
set_primary_key(array<string|int, mixed> $fields) : self
Parameters
- $fields : array<string|int, mixed>
Return values
selfstring()
Creates a string (VARCHAR) field
public
string(string $name[, int $length = 255 ][, bool $null = false ][, string|null $default = null ][, string|null $after = null ]) : self
This method defines a VARCHAR field in the database table. VARCHAR fields are used for storing variable-length string data.
Parameters
- $name : string
-
The name of the field
- $length : int = 255
-
The maximum length of the string (default: 255)
- $null : bool = false
-
Whether the field can be NULL (default: false)
- $default : string|null = null
-
The default value for the field (default: null)
- $after : string|null = null
-
The field after which this field should be positioned (default: null)
Tags
Return values
self —Returns the Schema instance for method chaining
text()
Creates a text field
public
text(string $name[, bool $null = false ][, string|null $after = null ]) : self
This method defines a TEXT field in the database table. TEXT fields are used for storing large amounts of text data (up to 65,535 characters).
Parameters
- $name : string
-
The name of the field
- $null : bool = false
-
Whether the field can be NULL (default: false)
- $after : string|null = null
-
The field after which this field should be positioned (default: null)
Tags
Return values
self —Returns the Schema instance for method chaining
time()
public
time(string $name[, bool $null = false ][, string|null $default = null ][, string|null $after = null ]) : self
Parameters
- $name : string
- $null : bool = false
- $default : string|null = null
- $after : string|null = null
Return values
selftimestamp()
public
timestamp(string $name[, bool $null = false ][, string|null $default = 'CURRENT_TIMESTAMP' ][, string|null $after = null ]) : self
Parameters
- $name : string
- $null : bool = false
- $default : string|null = 'CURRENT_TIMESTAMP'
- $after : string|null = null
Return values
selftinyint()
Creates a tiny integer field
public
tinyint(string $name[, bool $null = false ][, int|null $default = null ][, string|null $after = null ]) : self
This method defines a TINYINT field in the database table. TINYINT fields can store values from -128 to 127, or 0 to 255 if unsigned.
Parameters
- $name : string
-
The name of the field
- $null : bool = false
-
Whether the field can be NULL (default: false)
- $default : int|null = null
-
The default value for the field (default: null)
- $after : string|null = null
-
The field after which this field should be positioned (default: null)
Tags
Return values
self —Returns the Schema instance for method chaining