MilkCore

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
example
// Create a new users table
$schema = Get::schema('#__users');
$schema->id()
    ->string('username', 100)
    ->string('email', 255)
    ->string('password', 255)
    ->int('status', false, 1)
    ->timestamp('created_at')
    ->index('email', 'email')
    ->create();

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

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
self

date()

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
self

datetime()

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
self

decimal()

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
self

get_fields()

public get_fields() : array<string|int, mixed>
Return values
array<string|int, 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
example
// Default primary key
$schema->id();

// Custom primary key name
$schema->id('user_id');
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
self

int()

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
example
// Required integer field
$schema->int('age');

// Nullable integer field
$schema->int('parent_id', true);

// Integer field with default value
$schema->int('status', false, 1);

// Integer field positioned after another field
$schema->int('sort_order', false, 0, 'id');
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
self

modify()

ALTER TABLE METHODS - Versione migliorata per MySQL

public modify([mixed $force_update = false ]) : bool
Parameters
$force_update : mixed = false
Return values
bool

set_primary_key()

public set_primary_key(array<string|int, mixed> $fields) : self
Parameters
$fields : array<string|int, mixed>
Return values
self

string()

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
example
// Standard string field with default length (255)
$schema->string('title');

// String field with custom length
$schema->string('username', 100);

// Nullable string field
$schema->string('middle_name', 50, true);

// String field with default value
$schema->string('status', 20, false, 'active');

// String field positioned after another field
$schema->string('notes', 200, false, '', 'status');
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
example
// Required text field
$schema->text('content');

// Nullable text field
$schema->text('description', true);

// Text field positioned after another field
$schema->text('notes', false, 'content');
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
self

timestamp()

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
self

tinyint()

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
example
// Boolean-like field (0 or 1)
$schema->tinyint('is_active', false, 1);

// Small numeric field
$schema->tinyint('priority', false, 5);
Return values
self

Returns the Schema instance for method chaining


        
On this page

Search results