Milk Admin
Schema Class
Create and modify database tables (MySQL or SQLite) using a fluent API.
Create a Schema Instance
$schema = Get::schema('users');
You can pass a specific connection (e.g. db2):
$schema = Get::schema('users', Get::db2());
Field Methods (Examples)
$schema->id()
->string('username', 100, false)
->text('bio', true)
->int('age', false, 18)
->boolean('active', false, true)
->datetime('created_at');
$schema->decimal('price', 10, 2)
->date('birthday', true)
->time('start_time', true)
->timestamp('updated_at')
->tinyint('priority', false, 5)
->longtext('notes', true);
Indexes and Primary Keys
$schema->index('email_idx', ['email'])
->index('name_email_idx', ['name', 'email'])
->index('username_idx', ['username'], true);
Composite Primary Key: for MySQL you can use
$schema->setPrimaryKey(['post_id', 'tag_id']);.
On SQLite, prefer a unique composite index.
Create, Modify, Drop
$schema = Get::schema('users');
$schema->id()
->string('username', 100)
->string('email', 100)
->string('password', 255)
->text('bio', true)
->boolean('active', false, true)
->datetime('created_at')
->datetime('updated_at', true)
->index('email_idx', ['email'], true)
->index('username_idx', ['username'], true)
->create();
$schema = Get::schema('users');
$schema->id()
->string('username', 150)
->string('email', 100)
->boolean('active')
->index('email_idx', ['email'])
->modify();
Rename a Column
$schema = Get::schema('users');
$schema->id()
->string('name', 150)
->renameField('full_name', 'name')
->modify();
$schema = Get::schema('temporary_table');
$schema->drop();
Introspection
$schema = Get::schema('users');
if ($schema->exists()) {
$fields = $schema->getFields();
}
$differences = $schema->getFieldDifferences();
$lastError = $schema->getLastError();
Complete Examples
$schema = Get::schema('posts');
$schema->id()
->string('title', 200)
->text('content')
->int('author_id')
->string('status', 20, false, 'draft')
->datetime('published_at', true)
->datetime('created_at')
->datetime('updated_at', true)
->index('author_id_idx', ['author_id'])
->index('status_idx', ['status'])
->index('published_idx', ['published_at'])
->create();
$schema = Get::schema('post_tags');
$schema->int('post_id')
->int('tag_id')
->datetime('created_at')
->index('post_id_idx', ['post_id'])
->index('tag_id_idx', ['tag_id'])
->index('post_tag_idx', ['post_id', 'tag_id'], true)
->create();
Loading...