構造のリフレクション
**********
.{data-version:3.2.1}
Nette Database は [api:Nette\Database\Reflection]クラスで、データベースの構造を調べる道具を提供します。テーブル、列、インデックス、外部キーの情報を取り出せます。リフレクションはスキーマの生成、データベースを扱う柔軟なアプリケーション、汎用のデータベース向けの道具などに使えます。
リフレクションのオブジェクトはデータベース接続のインスタンスから取り出します。
```php
$reflection = $database->getReflection();
```
テーブルの取得
-------
読み取り専用のプロパティ `$reflection->tables` には、データベースのすべてのテーブルの連想配列が入っています。
```php
// すべてのテーブルの名前を並べます
foreach ($reflection->tables as $name => $table) {
echo $name . "\n";
}
```
さらに 2 つのメソッドが使えます。
```php
// テーブルが存在するかを調べます
if ($reflection->hasTable('users')) {
echo "Table users exists";
}
// テーブルのオブジェクトを返します。存在しなければ例外を投げます
$table = $reflection->getTable('users');
```
テーブルの情報
-------
テーブルは [Table|api:Nette\Database\Reflection\Table]オブジェクトが表し、次の読み取り専用のプロパティを持ちます。
- `$name: string` - テーブルの名前
- `$view: bool` - ビューかどうか
- `$fullName: ?string` - スキーマを含むテーブルの完全な名前(あれば)
- `$columns: array<string, Column>` - テーブルの列の連想配列
- `$indexes: Index[]` - テーブルのインデックスの配列
- `$primaryKey: ?Index` - テーブルの主キー、または null
- `$foreignKeys: ForeignKey[]` - テーブルの外部キーの配列
- `$comment: ?string` - テーブルのコメント
列
---
テーブルの `columns` プロパティは列の連想配列を返します。キーは列の名前で、値は次のプロパティを持つ [Column|api:Nette\Database\Reflection\Column]のインスタンスです。
- `$name: string` - 列の名前
- `$table: ?Table` - その列のテーブルへの参照
- `$nativeType: string` - データベース本来の型
- `$size: ?int` - 型の大きさ/長さ
- `$nullable: bool` - 列が NULL を持てるかどうか
- `$default: mixed` - 列の既定値
- `$autoIncrement: bool` - 列が自動採番かどうか
- `$primary: bool` - 主キーの一部かどうか
- `$vendor: array` - そのデータベースシステムに固有の追加のメタデータ
- `$comment: ?string` - 列のコメント
```php
foreach ($table->columns as $name => $column) {
echo "Column: $name\n";
echo "Type: {$column->nativeType}\n";
echo "Nullable: " . ($column->nullable ? 'Yes' : 'No') . "\n";
}
```
インデックス
------
テーブルの `indexes` プロパティはインデックスの配列を返します。それぞれのインデックスは次のプロパティを持つ [Index|api:Nette\Database\Reflection\Index]のインスタンスです。
- `$columns: Column[]` - インデックスを構成する列の配列
- `$unique: bool` - インデックスが一意かどうか
- `$primary: bool` - 主キーかどうか
- `$name: ?string` - インデックスの名前
テーブルの主キーは `primaryKey` プロパティで取り出せます。これは `Index` オブジェクトか、テーブルに主キーがなければ `null` を返します。
```php
// インデックスを並べます
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";
}
// 主キーを並べます
if ($primaryKey = $table->primaryKey) {
$columns = implode(', ', array_map(fn($col) => $col->name, $primaryKey->columns));
echo "Primary Key: $columns\n";
}
```
外部キー
----
テーブルの `foreignKeys` プロパティは外部キーの配列を返します。それぞれの外部キーは次のプロパティを持つ [ForeignKey|api:Nette\Database\Reflection\ForeignKey]のインスタンスです。
- `$foreignTable: Table` - 参照先のテーブル
- `$localColumns: Column[]` - 手元の列の配列
- `$foreignColumns: Column[]` - 参照先の列の配列
- `$name: string` - 外部キーの名前
```php
// 外部キーを並べます
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";
}
```
構造のリフレクション
Nette Database は Nette\Database\Reflectionクラスで、データベースの構造を調べる道具を提供します。テーブル、列、インデックス、外部キーの情報を取り出せます。リフレクションはスキーマの生成、データベースを扱う柔軟なアプリケーション、汎用のデータベース向けの道具などに使えます。
リフレクションのオブジェクトはデータベース接続のインスタンスから取り出します。
$reflection = $database->getReflection();
テーブルの取得
読み取り専用のプロパティ $reflection->tables
には、データベースのすべてのテーブルの連想配列が入っています。
// すべてのテーブルの名前を並べます
foreach ($reflection->tables as $name => $table) {
echo $name . "\n";
}
さらに 2 つのメソッドが使えます。
// テーブルが存在するかを調べます
if ($reflection->hasTable('users')) {
echo "Table users exists";
}
// テーブルのオブジェクトを返します。存在しなければ例外を投げます
$table = $reflection->getTable('users');
テーブルの情報
テーブルは Tableオブジェクトが表し、次の読み取り専用のプロパティを持ちます。
$name: string – テーブルの名前
$view: bool – ビューかどうか
$fullName: ?string – スキーマを含むテーブルの完全な名前(あれば)
$columns: array<string, Column> – テーブルの列の連想配列
$indexes: Index[] – テーブルのインデックスの配列
$primaryKey: ?Index – テーブルの主キー、または null
$foreignKeys: ForeignKey[] – テーブルの外部キーの配列
$comment: ?string – テーブルのコメント
列
テーブルの columns
プロパティは列の連想配列を返します。キーは列の名前で、値は次のプロパティを持つ Columnのインスタンスです。
$name: string – 列の名前
$table: ?Table – その列のテーブルへの参照
$nativeType: string – データベース本来の型
$size: ?int – 型の大きさ/長さ
$nullable: bool – 列が NULL を持てるかどうか
$default: mixed – 列の既定値
$autoIncrement: bool – 列が自動採番かどうか
$primary: bool – 主キーの一部かどうか
$vendor: array – そのデータベースシステムに固有の追加のメタデータ
$comment: ?string – 列のコメント
foreach ($table->columns as $name => $column) {
echo "Column: $name\n";
echo "Type: {$column->nativeType}\n";
echo "Nullable: " . ($column->nullable ? 'Yes' : 'No') . "\n";
}
インデックス
テーブルの indexes
プロパティはインデックスの配列を返します。それぞれのインデックスは次のプロパティを持つ
Indexのインスタンスです。
$columns: Column[] – インデックスを構成する列の配列
$unique: bool – インデックスが一意かどうか
$primary: bool – 主キーかどうか
$name: ?string – インデックスの名前
テーブルの主キーは primaryKey プロパティで取り出せます。これは Index
オブジェクトか、テーブルに主キーがなければ null を返します。
// インデックスを並べます
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";
}
// 主キーを並べます
if ($primaryKey = $table->primaryKey) {
$columns = implode(', ', array_map(fn($col) => $col->name, $primaryKey->columns));
echo "Primary Key: $columns\n";
}
外部キー
テーブルの foreignKeys
プロパティは外部キーの配列を返します。それぞれの外部キーは次のプロパティを持つ ForeignKeyのインスタンスです。
$foreignTable: Table – 参照先のテーブル
$localColumns: Column[] – 手元の列の配列
$foreignColumns: Column[] – 参照先の列の配列
$name: string – 外部キーの名前
// 外部キーを並べます
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";
}