com\setasign\SetaFramework\Database\Manager

TableSchema Class TableSchema

File: /SetaFramework/src/Database/Manager/TableSchema.php

Class hierarchy

Implements

Summary

Constants

INDEX

public const TableSchema::INDEX = 'index'

INDEX_FULLTEXT

public const TableSchema::INDEX_FULLTEXT = 'mysql-fulltext'

only possible in mysql

INDEX_PRIMARY_KEY

public const TableSchema::INDEX_PRIMARY_KEY = 'primary'

INDEX_UNIQUE

public const TableSchema::INDEX_UNIQUE = 'unique'

Properties

$autoSync

protected bool TableSchema::$autoSync = false

$collation

protected ?string TableSchema::$collation

$columns

protected array<string, Column\AbstractColumn> TableSchema::$columns = array()

$databaseManager

$engine

protected ?string TableSchema::$engine

$indexes

protected array TableSchema::$indexes = array()

$lastSync

protected ?array TableSchema::$lastSync

If null the table doesn't exist yet

$renamedColumns

protected array TableSchema::$renamedColumns = array()

$tableName

protected string TableSchema::$tableName

Methods

__construct()

public TableSchema::__construct (
string $tableName,
Column\AbstractColumn[] $columns,
?string $collation = null,
?string $engine = null
)
Parameters
$tableName : string
 
$columns : Column\AbstractColumn[]
 
$collation : ?string
 
$engine : ?string
 
Exceptions

Throws \InvalidArgumentException

__clone()

public TableSchema::__clone (
void
): void

addColumns()

public TableSchema::addColumns (
Column\AbstractColumn|Column\AbstractColumn[] $addColumns,
?string $before = null
): void

Adds a one or more columns to the table

Parameters
$addColumns : Column\AbstractColumn|Column\AbstractColumn[]
 
$before : ?string

Add the columns before another column. If null the columns will be added to the end.

Exceptions

Throws \InvalidArgumentException On duplicate columnName

addIndex()

public TableSchema::addIndex (
string|string[] $columnNames,
?string $indexName = null,
string $type = TableSchema::INDEX,
bool $merge = true
): void

Adds an index to the table

Parameters
$columnNames : string|string[]
 
$indexName : ?string
 
$type : string
 
$merge : bool

Merge with existing indexes with $indexName; If false existing indexes will be replaced

addPrimaryKey()

public TableSchema::addPrimaryKey (
string|string[] $columnNames,
bool $merge = true
): void

Adds a primary key

Helper method that call addIndex

Parameters
$columnNames : string|string[]
 
$merge : bool

If false existing indexes will be replaced

addUnique()

public TableSchema::addUnique (
string|string[] $columnNames,
?string $indexName = null,
bool $merge = true
): void

Adds a unique key

Helper method that call addIndex

Parameters
$columnNames : string|string[]
 
$indexName : ?string
 
$merge : bool

If false existing indexes will be replaced

alter()

protected TableSchema::alter (
bool $simulationMode
): void

Gets all changes and updates the table

Parameters
$simulationMode : bool
 

count()

public TableSchema::count (
void
): int

create()

protected TableSchema::create (
bool $simulationMode,
bool $ifNotExist = false
): void

Creates the table

Parameters
$simulationMode : bool
 
$ifNotExist : bool
 

getAlterChanges()

private TableSchema::getAlterChanges (
void
): array

getAutoSync()

public TableSchema::getAutoSync (
void
): bool

Returns the actual autoSync state

getCollation()

public TableSchema::getCollation (
void
): ?string

getColumn()

public TableSchema::getColumn (
string $columnName
): Column\AbstractColumn

Returns the column object

Parameters
$columnName : string
 

getColumnAfter()

public TableSchema::getColumnAfter (
string $columnName
): ?string
Parameters
$columnName : string
 

getColumnNames()

public TableSchema::getColumnNames (
void
): array

Return all column names of the table.

getColumns()

public TableSchema::getColumns (
void
): array

Returns all column objects of the table in an array with "key = string $columnName" and "value = AbstractColumn $column"

getEngine()

public TableSchema::getEngine (
void
): ?string

getIndexes()

public TableSchema::getIndexes (
void
): array

Returns the indexes

getIndexesOfColumn()

public TableSchema::getIndexesOfColumn (
string $columnName
): array

Returns all indexes of a single column

Parameters
$columnName : string
 
Return Values

The format is equal to Column::getIndexes before inserting into a TableSchema.

getName()

public TableSchema::getName (
void
): string

Returns the table name

getSyncSql()

public TableSchema::getSyncSql (
void
): string

Returns the sql query which would be performed on sync

Exceptions

Throws \BadMethodCallException if autoSync is enabled.

getTableExist()

public TableSchema::getTableExist (
void
): bool

Checks whether the tableSchema is already written to database.

hasColumn()

public TableSchema::hasColumn (
string $columnName
): bool

Checks whether a column exist in this table schema

Parameters
$columnName : string
 

hasIndex()

public TableSchema::hasIndex (
string $indexName
): bool

Checks whether the table has an index with the name $indexName

Parameters
$indexName : string
 

manuallySync()

public TableSchema::manuallySync (
bool $simulationMode = false
): void

Manually sync the tableSchema with the database. Only useable if autoSync is disabled!

Parameters
$simulationMode : bool

If this is true no query will be triggered

Exceptions

Throws \BadMethodCallException if autoSync is enabled.

process()

public TableSchema::process (
callable $callback,
bool $simulationMode = false
): void

Helper method to edit multiple columns

The autoSync will be disabled before the callback and will be activated after the callback.

Parameters
$callback : callable
 
$simulationMode : bool

If this is true no query will be triggered

removeAllIndexes()

public TableSchema::removeAllIndexes (
null|string|string[] $columnNames = null
): void

Deletes all indexes.

Parameters
$columnNames : null|string|string[]

Only delete indexes from certain columns. Null to delete all indexes.

removeColumns()

public TableSchema::removeColumns (
string|string[] $columnNames
): void

Removes one or more columns from the table

Parameters
$columnNames : string|string[]
 

removeIndex()

public TableSchema::removeIndex (
string $indexName,
null|string|string[] $columnNames = null
): void

Removes an index

If $columnNames is set only specific columns will be removed from the index

Parameters
$indexName : string
 
$columnNames : null|string|string[]
 

rename()

public TableSchema::rename (
string $newTableName
): void

Renames the table

Parameters
$newTableName : string
 

renameColumn()

public TableSchema::renameColumn (
string $oldColumnName,
string $newColumnName
): void

Renames a column

Call AbstractColumn::rename if possible.

Parameters
$oldColumnName : string
 
$newColumnName : string
 
Exceptions

Throws \com\setasign\SetaFramework\Exception If oldColumnName is unknown or the newColumnName already exist

renameColumnInIndexes()

private TableSchema::renameColumnInIndexes (
string $oldColumnName,
string $newColumnName
): void
Parameters
$oldColumnName : string
 
$newColumnName : string
 

reorderColumns()

public TableSchema::reorderColumns (
string[] $columnOrder
): void
Parameters
$columnOrder : string[]
 

resetSync()

public TableSchema::resetSync (
void
): void

This method is only supposed for setup of tableSchema in AbstractManager::getTableSchema.

setAutoSync()

public TableSchema::setAutoSync (
bool $autoSync = true,
bool $simulationMode = false
): void

Enable/Disable autoSync

If autoSync was disable and it will be enabled, the sync method will be called.

Parameters
$autoSync : bool
 
$simulationMode : bool

If this is true no query will be triggered

setCollation()

public TableSchema::setCollation (
string $collation
): void
Parameters
$collation : string
 

setDatabaseManager()

public TableSchema::setDatabaseManager (
AbstractManager $abstractManager,
bool $autoSync = true,
bool $tableExist = false
): TableSchema
Parameters
$abstractManager : AbstractManager
 
$autoSync : bool
 
$tableExist : bool

If this is true the actual data will be handled as the actual state of the database.

setEngine()

public TableSchema::setEngine (
string $engine
): void
Parameters
$engine : string