Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Schema

Table Builder

» Schema

» Index

» Foreign_Key

» Examples


__construct(string $table_name, ?callable $configure = null )

  • @param string $table_name The name of your table, if you wish to use a prefix, you can set this during the build
  • @param callable⎮null $configure Used to set the schema values are the same time as declaring the schema object.
$schema = new Schema('table', function(Schema $schema): void{
    $schema->column('id');
});
// Can be also created  as 
$schema = new Schema('table');
$schema->column('id');

prefix( ?string $prefix = null ): self

  • @param string∣null $prefix
  • @return self

You can set an optional table name prefix either at Schema definition or during the build/drop processes.

$schema = new Schema('table');
$schema->prefix('my_'); // table name = my_table


// Using WPDB to get the current sites prefix (acounts for multisite)
global $wpdb;
$schema = new Schema('table');
$schema->prefix($wpdb->prefix); // table name = wp_table (assuming the prefix set in WPDB is wp_)

column( string $name ): Column

  • @param string $name
  • @return Column

Returns a partially constructed Column object, which has its own fluent API for defining the column details. See Column methods for a more detailed explanation.

$schema = new Schema('table', function(Schema $schema): void{
    // Using shortcut types
    $schema->column('id')
        ->text(11)          // Sets as text with a length of 11
        ->default('empty'); // Sets defualt to empty

    // Using verbose type definitions.
    $schema->column('user_id')
        ->type('text')      // Sets type to TEXT
        ->length(11)        // Sets length to 11
        ->default('empty'); // Sets defualt to empty
});

Shortcut types (passing null, will not set the length or default)
varchar( ?int $length = null )
text( ?int $length = null )
int( ?int $length = null )
float( ?int $length = null )
double( ?int $length = null )
datetime( ?string $default = null )
timestamp( ?string $default = null )
unsigned_int( ?int $length = null )
unsigned_medium( ?int $length = null )


has_column( string $name ): bool

  • @param string $name
  • @return bool

Checks if a column has been set based in its name.

$schema = new Schema('table', function(Schema $schema): void{
    // Using shortcut types
    $schema->column('id')->int(12)->auto_increment();
    $schema->column('user_id')->type('text')->default('empty'); 
});

$schema->has_column('user_id'); // TRUE

get_columns(): array

  • @return Column[]

Retruns an array of Column objects.

$schema = new Schema('table', function(Schema $schema): void{
    // Using shortcut types
    $schema->column('id')->int(12)->auto_increment();
    $schema->column('user_id')->type('text')->default('empty'); 
});

$schema->get_columns(); // [Column{name: id..}, Column{name: user_id....}]

remove_column( string $name ): self

  • @param string $name
  • @return self
  • @throws Exception If column doesn’t exist.

Removes a colum from the table, can be used to conditionally remove a column before being created. Will throw an exception of the column doesn’t exist.

$schema = new Schema('table', function(Schema $schema): void{
    // Using shortcut types
    $schema->column('id')->int(12)->auto_increment();
    $schema->column('user_id')->type('text')->default('empty'); 
    $schema->column('site_id')->type('text')->nullable();
});

// Only use site_id for multisites.
if(! is_multisite()){
    $schema->remove_column('site_id');
}

index( string $column, ?string $keyname = null ): Index

  • @param string $keyname
  • @return \PinkCrab\Table_Builder\Index

Sets an Index for the table, returns a partially populated Index instance bound to the defined column. The keyname can either be defined or is generated as “ix_{column_name}”. Multiple Indexes set with matching keynames will be defined as a single expression INDEX keyname (col1,col2,col3)

$schema = new Schema('table', function(Schema $schema): void{
    
    $schema->column('id')->unsigned_int(12)->auto_increment();
    $schema->column('user')->int(12);
    $schema->column('booking_ref')->varchar(16);

    $schema->index('id')->primary();
    $schema->index('user')->unique();
    $schema->index('booking_ref')->unique();
});

has_indexes(): bool

  • @return bool

Returns if the table has indexes applied.

$schema = new Schema('table', function(Schema $schema): void{
    
    $schema->column('id')->unsigned_int(12)->auto_increment();
    $schema->column('user')->int(12);
    $schema->column('booking_ref')->varchar(16);
    $schema->index('id')->primary();
});

$schema->has_indexes(); // TRUE

get_indexes(): array

  • @return \PinkCrab\Table_Builder\Index[]

Returns an array of all indexes currently set to the Schema.

$schema = new Schema('table', function(Schema $schema): void{
    
    $schema->column('id')->unsigned_int(12)->auto_increment();
    $schema->column('user')->int(12);
    $schema->column('booking_ref')->varchar(16);
    $schema->index('id')->primary();
    $schema->index('booking_ref')->unique();
});

$schema->get_indexes(); // [Index{column: id..}, Index{column: booking_ref..}]

foreign_key( string $column, ?string $keyname = null ): Foreign_Key

  • @param string $column Column index is applied to
  • @param string∣null $keyname The indexes reference
  • @return \PinkCrab\Table_Builder\Foreign_Key

Creates a Foreign Key relationship with another table. Can be set with a custom or default keyname. Returns a partially applied

$schema = new Schema('table', function(Schema $schema): void{
    
    $schema->column('id')->unsigned_int(12)->auto_increment();
    $schema->column('user')->int(12);

    $schema->foreign_key('user', 'user_fk')
        ->reference_table('users')
        ->reference_column('id');
});

has_foreign_keys(): bool

  • @return bool

Returns if the table has Foreign Keys applied.

$schema = new Schema('table', function(Schema $schema): void{
    
    $schema->column('id')->unsigned_int(12)->auto_increment();
    $schema->column('user')->int(12);
    $schema->column('booking_ref')->varchar(16);
    $schema->foreign_key('user', 'user_fk')
        ->reference_table('users')
        ->reference_column('id');
});

$schema->has_foreign_keys(); // TRUE

get_foreign_keys(): array

  • @return Foreign_Key[]

Returns an array of all Foreign Keys currently set to the Schema.

$schema = new Schema('table', function(Schema $schema): void{
    
    $schema->column('id')->unsigned_int(12)->auto_increment();
    $schema->column('user')->int(12);
    $schema->column('booking_ref')->varchar(16);
    $schema->index('id')->primary();
        $schema->foreign_key('user', 'user_fk')
        ->reference_table('users')
        ->reference_column('id');
});

$schema->get_indexes(); // [Foreign_Key{column: user..}]