Reflection of Structure

Nette Database provides tools for inspecting database structures through the Nette\Database\Reflection class. This allows you to retrieve information about tables, columns, indexes, and foreign keys. You can use reflection to generate schemas, create flexible database applications, or build general database tools.

You can get the reflection object from a database connection instance:

$reflection = $database->getReflection();

Retrieving Tables

The readonly property $reflection->tables provides an associative array of all tables in the database:

// Listing the names of all tables
foreach ($reflection->tables as $name => $table) {
	echo $name . "\n";
}

There are two additional methods available:

// Check if a table exists
if ($reflection->hasTable('users')) {
	echo "The table 'users' exists.";
}

// Retrieve a table object; throws an exception if it doesn't exist
$table = $reflection->getTable('users');

Table Information

A table is represented by the Table object, which provides the following readonly properties:

  • $name: string – the name of the table
  • $view: bool – whether it is a view
  • $fullName: ?string – the full name of the table, including schema (if applicable)
  • $columns: array<string, Column> – an associative array of the table's columns
  • $indexes: Index[] – an array of the table's indexes
  • $primaryKey: ?Index – the primary key of the table or null
  • $foreignKeys: ForeignKey[] – an array of the table's foreign keys

Columns

The columns property of a table provides an associative array of columns, where the key is the column name and the value is an instance of Column with the following properties:

  • $name: string – the name of the column
  • $table: ?Table – a reference to the column's table
  • $nativeType: string – the native database type
  • $size: ?int – the size/length of the type
  • $nullable: bool – whether the column can contain NULL
  • $default: mixed – the default value of the column
  • $autoIncrement: bool – whether the column is auto-incremented
  • $primary: bool – whether the column is part of the primary key
  • $vendor: array – additional metadata specific to the database system
foreach ($table->columns as $name => $column) {
	echo "Column: $name\n";
	echo "Type: {$column->nativeType}\n";
	echo "Nullable: " . ($column->nullable ? 'Yes' : 'No') . "\n";
}

Indexes

The indexes property of a table contains an array of indexes, where each index is an instance of Index with the following properties:

  • $columns: Column[] – an array of columns that make up the index
  • $unique: bool – whether the index is unique
  • $primary: bool – whether it is a primary key
  • $name: ?string – the name of the index

The primary key of a table can be accessed through the primaryKey property, which returns an Index object or null if the table does not have one.

// Listing indexes
foreach ($table->indexes as $index) {
	$columns = implode(', ', array_map(fn($col) => $col->name, $index->columns));
	echo "Index" . ($index->name ? " {$index->name}" : '') . ":\n";
	echo "  Columns: $columns\n";
	echo "  Unique: " . ($index->unique ? 'Yes' : 'No') . "\n";
}

// Displaying the primary key
if ($primaryKey = $table->primaryKey) {
	$columns = implode(', ', array_map(fn($col) => $col->name, $primaryKey->columns));
	echo "Primary Key: $columns\n";
}

Foreign Keys

The foreignKeys property of a table contains an array of foreign keys, each represented as an instance of ForeignKey with the following properties:

  • $foreignTable: Table – the referenced table
  • $localColumns: Column[] – an array of local columns
  • $foreignColumns: Column[] – an array of referenced columns
  • $name: ?string – the name of the foreign key
// Listing foreign keys
foreach ($table->foreignKeys as $fk) {
	$localCols = implode(', ', array_map(fn($col) => $col->name, $fk->localColumns));
	$foreignCols = implode(', ', array_map(fn($col) => $col->name, $fk->foreignColumns));

	echo "FK" . ($fk->name ? " {$fk->name}" : '') . ":\n";
	echo "  $localCols -> {$fk->foreignTable->name}($foreignCols)\n";
}
version: 4.0 3.x