1. Go to this page and download the library: Download butschster/dbml-parser library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
butschster / dbml-parser example snippets
use Butschster\Dbml\DbmlParserFactory;
$parser = DbmlParserFactory::create();
$schema = $parser->parse(<<<DBML
Project test {
database_type: 'PostgreSQL'
Note: 'Description of the project'
}
//// -- LEVEL 1
//// -- Tables and References
// Creating tables
Table users as U {
id int [pk, unique, increment] // auto-increment
full_name varchar [not null, unique, default: 1]
created_at timestamp
country_code int
type int
note int
Note: 'khong hieu duoc'
}
Table merchants {
id int
}
Table countries {
code int [pk]
name varchar
continent_name varchar
}
// Creating references
// You can also define relationship separately
// > many-to-one; < one-to-many; - one-to-one
Ref{
U.country_code > countries.code
merchants.country_code > countries.code
}
//----------------------------------------------//
//// -- LEVEL 2
//// -- Adding column settings
Table order_items {
order_id int [ref: > orders.id]
product_id int
quantity int [default: 1] // default value
}
Ref: order_items.product_id > products.id
Table orders {
id int [pk] // primary key
user_id int [not null, unique]
status varchar
created_at varchar [note: '''When order created'''] // add column note
}
Table int {
id int
}
//----------------------------------------------//
//// -- Level 3
//// -- Enum, Indexes
// Enum for 'products' table below
Enum products_status {
out_of_stock
in_stock
running_low [note: 'less than 20'] // add column note
}
// Indexes: You can define a single or multi-column index
Table products {
id int [pk]
name varchar
merchant_id int [not null]
price int
status products_status
created_at datetime [default: `now()`]
Indexes {
(merchant_id, status) [name:'product_status', type: hash]
id [unique]
}
}
Ref: products.merchant_id > merchants.id // many-to-one
TableGroup hello_world {
just_test
just_a_test
}
DBML
);
// List of tables
$tables = $schema->getTables(); // \Butschster\Dbml\Ast\TableNode[]
// Check if table exists
$schema->hasTable('users');
// Get table by name
$table = $schema->getTable('users'); // \Butschster\Dbml\Ast\TableNode[]
// Get project data
$project = $schema->getProject(); // \Butschster\Dbml\Ast\ProjectNode
// Get table groups
$tableGroups = $schema->getTableGroups(); // \Butschster\Dbml\Ast\TableGroupNode[]
// Check if table group with given name exists
$schema->hasTableGroup('name');
// Get table group object by name
$tableGroup = $schema->getTableGroup('name'); // \Butschster\Dbml\Ast\TableGroupNode
// Get enums
$enums = $schema->getEnums(); // \Butschster\Dbml\Ast\EnumNode[]
// Check if enum with given name exists
$schema->hasEnum('name');
// Get enum object by name
$enum = $schema->getEnum('name'); // \Butschster\Dbml\Ast\EnumNode
// Get refs
$refs = $schema->getRefs(); // \Butschster\Dbml\Ast\RefNode[]
/** @var \Butschster\Dbml\Ast\TableNode $table */
$table = $schema->getTable('users');
$name = $table->getName(); // users
$alias = $table->getAlias(); // U
$note = $table->getNote(); // khong hieu duoc
// Get table columns
$columns = $table->getColumns(); // \Butschster\Dbml\Ast\Table\ColumnNode[]
// Check if table column exists
$table->hasColumn('id');
// Get column by name
$column = $table->getColumn('id');
// Get table indexes
$indexes = $table->getIndexes(); // \Butschster\Dbml\Ast\Table\IndexNode[]
/** @var \Butschster\Dbml\Ast\Table\ColumnNode $column */
$column = $schema->getTable('users')->getColumn('id');
$name = $column->getName(); // id
$type = $column->getType()->getName(); // int
$size = $column->getType()->getSize(); // null|int
$note = $column->getNote(); // string
$refs = $column->getRefs(); // \Butschster\Dbml\Ast\RefNode[]
/** @var \Butschster\Dbml\Ast\Values\IntNode $default */
$default = $column->getDefault();
$value = $default->getValue(); // 1
// Check if column is primary
$column->isPrimaryKey();
// Check if column is auto increment
$column->isIncrement();
// Check if column is unique
$column->isUnique();
// heck if column is nullable
$column->isNull();
/** @var \Butschster\Dbml\Ast\Table\IndexNode $index */
$index = $schema->getTable('products')->getIndexes()[0];
/** @var \Butschster\Dbml\Ast\Values\StringNode[]|\Butschster\Dbml\Ast\Values\ExpressionNode[] $columns */
$columns = $index->getColumns();
count($columns); // 2
$column1 = $index->getColumns()[0]->getValue(); // merchant_id
$column2 = $index->getColumns()[1]->getValue(); // status
$type = $index->getType(); // hash
$name = $index->getName(); // product_status
$note = $index->getNote();
// Check if index is pk
$index->isPrimaryKey();
// Check if index is unique
$index->isUnique();
/** @var \Butschster\Dbml\Ast\EnumNode $enum */
$enum = $schema->getEnum('products_status');
$name = $enum->getName(); // products_status
// Get amount of values
$enum->count(); // 3
// Check if enum contains value
$enum->hasValue('out_of_stock'); // true
// Get enum value object by name
$value = $enum->getValue('running_low'); // \Butschster\Dbml\Ast\Enum\ValueNode
$note = $value->getNote();
$value = $value->getValue();
/** @var \Butschster\Dbml\Ast\TableGroupNode $group */
$group = $schema->getTableGroup('hello_world');
$name = $group->getName(); // hello_world
// Check if table with given name contains in this group
$group->hasTable('just_test');
// Get list of tables
$tables = $group->getTables(); // string[]