アプリケーションのデータベース スキーマを更新し、バージョン間でデータを移行するための使いやすい PHP パッケージ。
composer req aimeos/upscheme
目次
移行はデータベースのバージョン管理のようなものです。これにより、どのインストールでもまったく同じ状態を得ることができます。 Upscheme を使用すると、次のことが得られます。
以下は、テーブル レイアウトを変更する必要がある場合にいつでも適用できるテーブル定義の例です。次に、Upscheme は既存の列とテーブル プロパティを自動的に追加および変更します (ただし、安全上の理由から何も削除しないでください)。
$ this -> db ()-> table ( ' test ' , function ( $ t ) {
$ t -> engine = ' InnoDB ' ;
$ t -> id ();
$ t -> string ( ' domain ' , 32 );
$ t -> string ( ' code ' , 64 )-> opt ( ' charset ' , ' binary ' , [ ' mariadb ' , ' mysql ' ] );
$ t -> string ( ' label ' , 255 );
$ t -> int ( ' pos ' )-> default ( 0 );
$ t -> smallint ( ' status ' );
$ t -> default ();
$ t -> unique ( [ ' domain ' , ' code ' ] );
$ t -> index ( [ ' status ' , ' pos ' ] );
} );
リレーショナル データベース スキーマをアップグレードする場合、現在、Doctrine DBAL と Doctrine の移行という 2 つのパッケージが最も頻繁に使用されています。 Doctrine DBAL は、いくつかのデータベース実装の違いをうまく抽象化しますが、その API では多くのコードを記述する必要があります。他のサイトでの Doctrine の移行には、サードパーティの拡張機能をサポートするすべてのアプリケーションでの使用を困難にするいくつかの欠点があります。
DBAL の API は非常に冗長なので、単純なものであっても大量のコードを記述する必要があります。 Upscheme は Doctrine DBAL を使用して、最小限のコードでアプリケーションのデータベース スキーマをアップグレードするための使いやすい API を提供します。上記の Upscheme の例では、次のコード行は移行における DBAL に相当します。
$ dbalManager = $ conn -> createSchemaManager ();
$ from = $ manager -> createSchema ();
$ to = $ manager -> createSchema ();
if ( $ to -> hasTable ( ' test ' ) ) {
$ table = $ to -> getTable ( ' test ' );
} else {
$ table = $ to -> createTable ( ' test ' );
}
$ table -> addOption ( ' engine ' , ' InnoDB ' );
$ table -> addColumn ( ' id ' , ' integer ' , [ ' autoincrement ' => true ] );
$ table -> addColumn ( ' domain ' , ' string ' , [ ' length ' => 32 ] );
$ platform = $ conn -> getDatabasePlatform ();
if ( $ platform instanceof Doctrine DBAL Platform MySQLPlatform
|| $ platform instanceof Doctrine DBAL Platform MariaDBPlatform
) {
$ table -> addColumn ( ' code ' , ' string ' , [ ' length ' => 64 , ' customSchemaOptions ' => [ ' charset ' => ' binary ' ]] );
} else {
$ table -> addColumn ( ' code ' , ' string ' , [ ' length ' => 64 ]] );
}
$ table -> addColumn ( ' label ' , ' string ' , [ ' length ' => 255 ] );
$ table -> addColumn ( ' pos ' , ' integer ' , [ ' default ' => 0 ] );
$ table -> addColumn ( ' status ' , ' smallint ' , [] );
$ table -> addColumn ( ' mtime ' , ' datetime ' , [] );
$ table -> addColumn ( ' ctime ' , ' datetime ' , [] );
$ table -> addColumn ( ' editor ' , ' string ' , [ ' length ' => 255 ] );
$ table -> setPrimaryKey ( [ ' id ' ] );
$ table -> addUniqueIndex ( [ ' domain ' , ' code ' ] );
$ table -> addIndex ( [ ' status ' , ' pos ' ] );
foreach ( $ from -> getMigrateToSql ( $ to , $ conn -> getDatabasePlatform () ) as $ sql ) {
$ conn -> executeStatement ( $ sql );
}
Doctrine Migration は、特定の順序を保証するために、作成時に名前が付けられた移行クラスに依存します。さらに、どの移行が実行されたかをデータベースのテーブルに保存します。そこから生じる 3 つの大きな問題があります。
down()
使用時のデータ損失アプリケーションがサードパーティの拡張機能をサポートしている場合、これらの拡張機能は既存のテーブルに列を追加し、データ自体を移行する可能性があります。移行間の依存関係を定義する方法がないため、複数のサードパーティ拡張機能を備えたアプリケーションで競合なしに移行を実行することはほぼ不可能になる可能性があります。これを回避するために、Upscheme は各移行タスクで、タスクが他のタスクへの依存関係を定義できる、使いやすいbefore()
メソッドとafter()
メソッドを提供します。
Doctrine Migrations はデータベース テーブルを使用してどの移行が既に実行されたかを記録するため、問題が発生した場合にこれらの記録が簡単に同期しなくなる可能性があります。対照的に、Upscheme は実際のスキーマのみに依存するため、以前に何が起こったかに関係なく、どの状態からでもアップグレードできます。
Doctrine Migrations は、 down()
メソッドでの逆の操作もサポートしているため、Upscheme ではサポートされていない移行をロールバックできます。経験上、たとえば、新しい列を追加し、既存の列のデータを移行し、その後古い列を削除した後など、移行をロールバックすることは不可能な場合が多いことがわかっています。データの移行に損失があった場合、 down()
メソッドで同じ状態を再作成することはできません。テーブルを削除した場合も同様です。したがって、Upscheme はスキームのアップグレードのみを提供し、暗黙的なデータ損失を避けるためのダウングレードは提供しません。
Upscheme は、さまざまなデータベース サーバー実装から抽象化するために Doctrine DBAL を使用します。 DBAL は、すべての主要なリレーショナル データベース管理システム (RDBMS) をサポートしますが、利用可能な機能のサポート レベルは異なります。
優れたサポート:
限定的なサポート:
Composer を使用してaimeos/upscheme
パッケージをインストールした後、 Up
クラスを使用して移行タスクを実行できます。
$ config = [
' driver ' => ' pdo_mysql ' ,
' host ' => ' 127.0.0.1 ' ,
' dbname ' => ' <database> ' ,
' user ' => ' <dbuser> ' ,
' password ' => ' <secret> '
];
Aimeos Upscheme Up:: use ( $ config , ' src/migrations ' )-> up ();
Up::use()
メソッドには、データベース構成と移行タスクへのパスという 2 つのパラメータが必要です。設定では、ドライバーの配列キーと値が Doctrine DBAL でサポートされている必要があります。利用可能なドライバーは次のとおりです。
一部のデータベースでは異なるパラメータが必要ですが、最も注目すべきは SQLite と Oracle です。
SQLite:
$ config = [
' driver ' => ' pdo_sqlite ' ,
' path ' => ' path/to/file.sq3 '
];
オラクル:
$ config = [
' driver ' => ' pdo_oci ' ,
' host ' => ' <host or IP> ' ,
' dbname ' => ' <SID or service name (Oracle 18+)> ' ,
' service ' => true , // for Oracle 18 + only
' user ' => ' <dbuser> ' ,
' password ' => ' <secret> '
];
以前に Doctrine DBAL を使用したことがない場合は、データベース構成が異なる構造になっているか、データベース タイプに異なる値が使用されている可能性があります。 Upscheme を使用すると、構成を有効な DBAL 設定に変換するカスタム メソッドを登録できます。例:
Aimeos Upscheme Up:: macro ( ' connect ' , function ( array $ cfg ) {
return Doctrine DBAL DriverManager:: getConnection ( [
' driver ' => $ cfg [ ' adapter ' ],
' host ' => $ cfg [ ' host ' ],
' dbname ' => $ cfg [ ' database ' ],
' user ' => $ cfg [ ' username ' ],
' password ' => $ cfg [ ' password ' ]
] );
} );
Upscheme は、キー名で区別できるいくつかのデータベース接続もサポートしています。
$ config = [
' db ' => [
' driver ' => ' pdo_mysql ' ,
' host ' => ' 127.0.0.1 ' ,
' dbname ' => ' <database> ' ,
' user ' => ' <dbuser> ' ,
' password ' => ' <secret> '
],
' temp ' => [
' driver ' => ' pdo_sqlite ' ,
' path ' => ' /tmp/mydb.sqlite '
]
];
Aimeos Upscheme Up:: use ( $ config , ' src/migrations ' )-> up ();
もちろん、いくつかの移行パスをUp
クラスに渡すこともできます。
Aimeos Upscheme Up:: use ( $ config , [ ' src/migrations ' , ' ext/migrations ' ] )-> up ();
出力を有効化 (デバッグ) するには、verbose() メソッドを使用します。
Aimeos Upscheme Up:: use ( $ config , ' src/migrations ' )-> verbose ()-> up (); // most important only
Aimeos Upscheme Up:: use ( $ config , ' src/migrations ' )-> verbose ( ' vv ' )-> up (); // more verbose
Aimeos Upscheme Up:: use ( $ config , ' src/migrations ' )-> verbose ( ' vvv ' )-> up (); // debugging
移行タスクではup()
メソッドの実装のみが必要で、 Up
クラスに渡されるディレクトリの 1 つに保存する必要があります。
<?php
namespace Aimeos Upscheme Task ;
use Aimeos Upscheme Schema Table ;
return new class ( $ this ) extends Base {
public function up ()
{
$ this -> db ()-> table ( ' test ' , function ( Table $ t ) {
$ t -> id ();
$ t -> string ( ' label ' );
$ t -> bool ( ' status ' );
} );
}
};
PHP ファイルには、常にnamespace
ステートメントを最初に含めます。 use
ステートメントはオプションであり、クロージャ関数の引数の型ヒントのショートカットとしてのみ必要です。また、クラスは「Base」タスク クラスから拡張するか、「Iface」タスク インターフェイスを実装する必要があります。
匿名クラスの代わりに、名前付きクラスを移行タスクに使用できます。
<?php
namespace Aimeos Upscheme Task ;
use Aimeos Upscheme Schema Table ;
class TestTable extends Base
{
public function up ()
{
$ this -> db ()-> table ( ' test ' , function ( Table $ t ) {
$ t -> id ();
$ t -> string ( ' label ' );
$ t -> bool ( ' status ' );
} );
}
}
クラスが保存されているファイルは、クラス自体と同じ名前 (大文字と小文字を区別) と.php
接尾辞を持つ必要があります。例:
class TestTable -> TestTable.php
移行タスク ファイルに名前を付ける方法に厳密な規則はありません。何を行うか (例: 「CreateTestTable.php」)、何を操作するか (例: 「TestTable.php」) で名前を付けるか、タイムスタンプ (例: 「20201231_Test.php」) を使用することもできます。
タスクに依存関係が含まれていない場合、タスクはファイル名に従ってアルファベット順に並べ替えられて実行されます。並べ替えは次のようになります。
20201231_Test.php
CreateTestTable.php
TestTable.php
他の移行タスクへの依存関係を指定するには、 after()
メソッドとbefore()
メソッドを使用します。タスクは、 after()
によって返されたタスクの後、 before()
によって返されたタスクの前に実行されます。
return new class ( $ this ) extends Base {
public function after () : array
{
return [ ' CreateRefTable ' ];
}
public function before () : array
{
return [ ' InsertTestData ' ];
}
}
タスク名は、 .php
接尾辞を除いたタスクのファイル名です。移行例がTestTable.php
ファイルに保存されている場合、実行順序は次のようになります。
CreateRefTable.php -> TestTable.php -> InsertTestData.php
移行タスクでメッセージを出力するには、 info()
メソッドを使用します。
$ this -> info ( ' some message ' );
$ this -> info ( ' more verbose message ' , ' vv ' );
$ this -> info ( ' very verbose debug message ' , ' vvv ' );
2 番目のパラメータは冗長レベルで、none またはv
は標準メッセージ、 vv
より冗長性が必要な場合にのみ表示されるメッセージ、 vvv
デバッグ メッセージ用です。メッセージをインデントするための 3 番目のパラメーターもあります。
$ this -> info ( ' some message ' );
$ this -> info ( ' second level message ' , ' v ' , 1 );
$ this -> info ( ' third level message ' , ' v ' , 2 );
これにより、次のように表示されます。
some message
second level message
third level message
前提条件は、 Up
クラスのverbose()
メソッドが以前に呼び出されていることです。
Aimeos Upscheme Up:: use ( $ config , ' ... ' )-> verbose ()-> up ();
up()
メソッドでは、 db()
メソッドを使用してデータベース スキーマにアクセスできます。複数のデータベース構成をUp::use()
に渡している場合は、構成キーによってさまざまなスキーマにアクセスできます。
// $config = [ 'db' => [ ... ] , 'temp' => [ ... ] ] ;
// Aimeos UpschemeUp::use( $config , '...' )->up() ;
$ this -> db ();
$ this -> db ( ' db ' );
$ this -> db ( ' temp ' );
構成キーを渡さないか、存在しない場合は、最初の構成 (この場合は「db」) が返されます。データベース スキーマ オブジェクトの利用可能なメソッドを使用すると、テーブル、列、インデックス、およびその他のデータベース オブジェクトを追加、更新、または削除できます。また、 insert()
、 select()
、 update()
、 delete()
およびstmt()
を使用してテーブルのレコードを操作することもできます。
各移行タスクの後、タスクで行われたスキーマの更新はデータベースに自動的に適用されます。データを挿入するために変更をすぐに永続化する必要がある場合は、 $this->db()->up()
を自分で呼び出してください。 up()
メソッドはテーブル、シーケンス、列オブジェクトでも使用できるため、どこでもup()
を呼び出すことができます。
SELECT ステートメントと INSERT/UPDATE/DELETE ステートメントを同時に実行するために 2 つの異なるデータベース接続が必要な場合は、2 番目のパラメーターとして TRUE をdb()
に渡して、新しい接続を含むデータベース スキーマを取得します。
$ db1 = $ this -> db ();
$ db2 = $ this -> db ( ' db ' , true );
foreach ( $ db1 -> select ( ' users ' , [ ' status ' => false ] ) as $ row ) {
$ db2 -> insert ( ' oldusers ' , $ row );
}
$ db2 -> delete ( ' users ' , [ ' status ' => false ] );
行われたすべてのスキーマ変更は、新しい接続を持つスキーマが返される前にデータベースに適用されます。データベース サーバーが新しい接続を拒否するまでデータベース接続が蓄積しないようにするには、 db( '<name>', true )
によって作成された新しい接続に対して常にclose()
を呼び出します。
$ db2 -> close ();
データベース オブジェクトの移行を手動で記述する代わりに、以下を使用して移行ファイルを自動的に生成できます。
$ config = [
' db ' => [
' driver ' => ' pdo_mysql ' ,
' host ' => ' 127.0.0.1 ' ,
' dbname ' => ' <database> ' ,
' user ' => ' <dbuser> ' ,
' password ' => ' <secret> '
]
];
Aimeos Upscheme Up:: use ( $ config , ' migrations ' )-> create ();
これにより、渡されたディレクトリ (この例では./migrations/
) にシーケンス、テーブル、ビューごとに 1 つのファイルが生成されます。複数のデータベースがあり、それらすべての移行を一度に作成したい場合は、構成から接続キーをcreate()
に渡します。
$ config = [
' db ' => [
' driver ' => ' pdo_mysql ' ,
// ...
],
' order ' => [
' driver ' => ' pdo_oci ' ,
// ...
]
];
Aimeos Upscheme Up:: use ( $ config , ' migrations ' )-> create ( [ ' db ' , ' order ' ] );
スキーマのセクションで説明したように、 $this->db()
を呼び出すことで、タスクでデータベース スキーマ オブジェクトを取得します。これにより、すべてのテーブル、シーケンス、その他のスキーマ オブジェクトを含むデータベース スキーマに完全にアクセスできるようになります。
$ table = $ this -> db ()-> table ( ' users ' );
$ seq = $ this -> db ()-> sequence ( ' seq_users ' );
テーブルまたはシーケンスが存在しない場合は作成されます。それ以外の場合は、既存のテーブルまたはシーケンス オブジェクトが返されます。どちらの場合も、後でオブジェクトを変更して、テーブルに新しい列などを追加できます。
$this->db()
によって返されるデータベース スキーマを使用して、テーブル、列、インデックス、外部キー、シーケンスをテストできます。
$ db = $ this -> db ();
if ( $ db -> hasTable ( ' users ' ) ) {
// The "users" table exists
}
if ( $ db -> hasColumn ( ' users ' , ' name ' ) ) {
// The "name" column in the "users" table exists
}
if ( $ db -> hasIndex ( ' users ' , ' idx_name ' ) ) {
// The "idx_name" index in the "users" table exists
}
if ( $ db -> hasForeign ( ' users_address ' , ' fk_users_id ' ) ) {
// The foreign key "fk_users_id" in the " users_address " table exists
}
if ( $ db -> hasSequence ( ' seq_users ' ) ) {
// The "seq_users" sequence exists
}
if ( $ db -> hasView ( ' testview ' ) ) {
// The "testview" view exists
}
$this->db()
によって返されるデータベース オブジェクトでは、 renameTable()
、 renameColumn()
、およびrenameIndex()
を使用してテーブル、列、インデックスの名前を変更できます。
$ db = $ this -> db ();
// Renames the table "users" to "accounts"
$ db -> renameTable ( ' users ' , ' account ' );
// Renames the column "label" to "name" in the "users" table
$ db -> renameColumn ( ' users ' , ' label ' , ' name ' );
// Renames the column "idx_label" to "idx_name" in the "users" table
$ db -> renameIndex ( ' users ' , ' idx_label ' , ' idx_name ' );
$this->db()
によって返されるデータベース オブジェクトには、テーブル、列、インデックス、外部キー、シーケンスを削除するメソッドもあります。
$ db = $ this -> db ();
// Drops the foreign key " fk_users_id " from the " users_address " table
$ db -> dropForeign ( ' users_address ' , ' fk_users_id ' );
// Drops the " idx_name " index from the " users " table
$ db -> dropIndex ( ' users ' , ' idx_name ' );
// Drops the " name " column from the " users " table
$ db -> dropColumn ( ' users ' , ' name ' );
// Drops the "seq_users" sequence
$ db -> dropSequence ( ' seq_users ' );
// Drops the "users" table
$ db -> dropTable ( ' users ' );
// Drops the "testview" view
$ db -> dropView ( ' testview ' );
テーブル、列、インデックス、外部キー、またはシーケンスが存在しない場合は、暗黙的に無視されます。存在するかどうかを確認する必要がある場合は、「存在の確認」セクションで説明したように、前にhasTable()
、 hasColumn()
、 hasIndex()
、 hasForeign()
、およびhasSeqence()
メソッドを使用します。
insert()
、 select()
、 update()
、およびdelete()
メソッドを使用すると、テーブル内の行を簡単に追加、取得、変更、削除できます。
$ this -> db ()-> transaction ( function ( $ db ) {
$ db2 = $ this -> db ( ' db ' , true );
foreach ( $ db2 -> select ( ' users ' , [ ' status ' => false ] ) as $ row )
{
$ db -> insert ( ' newusers ' , [ ' userid ' => $ row [ ' id ' ], ' status ' => true ] );
$ db -> update ( ' users ' , [ ' refid ' => $ db -> lastId ()], [ ' id ' => $ row [ ' id ' ]] );
}
$ db -> delete ( ' newusers ' , [ ' status ' => false ] );
$ db2 -> close ();
} );
select()
insert()
、 update()
、またはdelete()
と同時に使用する場合は、新しいコマンドをデータベース サーバーに送信する間にselect()
ステートメントが行を返すため、2 番目のデータベース接続を作成する必要があります。これは、同じ接続ではなく、別々の接続でのみ機能します。
すべての削除/挿入/更新操作をトランザクションにラップするには、データベース オブジェクトのtransaction()
メソッドを使用する必要があります。
$ this -> db ()-> transaction ( function ( $ db ) {
// $db- > insert ( ... )
// $db- > update ( ... )
// $db- > delete ( ... )
} );
これにより、すべての書き込み操作がアトミックに実行されるか、エラーが発生した場合にはまったく実行されないことが保証されます。 transaction()
メソッドは、匿名関数がメソッドに制御を返した後、トランザクションが自動的にコミットまたはロールバックされることを保証します。
匿名関数内で追加のパラメーターが必要な場合は、関数のuse
リストでパラメーターを渡すことができます。
$ userid = 123 ;
$ this -> db ()-> transaction ( function ( $ db ) use ( $ userid ) {
$ db -> insert ( ' newusers ' , [ ' userid ' => userid, ' status ' => true ] );
} );
AND で結合されるメソッドには、条件として単純なキーと値のペアのみを渡すことができます。より複雑なクエリが必要な場合は、代わりにstmt()
使用してください。
$ db = $ this -> db ();
$ result = $ db -> stmt ()-> select ( ' id ' , ' name ' )
-> from ( ' users ' )
-> where ( ' status != ? ' )
-> setParameter ( 0 , false )
-> executeQuery ();
$ db -> stmt ()-> delete ( ' users ' )
-> where ( ' status != ? ' )
-> setParameter ( 0 , false )
-> executeStatement ();
$ db -> stmt ()-> update ( ' users ' )
-> set ( ' status ' , ' ? ' )
-> where ( ' status != ? ' )
-> setParameters ( [ true , false ] )
-> executeStatement ();
stmt()
メソッドは、より高度なステートメントを構築できるDoctrineDBALQueryQueryBuilder
オブジェクトを返します。詳細については、Doctrine Query Builder のドキュメントを参照してください。
SQL ステートメントで値を直接使用したい場合 (セキュリティ上の理由から、可能な限りプリペアド ステートメントを使用してください!)、 q()
メソッドを使用して値を引用符で囲む必要があります。
$ db = $ this -> db ();
$ result = $ db -> stmt ()-> select ( ' * ' )-> from ( ' products ' )
-> where ( ' status = ' . $ db -> q ( $ _GET [ ' status ' ] ) )-> executeQuery ();
同様に、スキーマに予約済みのキーワード (列名など) が含まれている場合は、 qi()
メソッドを使用してそれらのキーワードも引用符で囲む必要があります。
$ db = $ this -> db ();
$ result = $ db -> stmt ()-> select ( $ db -> qi ( ' key ' ) )-> from ( ' products ' )-> executeQuery ();
Doctrine は SQL ステートメントの共通サブセットのみをサポートしており、データベース ベンダーが実装しているすべての可能性をサポートしているわけではありません。この制限を取り除くために、Upscheme は、Doctrine DBAL でサポートされていないカスタム SQL ステートメントを実行するためのexec()
、 for()
およびquery()
メソッドを提供します。
カスタム SQL クエリを実行するには、反復処理できる結果セットを返すquery()
メソッドを使用します。
$ sql = ' SELECT id, label, status FROM product WHERE label LIKE ? ' ;
$ result = $ this -> db ()-> query ( $ sql , [ ' test% ' ] );
foreach ( $ result -> iterateAssociative () as $ row ) {
// ...
}
他のすべての SQL ステートメントでは、影響を受ける行の数を返すexec()
メソッドを使用します。
$ sql = ' UPDATE product SET status=? WHERE status=? ' ;
$ num = $ this -> db ()-> exec ( $ sql , [ 1 , 0 ] );
for()
メソッドを使用すると、データベース プラットフォームに応じてステートメントを実行することもできます。
$ this -> db ()-> for ( ' mysql ' , ' CREATE FULLTEXT INDEX idx_text ON product (text) ' );
データベース プラットフォームの指定は、データベース実装間で構文が異なる特殊なタイプのインデックスを作成する場合に非常に役立ちます。
カスタムメソッドを呼び出すか、未知のメソッド呼び出しをDoctrineスキーマオブジェクトに渡します
public function __call( string $ method , array $ args )
$method
メソッドの名前$args
メソッドパラメータ例:
Upscheme DB オブジェクトのクラス プロパティにアクセスできるカスタム メソッドを登録できます。
Aimeos Upscheme Schema DB :: macro ( ' hasFkIndexes ' , function ( $ val ) {
return $ this -> to -> hasExplicitForeignKeyIndexes ();
} );
$ db -> hasFkIndexes ();
// returns true / false
利用可能なクラス プロパティは次のとおりです。
$this->from
: 現在のデータベースを表す元の Doctrine データベース スキーマ
$this->to
: これまでに行われた変更を含む Doctrine データベース スキーマ
$this->conn
: Doctrine データベース接続
$this->up
: Upscheme オブジェクト
さらに、任意の Doctrine スキーマ メソッドを直接呼び出すことができます。例:
$ db -> hasExplicitForeignKeyIndexes ();
データベース接続を閉じます
public function close() : void
$this->db( '...', true )
で作成された DB スキーマ オブジェクトに対してのみclose()
を呼び出します。そうしないと、メイン接続が閉じられ、DBAL がサーバーに再接続する必要があり、パフォーマンスが低下します。
例:
$ db = $ this -> db ( ' temp ' , true );
$ db -> dropTable ( ' test ' );
$ db -> close ();
指定されたテーブルからレコードを削除します
public function delete( string $ table , array $ conditions = [] ) : self
$table
テーブルの名前$conditions
比較する列名と値のキー/値ペア警告:条件値はエスケープされますが、テーブル名と条件列名はエスケープされません。テーブル名と条件列名には固定文字列のみを使用し、外部入力は使用しないでください。
例:
$ db -> delete ( ' test ' , [ ' status ' => false , ' type ' => ' old ' ] );
$ db -> delete ( ' test ' );
2 番目のパラメーターで渡されるいくつかの条件は、「AND」によって結合されます。より複雑なステートメントが必要な場合は、代わりにstmt()
メソッドを使用してください。
名前で指定された列が存在する場合は削除します
public function dropColumn( string $ table , $ name ) : self
$table
列が属するテーブルの名前$name
列の名前例:
$ db -> dropColumn ( ' test ' , ' oldcol ' );
$ db -> dropColumn ( ' test ' , [ ' oldcol ' , ' oldcol2 ' ] );
列または列の 1 つが存在しない場合、その列は黙って無視されます。
名前で指定された外部キー制約が存在する場合は削除します。
public function dropForeign( string $ table , $ name ) : self
$table
外部キー制約が属するテーブルの名前$name
外部キー制約の名前例:
$ db -> dropForeign ( ' test ' , ' fk_old ' );
$ db -> dropForeign ( ' test ' , [ ' fk_old ' , ' fk_old2 ' ] );
外部キー制約または制約の 1 つが存在しない場合、それは黙って無視されます。
名前で指定されたインデックスが存在する場合は削除します
public function dropIndex( string $ table , $ name ) : self
$table
インデックスが属するテーブルの名前$name
インデックスの名前例:
$ db -> dropIndex ( ' test ' , ' idx_old ' );
$ db -> dropIndex ( ' test ' , [ ' idx_old ' , ' idx_old2 ' ] );
インデックスまたはインデックスの 1 つが存在しない場合は、暗黙的に無視されます。
名前で指定されたシーケンスが存在する場合はそれを削除します
public function dropSequence( $ name ) : self
$name
シーケンスの名前例:
$ db -> dropSequence ( ' seq_old ' );
$ db -> dropSequence ( [ ' seq_old ' , ' seq_old2 ' ] );
シーケンスまたはシーケンスの 1 つが存在しない場合は、黙って無視されます。
名前で指定されたテーブルが存在する場合は削除します
public function dropTable( $ name ) : self
$name
テーブルの名前例:
$ db -> dropTable ( ' test ' );
$ db -> dropTable ( [ ' test ' , ' test2 ' ] );
テーブルまたはテーブルの 1 つが存在しない場合は、黙って無視されます。
名前で指定されたビューが存在する場合は削除します
public function dropView( $ name ) : self
$name
ビューの名前例:
$ db -> dropView ( ' test ' );
$ db -> dropView ( [ ' test ' , ' test2 ' ] );
ビューまたはビューの 1 つが存在しない場合、そのビューは黙って無視されます。
カスタム SQL ステートメントを実行します
public function exec( string $ sql , array $ params = [], array $ types = [] ) : int
$sql
カスタム SQL ステートメント$params
位置パラメータのリスト、またはプレースホルダとパラメータの連想リスト$types
位置または結合プレースホルダー パラメーターの DBAL データ型のリストデータベースの変更はすぐには適用されないため、カスタム ステートメントを実行する前に必ず up() を呼び出して、使用するテーブルが以前に作成されていることを確認してください。
例:
$ sql = ' UPDATE product SET status=? WHERE status=? ' ;
$ num = $ this -> db ()-> exec ( $ sql , [ 1 , 0 ] );
データベースが指定されたタイプの場合、カスタム SQL ステートメントを実行します。
public function for( $ type , $ sql ) : self
$type
ステートメントを実行するデータベースの種類$sql
カスタム SQL ステートメント利用可能なデータベース プラットフォームのタイプは次のとおりです。
データベースの変更はすぐには適用されないため、カスタム ステートメントを実行する前に必ずup()
を呼び出して、使用するテーブルが以前に作成されていることを確認してください。
例:
$ db -> for ( ' mysql ' , ' CREATE INDEX idx_test_label ON test (label(16)) ' );
$ db -> for ( [ ' mysql ' , ' sqlite ' ], [
' DROP INDEX unq_test_status ' ,
' UPDATE test SET status = 0 WHERE status IS NULL ' ,
] );
列が存在するかどうかを確認します
public function hasColumn( string $ table , $ name ) : bool
$table
列が属するテーブルの名前$name
列の名前例:
$ db -> hasColumn ( ' test ' , ' testcol ' );
$ db -> hasColumn ( ' test ' , [ ' testcol ' , ' testcol2 ' ] );
外部キー制約が存在するかどうかを確認します
public function hasForeign( string $ table , $ name ) : bool
$table
外部キー制約が属するテーブルの名前$name
外部キー制約の名前例:
$ db -> hasForeign ( ' test ' , ' fk_testcol ' );
$ db -> hasForeign ( ' test ' , [ ' fk_testcol ' , ' fk_testcol2 ' ] );
インデックスが存在するかどうかを確認します
public function hasIndex( string $ table , $ name ) : bool
$table
インデックスが属するテーブルの名前$name
インデックスの名前例:
$ db -> hasIndex ( ' test ' , ' idx_test_col ' );
$ db -> hasIndex ( ' test ' , [ ' idx_test_col ' , ' idx_test_col2 ' ] );
シーケンスが存在するかどうかを確認します
public function hasSequence( $ name ) : bool
$name
シーケンスの名前例:
$ db -> hasSequence ( ' seq_test ' );
$ db -> hasSequence ( [ ' seq_test ' , ' seq_test2 ' ] );
テーブルが存在するかどうかを確認します
public function hasTable( $ name ) : bool
$name
テーブルの名前例:
$ db -> hasTable ( ' test ' );
$ db -> hasTable ( [ ' test ' , ' test2 ' ] );
ビューが存在するかどうかを確認します
public function hasView( $ name ) : bool
$name
ビューの名前例:
$ db -> hasView ( ' test ' );
$ db -> hasView ( [ ' test ' , ' test2 ' ] );
指定されたテーブルにレコードを挿入します
public function insert( string $ table , array $ data ) : self
$table
テーブルの名前$data
挿入する列名/値のキー/値ペア例:
$ db -> insert ( ' test ' , [ ' label ' => ' myvalue ' , ' status ' => true ] );
データベーステーブルに最後に挿入された行のIDを返します。
public function lastId() : string
注意: Doctrine DBAL は現時点で Oracle IDENTITY 列をサポートしていないため、これは Oracle プラットフォームでは機能しません。
例:
$ db -> lastId ();
データベースの名前を返します
public function name() : string
例:
$ db -> name ();
値を引用符で囲みます
public function q( $ value , $ type = Doctrine DBAL ParameterType:: STRING ) : string
$value
準備されていない SQL クエリで使用する値$type
パラメータタイプ例:
$ result = $ db -> stmt ()-> select ( ' * ' )-> from ( ' products ' )
-> where ( ' status = ' . $ db -> q ( $ _GET [ ' status ' ] ) )-> executeQuery ();
データベース識別子を引用符で囲みます
public function qi( string $ identifier ) : string
$identifier
テーブル名や列名などの識別子例:
$ result = $ db -> stmt ()-> select ( $ db -> qi ( ' key ' ) )-> from ( ' products ' )-> executeQuery ();
カスタム SQL クエリを実行します
public function query( string $ sql , array $ params = [], array $ types = [] ) : Doctrine DBAL Result
$sql
カスタム SQL ステートメント$params
位置パラメータのリスト、またはプレースホルダとパラメータの連想リスト$types
位置または結合プレースホルダー パラメーターの DBAL データ型のリスト例:
$ result = $ db -> query ( ' SELECT id, label, status FROM product WHERE label LIKE ? ' , [ ' test% ' ] );
foreach ( $ result -> iterateAssociative () as $ row ) {
// ...
}
ヒント:詳細については、データを取得するための DBAL メソッドを確認してください。
列または列のリストの名前を変更します。
public function renameColumn( string $ table , $ from , string $ to = null ) : self
$table
テーブルの名前$from
列名、または古い/新しい列名の配列$to
最初のパラメータが配列の場合、新しい列名は無視されます列がまだ存在しない場合、メソッドは成功しますが、何も起こりません。 up()
を呼び出す必要はありません。
制限事項
例:
// single column
$ db -> renameColumn ( ' testtable ' , ' test_col ' , ' test_column ' );
// rename several columns at once
$ db -> renameColumn ( ' testtable ' , [ ' tcol ' => ' testcol ' , ' tcol2 ' => ' testcol2 ' ] );
列または列のリストの名前を変更します。
public function renameIndex( string $ table , $ from , string $ to = null ) : self
$table
テーブルの名前$from
インデックス名または新旧インデックス名の配列$to
最初のパラメータが配列の場合、新しいインデックス名は無視されますインデックスがまだ存在しない場合、メソッドは成功しますが、何も起こりません。 up()
を呼び出す必要はありません。
例:
// single index
$ db -> renameIndex ( ' testtable ' , ' idxcol ' , ' idx_column ' );
// rename several indexes at once
$ db -> renameIndex ( ' testtable ' , [ ' idxcol ' => ' idx_column ' , ' idxcol2 ' => ' idx_column2 ' ] );
テーブルまたはテーブルのリストの名前を変更します
public function renameTable( $ from , string $ to = null ) : self
$from
テーブル名または古い/新しいテーブル名の配列$to
最初のパラメータが配列の場合、新しいテーブル名は無視されますテーブルがまだ存在しない場合、メソッドは成功しますが、何も起こりません。 up()
を呼び出す必要はありません。
例:
// single table
$ db -> renameTable ( ' testtable ' , ' newtable ' );
// rename several tables at once
$ db -> renameTable ( [ ' testtable ' => ' newtable ' , ' oldtable ' => ' testtable2 ' ] );
現在のデータベースの実際の Doctrine スキーマを再ロードします
public function reset() : self
例:
$ db -> reset ();
指定されたテーブルからレコードを返します
public function select( string $ table , array $ conditions = null ) : array
$table
テーブルの名前$conditions
比較する列名と値のキー/値ペア例:
$ db -> select ( ' test ' , [ ' status ' => false , ' type ' => ' old ' ] );
$ db -> select ( ' test ' );
2 番目のパラメーターで渡されるいくつかの条件は、「AND」によって結合されます。より複雑なステートメントが必要な場合は、代わりにstmt()
メソッドを使用してください。
指定された名前のシーケンス オブジェクトを返します。
public function sequence( string $ name , Closure $ fcn = null ) : Sequence
$name
シーケンスの名前$fcn
($sequence) パラメータを持つ匿名関数でシーケンス定義を作成または更新しますシーケンスがまだ存在しない場合は作成されます。変更をデータベースに保持するには、 up()
を呼び出す必要があります。
例:
$ sequence = $ db -> sequence ( ' seq_test ' );
$ sequence = $ db -> sequence ( ' seq_test ' , function ( $ seq ) {
$ seq -> start ( 1000 )-> step ( 2 )-> cache ( 100 );
} )-> up ();
新しい SQL ステートメントのクエリ ビルダーを返します。
public function stmt() : Doctrine DBAL Query QueryBuilder
例:
$ db -> stmt ()-> delete ( $ db -> qi ( ' test ' ) )
-> where ( $ db -> qi ( ' stat ' ) . ' = ? ' )-> setParameter ( 0 , false )
-> executeStatement ();
$ db -> stmt ()-> update ( $ db -> qi ( ' test ' ) )
-> where ( $ db -> qi ( ' stat ' ) . '' , ' ? ' )-> setParameter ( 0 , true )
-> executeStatement ();
$ result = $ db -> stmt ()-> select ( $ db -> qi ( ' id ' ), $ db -> qi ( ' code ' ) )
-> from ( $ db -> qi ( ' test ' ) )
-> where ( $ db -> qi ( ' stat ' ) . ' = 1 ' )
-> executeQuery ();
while ( $ row = $ result -> fetchAssociative () ) {
$ id = $ row [ ' id ' ];
}
**注意: ** $db->qi()
メソッドを使用して、すべてのテーブル名と列名を自分で引用符で囲む必要があります。
利用可能な Doctrine QueryBuilder メソッドの詳細については、Doctrine のドキュメントを参照してください。
指定された名前のテーブル オブジェクトを返します。
public function table( string $ name , Closure $ fcn = null ) : Table
$name
テーブルの名前$fcn
($table) パラメータを持つ匿名関数でテーブル定義を作成または更新しますテーブルがまだ存在しない場合は作成されます。変更をデータベースに保持するには、 up()
を呼び出す必要があります。
例:
$ table = $ db -> table ( ' test ' );
$ table = $ db -> table ( ' test ' , function ( $ t ) {
$ t -> id ();
$ t -> string ( ' label ' );
$ t -> bool ( ' status ' );
} )-> up ();
トランザクション内で指定されたクロージャを実行します
public function transaction( Closure $ fcn ) : self
例:
$ this -> db ()-> transaction ( function ( $ db ) {
// $db- > insert ( ... )
// $db- > update ( ... )
// $db- > delete ( ... )
} );
データベースからオブジェクトを配列として返します
public function toArray() : array
例:
$ this -> db ()-> toArray ();
返される配列の構造は次のとおりです。
[
' sequence ' => [
' testseq ' => [
' name ' => ' testseq ' ,
' cache ' => null ,
' start ' => 1000 ,
' step ' => 1
]
],
' table ' => [
' testtable ' => [
' name ' => ' testtable ' ,
' opt ' => [
' engine ' => ' InnoDB ' ,
' collation ' => ' utf8mb4_unicode_ci ' ,
' charset ' => ' utf8mb4 ' ,
' autoincrement ' => 1 ,
' comment ' => ''
],
' col ' => [
' id ' => [
' name ' => ' id ' ,
' type ' => ' integer ' ,
' length ' => null ,
' precision ' => null ,
' scale ' => 0 ,
' null ' => false ,
' seq ' => 1
' default ' => null,
' fixed ' => false ,
' unsigned ' => false ,
' comment ' => '' ,
' opt ' => []
],
' parentid ' => [
' name ' => ' parentid ' ,
' type ' => ' bigint ' ,
' length ' => null ,
' precision ' => null ,
' scale ' => 0 ,
' null ' => false ,
' seq ' => false ,
' default ' => null ,
' fixed ' => false ,
' unsigned ' => false ,
' comment ' => '' ,
' opt ' => []
],
' label ' => [
' name ' => ' label ' ,
' type ' => ' string ' ,
' length ' => 255 ,
' precision ' => null ,
' scale ' => 0 ,
' null ' => false ,
' seq ' => false ,
' default ' => null ,
' fixed ' => false ,
' unsigned ' => false ,
' comment ' => '' ,
' opt ' => [
' charset ' => ' utf8mb4 ' ,
' collation ' => ' utf8mb4_unicode_ci '
]
]
],
' index ' => [
' PRIMARY ' => [
' columns ' => [
0 => ' id '
],
' name ' => ' PRIMARY ' ,
' flags ' => [],
' options ' => [
' lengths ' => [
0 => null
]
],
' unique ' => 1 ,
' primary ' => 1
],
],
' foreign ' => [
' FK_6C73FFCA343B91AE ' => [
' localcol ' => [
0 => ' parentid '
],
' fktable ' => ' test ' ,
' fkcol ' => [
0 => ' id '
],
' name ' => ' FK_6C73FFCA343B91AE ' ,
' onDelete ' => ' CASCADE ' ,
' onUpdate ' => ' CASCADE '
]
]
]
],
' view ' => [
' testview ' => [
' name ' => ' testview ' ,
' sql ' => ' select `testtable`.`id` AS `id`,`testtable`.`label` AS `label` from `testtable` '
]
]
]
データベースのタイプを返します
public function type() : string
可能な値は次のとおりです。
例:
$ type = $ db -> type ();
変更をデータベーススキーマに適用します
public function up() : self
例:
$ db -> up ();
指定されたテーブルのレコードを更新します
public function update( string $ table , array $ data , array $ conditions = [] ) : self
$table
テーブルの名前$data
更新する列名/値のキー/値ペア$conditions
比較する列名と値のキー/値ペア例:
$ db -> update ( ' test ' , [ ' status ' => true ] );
$ db -> update ( ' test ' , [ ' status ' => true ], [ ' status ' => false , ' type ' => ' new ' ] );
2 番目のパラメーターで渡されるいくつかの条件は、「AND」によって結合されます。より複雑なステートメントが必要な場合は、代わりにstmt()
メソッドを使用してください。
ビューがまだ存在しない場合は、指定された名前でビューを作成します
public function view( string $ name , string $ sql , $ for = null ) : self
$name
ビューの名前$sql
ビューにデータを設定するための SELECT ステートメント$for
この SQL が使用されるデータベース タイプ ("mysql"、"mariadb"、"postgresql"、"sqlite"、"sqlserver"、"oracle"、"db2")ビューがまだ存在しない場合は作成されます。そうでなければ何も起こりません。
例:
$ db -> view ( ' testview ' , ' SELECT * FROM testtable ' );
$ db -> view ( ' testview ' , ' SELECT id, label, status FROM testtable WHERE status = 1 ' );
$ db -> view ( ' testview ' , ' SELECT * FROM `testtable` WHERE `status` = 1 ' , ' mysql ' );
移行タスクでtable()
呼び出して取得するテーブル スキーム オブジェクトを使用すると、テーブルへの完全なアクセスが可能になり、列、インデックス、外部キーを追加、変更、削除できます。例:
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> id ();
$ table -> string ( ' label ' );
$ table -> col ( ' status ' , ' tinyint ' )-> default ( 0 );
} );
任意の型の列を追加できるcol()
メソッドのほかに、すべてのデータベース サーバー実装で使用できる型のショートカット メソッドがいくつかあります。
列の種類 | 説明 |
---|---|
でかい | シーケンス/オートインクリメントと主キーが割り当てられた BIGINT 列 |
ビギント | −9223372036854775808 から 9223372036854775807 の範囲の BIGINT 列 |
バイナリ | 最大 255 バイトの VARBINARY 列 |
塊 | 最大 2GB の BLOB カラム |
ブール | BOOLEAN/BIT/NUMBER 列、「boolean」のエイリアス |
ブール値 | TRUE/FALSE を表す BOOLEAN/BIT/NUMBER 列。 0/1 値 |
文字 | 固定文字数の CHAR 列 |
日付 | 時刻とタイムゾーンを含まない ISO 日付形式 (「YYYY-MM-DD)」の DATE 列 |
日時 | ISO 日付/時刻形式の DATETIME 列 ("YYYY-MM-DD HH:mm:ss" ) |
テーブル日付時刻 | ISO 日付/時刻形式だがタイムゾーン形式が異なる DATETIMETZ 列 |
10進数 | 固定小数点精度の数値データの DECIMAL 列 (PHP では文字列) |
フロート | 8 バイト浮動小数点精度の数値データの FLOAT 列 |
ガイド | 36 バイトのグローバルに一意な識別子 |
ID | シーケンス/オートインクリメントと主キーが割り当てられた INTEGER 列 |
整数 | INTEGER 列、「整数」の別名 |
整数 | -2147483648 ~ 2147483647 の範囲の INTEGER 列 |
json | UTF-8 でエンコードされた JSON データの JSON 列 |
smallint | -32768 ~ 32767 の範囲の INTEGER 列 |
弦 | 最大 255 文字の VARCHAR 列 |
文章 | 最大 2GB の文字を含む TEXT/CLOB 列 |
時間 | 24 時間制の「HH:MM」形式の TIME 列、例: 「05:30」または「22:15」 |
uuid | 36 バイトのグローバルに一意な識別子、「guid」のエイリアス |
MySQL (または MariaDB など) は、テーブルの側面を定義するためのいくつかのオプションをサポートしています。エンジンオプションは、テーブルに使用されるストレージ エンジンを指定します。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> opt ( ' engine ' , ' InnoDB ' );
} );
ショートカットとして、オプションをプロパティとして設定することもできます。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> engine = ' InnoDB ' ;
} );
一時テーブルを作成するには、次を使用します。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> temporary = true ;
} );
文字列列とテキスト列のデフォルトの文字セットと照合順序を設定することもできます。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> charset = ' utf8mb4 ' ;
$ table -> collation = ' utf8mb4_unicode_ci ' ;
} );
注:照合順序は PostgreSQL と SQL Server でもサポートされていますが、それらの値は異なります。したがって、すべてのサーバー タイプに同じ値を使用することはできません。この問題を回避するには、列opt()
メソッドを使用し、データベース サーバーの種類を 3 番目のパラメータとして渡します。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> opt ( ' charset ' , ' utf8mb4 ' , ' mysql ' );
$ table -> opt ( ' collation ' , ' utf8mb4_unicode_ci ' , ' mysql ' );
} );
現在、デフォルトの文字セットと照合順序は、 MySQL データベース サーバー (または MariaDB および同様のフォーク) に対してのみ設定されます。
テーブル オプションの現在の値を知る必要がある場合は、次のようにします。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
// return the used table engine ( only MySQL , MariaDB , etc . )
$ engine = $ table -> engine ;
// returns TRUE if it ' s a temporary table
$ isTemp = $ table -> temporary ;
// return the current charset
$ charset = $ table -> charset ;
// return the current collation
$ collation = $ table -> collation ;
} );
テーブルがすでに存在するかどうかを確認するには、 hasTable()
メソッドを使用します。
if ( $ this -> db ()-> hasTable ( ' users ' ) ) {
// The "users" table exists
}
複数のテーブルを一度に確認することもできます。
if ( $ this -> db ()-> hasTable ( [ ' users ' , ' addresses ' ] ) ) {
// The "users" and "addresses" tables exist
}
hasTable()
メソッドは、すべてのテーブルが存在する場合にのみ TRUE を返します。
テーブルの作成とアクセスのほかに、スキーマ オブジェクトのtable()
メソッドを使用してテーブル スキーマを更新することもできます。テーブル名と、テーブル スキーマ オブジェクトを受け取るクロージャを受け入れます。
まず、3 つの列を含むtestという名前のテーブルを作成しましょう。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> id ();
$ table -> string ( ' label ' );
$ table -> col ( ' status ' , ' tinyint ' )-> default ( 0 );
} );
ここで、コード列を追加し、既存のステータス列のデフォルト値を変更することで、別の移行でテーブルを更新したいと思います。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> string ( ' code ' );
$ table -> col ( ' status ' , ' tinyint ' )-> default ( 1 );
} );
table()
メソッドが返されるとすぐに変更がデータベースに保持されるため、後で自分でup()
呼び出す必要はありません。利用可能な列のタイプとオプションについては、「列」セクションを参照してください。
$this->db()
によって返されるデータベース オブジェクトは、 renameTable()
メソッドを使用するときにテーブルの名前を変更できます。
// Renames the table "users" to "accounts"
$ this -> db ()-> renameTable ( ' users ' , ' account ' );
新旧の名前をキーと値のペアとして指定した連想配列を渡すと、複数のテーブルの名前を一度に変更することもできます。
// Renames the table "users" to "accounts" and "blog" to "posts"
$ this -> db ()-> renameTable ( [ ' users ' => ' account ' , ' blog ' => ' posts ' ] );
テーブルの名前は、テーブルが存在する場合にのみ変更されます。テーブルがもう存在しない場合、エラーは報告されません。
$ this -> db ()-> renameTable ( ' notexist ' , ' newtable ' );
この場合、メソッド呼び出しは成功しますが、何も起こりません。
テーブルを削除するには、データベース スキーマからdropTable()
メソッドを使用する必要があります。
$ this -> db ()-> dropTable ( ' users ' );
リストを配列として渡すことで、複数のテーブルを一度に削除することもできます。
$ this -> db ()-> dropTable ( [ ' users ' , ' addresses ' ] );
テーブルは存在する場合にのみ削除されます。テーブルがもう存在しない場合、エラーは報告されません。
$ this -> db ()-> dropTable ( ' notexist ' );
この場合、メソッド呼び出しは成功しますが、何も起こりません。
カスタムメソッドを呼び出すか、未知のメソッド呼び出しをDoctrineテーブルオブジェクトに渡します
public function __call( string $ method , array $ args )
$method
メソッドの名前$args
メソッドパラメータ例:
Upscheme Table オブジェクトのクラス プロパティにアクセスできるカスタム メソッドを登録できます。
Aimeos Upscheme Schema Table:: macro ( ' addConstraint ' , function ( array $ columns ) {
return $ this -> to -> addUniqueConstraint ( $ columns );
} );
$ table -> addConstraint ( [ ' col1 ' , ' col2 ' ] );
利用可能なクラス プロパティは次のとおりです。
$this->table
: Doctrine テーブルのスキーマ
$this->up
: Upscheme オブジェクト
さらに、任意の Doctrine テーブル メソッドを直接呼び出すことができます。例:
$ table -> addUniqueConstraint ( [ ' col1 ' , ' col2 ' ] );
指定されたテーブル オプションの値を返します。
public function __get( string $ name )
$name
テーブルオプション名使用可能なテーブル オプションのリストは次のとおりです。
例:
$ engine = $ table -> engine ;
// same as
$ engine = $ table -> opt ( ' engine ' );
指定されたテーブル オプションの新しい値を設定します。
public function __set( string $ name , $ value )
$name
テーブルオプション名使用可能なテーブル オプションのリストは次のとおりです。
例:
$ table -> engine = ' InnoDB ' ;
// same as
$ table -> opt ( ' engine ' , ' InnoDB ' );
「bigint」タイプの新しい ID 列を作成するか、既存の ID 列を返します。
public function bigid( string $ name = null ) : Column
$name
ID 列の名前列にはシーケンス (自動インクリメント) と主キーが自動的に割り当てられます。列がまだ存在しない場合は作成されます。
例:
$ table -> bigid ();
$ table -> bigid ( ' uid ' );
「bigint」型の新しい列を作成するか、既存の列を返します。
public function bigint( string $ name ) : Column
$name
列の名前列がまだ存在しない場合は作成されます。
例:
$ table -> bigint ( ' testcol ' );
「バイナリ」タイプの新しい列を作成するか、既存の列を返します。
public function binary( string $ name , int $ length = 255 ) : Column
$name
列の名前$length
カラムの長さ(バイト単位)列がまだ存在しない場合は作成されます。
例:
$ table -> binary ( ' testcol ' );
$ table -> binary ( ' testcol ' , 32 );
「blob」タイプの新しい列を作成するか、既存の列を返します。
public function blob( string $ name , int $ length = 0x7fff ) : Column
$name
列の名前$length
カラムの長さ(バイト単位)「blob」列の最大長は 2GB です。列がまだ存在しない場合は作成されます。
例:
$ table -> blob ( ' testcol ' );
$ table -> blob ( ' testcol ' , 0x7fffffff );
「ブール」型の新しい列を作成するか、既存の列を返します。
public function bool( string $ name ) : Column
$name
列の名前このメソッドは boolean() のエイリアスです。列がまだ存在しない場合は作成されます。
例:
$ table -> bool ( ' testcol ' );
「ブール」型の新しい列を作成するか、既存の列を返します。
public function boolean( string $ name ) : Column
$name
列の名前列がまだ存在しない場合は作成されます。
例:
$ table -> boolean ( ' testcol ' );
固定型の「char」型の新しい列を作成するか、既存の列を返します。
public function char( string $ name , int $ length ) : Column
$name
列の名前$length
列の長さ(文字数)列がまだ存在しない場合は、作成されます。
例:
$ table -> char ( ' testcol ' , 3 );
新しい列を作成するか、既存の列を返します
public function col( string $ name , string $ type = null ) : Column
$name
$type
の列のタイプ列がまだ存在しない場合は、作成されます。
例:
$ table -> col ( ' testcol ' );
$ table -> col ( ' testcol ' , ' tinyint ' );
タイプ「日付」の新しい列を作成するか、既存の列を返します
public function date( string $ name ) : Column
$name
列がまだ存在しない場合は、作成されます。
例:
$ table -> date ( ' testcol ' );
タイプ「DateTime」の新しい列を作成するか、既存の列を返します
public function datetime( string $ name ) : Column
$name
列がまだ存在しない場合は、作成されます。
例:
$ table -> datetime ( ' testcol ' );
タイプ「datetimetz」の新しい列を作成するか、既存の列を返します
public function datetimetz( string $ name ) : Column
$name
列がまだ存在しない場合は、作成されます。
例:
$ table -> datetimetz ( ' testcol ' );
タイプ「小数」の新しい列を作成するか、既存の列を返します
public function decimal( string $ name , int $ digits , int $ decimals = 2 ) : Column
$name
$digits
を含む10進数の総数$decimals
小数点後の数桁数列がまだ存在しない場合は、作成されます。
例:
$ table -> decimal ( ' testcol ' , 10 ); // 10 digits incl . 2 decimals
$ table -> decimal ( ' testcol ' , 10 , 4 ); // 10 digits incl . 4 decimals
存在する場合、その名前で与えられた列をドロップします
public function dropColumn( $ name ) : self
$name
列または列の1つが存在しない場合、静かに無視されます。移行タスクが終了またはup()
が呼び出されるまで、変更は適用されません。
例:
$ table -> dropColumn ( ' testcol ' );
$ table -> dropColumn ( [ ' testcol ' , ' testcol2 ' ] );
存在する場合、その名前で与えられたインデックスをドロップします
public function dropIndex( $ name ) : self
$name
名インデックスまたはインデックスのいずれかが存在しない場合、静かに無視されます。移行タスクが終了またはup()
が呼び出されるまで、変更は適用されません。
例:
$ table -> dropIndex ( ' idx_test_col ' );
$ table -> dropIndex ( [ ' idx_test_col ' , ' idx_test_col2 ' ] );
存在する場合、その名前で与えられた外部キーの制約を削除します
public function dropForeign( $ name ) : self
$name
外部キーの制約または制約の名前名外部キーの制約または制約の1つが存在しない場合、それは静かに無視されます。移行タスクが終了またはup()
が呼び出されるまで、変更は適用されません。
例:
$ table -> dropForeign ( ' fk_test_col ' );
$ table -> dropForeign ( [ ' fk_test_col ' , ' fk_test_col2 ' ] );
存在する場合、主キーをドロップします
public function dropPrimary() : self
主キーが存在しない場合、静かに無視されます。移行タスクが終了またはup()
が呼び出されるまで、変更は適用されません。
例:
$ table -> dropPrimary ();
タイプ「フロート」の新しい列を作成するか、既存の列を返します
public function float( string $ name ) : Column
$name
列がまだ存在しない場合は、作成されます。
例:
$ table -> float ( ' testcol ' );
新しい外部キーを作成するか、既存のキーを返します
public function foreign( $ localcolumn , string $ foreigntable , $ foreigncolumn = ' id ' , string $ name = null ) : Foreign
$localcolumn
Local Columnまたは列の名前$foreigntable
人名$foreigncolumn
参照列または列の名前$name
fortion key constraintとfortion Key Indexまたはnull for autogenerated nameの名前外部キー名の長さは、最大の互換性のために30文字以上ではありません。
例:
$ table -> foreign ( ' parentid ' , ' test ' );
$ table -> foreign ( ' parentid ' , ' test ' , ' uid ' );
$ table -> foreign ( ' parentid ' , ' test ' , ' id ' , ' fk_test_pid ' );
$ table -> foreign ( [ ' parentid ' , ' siteid ' ], ' test ' , [ ' uid ' , ' siteid ' ] );
タイプ「GUID」の新しい列を作成するか、既存の列を返します
public function guid( string $ name ) : Column
$name
列がまだ存在しない場合は、作成されます。
例:
$ table -> guid ( ' testcol ' );
列が存在するかどうかを確認します
public function hasColumn( $ name ) : bool
$name
例:
$ table -> hasColumn ( ' testcol ' );
$ table -> hasColumn ( [ ' testcol ' , ' testcol2 ' ] );
インデックスが存在するかどうかを確認します
public function hasIndex( $ name ) : bool
$name
名例:
$ table -> hasIndex ( ' idx_test_col ' );
$ table -> hasIndex ( [ ' idx_test_col ' , ' idx_test_col2 ' ] );
外部キーの制約が存在するかどうかを確認します
public function hasForeign( $ name ) : bool
$name
外部キーの制約または制約の名前名例:
$ table -> hasForeign ( ' fk_test_col ' );
$ table -> hasForeign ( [ ' fk_test_col ' , ' fk_test_col2 ' ] );
タイプ「整数」の新しいID列を作成するか、既存の列を返します
public function id( string $ name = null ) : Column
$name
列には、シーケンス(Autoincrement)と自動的に割り当てられたプライマリキーが取得されます。列がまだ存在しない場合は、作成されます。
例:
$ table -> id ();
$ table -> id ( ' uid ' );
新しいインデックスを作成するか、既存のインデックスを置き換えます
public function index( $ columns , string $ name = null ) : self
$columns
列の列または列の名前はインデックスを生み出しています$name
index nameまたはnull for autogenerated nameインデックス名の長さは、最大の互換性のために30文字以上になることはありません。
例:
$ table -> index ( ' testcol ' );
$ table -> index ( [ ' testcol ' , ' testcol2 ' ] );
$ table ->index( ' testcol ' , 'idx_test_testcol );
タイプ「整数」の新しい列を作成するか、既存の列を返します
public function int( string $ name ) : Column
$name
この方法は、integer()のエイリアスです。列がまだ存在しない場合は、作成されます。
例:
$ table -> int ( ' testcol ' );
タイプ「整数」の新しい列を作成するか、既存の列を返します
public function integer( string $ name ) : Column
$name
列がまだ存在しない場合は、作成されます。
例:
$ table -> integer ( ' testcol ' );
タイプ「json」の新しい列を作成するか、既存の列を返します
public function json( string $ name ) : Column
$name
列がまだ存在しない場合は、作成されます。
例:
$ table -> json ( ' testcol ' );
テーブルの名前を返します
public function name() : string
例:
$ tablename = $ table -> name ();
カスタムスキーマオプションを設定するか、現在の値を返します
public function opt( string $ name , $ value = null )
$name
の名前テーブル関連のカスタムスキーマオプション$value
値利用可能なカスタムスキーマオプションは次のとおりです。
例:
$ charset = $ table -> opt ( ' charset ' );
$ table -> opt ( ' charset ' , ' utf8 ' )-> opt ( ' collation ' , ' utf8_bin ' );
// Magic methods :
$ charset = $ table -> charset ;
$ table -> charset = ' binary ' ;
新しいプライマリインデックスを作成するか、既存のインデックスを置き換えます
public function primary( $ columns , string $ name = null ) : self
$columns
列の列または列の名前はインデックスを生み出しています$name
index nameまたはnull for autogenerated nameインデックス名の長さは、最大の互換性のために30文字以上になることはありません。
例:
$ table -> primary ( ' testcol ' );
$ table -> primary ( [ ' testcol ' , ' testcol2 ' ] );
$ table -> primary ( ' testcol ' , ' pk_test_testcol ' );
列または列のリストを変更します
public function renameColumn( $ from , string $ to = null ) : self
$from
$to
new列名は、最初のパラメーターが配列である場合に無視されます例:
// single column
$ table -> renameColumn ( ' test_col ' , ' test_column ' );
// rename several columns at once
$ table -> renameColumn ( [ ' tcol ' => ' testcol ' , ' tcol2 ' => ' testcol2 ' ] );
インデックスまたはインデックスのリストを変更します
public function renameIndex( $ from , string $ to = null ) : self
$from
配列(新しいインデックス名がnullの場合、生成されます)$to
new index nameまたはnull for autogenerated name(最初のパラメーターが配列である場合は無視)インデックス名の長さは、最大の互換性のために30文字を超えてはなりません。
例:
// generate a new name automatically
$ table -> renameIndex ( ' test_col_index ' );
// custom name
$ table -> renameIndex ( ' test_col_index ' , ' idx_test_col ' );
// rename several indexes at once
$ table -> renameIndex ( [ ' test_col_index ' => null , ' test_index ' => ' idx_test_col ' ] );
タイプ「Smallint」の新しい列を作成するか、既存の列を返します
public function smallint( string $ name ) : Column
$name
列がまだ存在しない場合は、作成されます。
例:
$ table -> smallint ( ' testcol ' );
新しい空間インデックスを作成するか、既存のインデックスを置き換えます
public function spatial( $ columns , string $ name = null ) : self
$columns
列の列または列の名前はインデックスを生み出しています$name
index nameまたはnull for autogenerated nameインデックス名の長さは、最大の互換性のために30文字以上になることはありません。
例:
$ table -> spatial ( ' testcol ' );
$ table -> spatial ( [ ' testcol ' , ' testcol2 ' ] );
$ table -> spatial ( ' testcol ' , ' idx_test_testcol ' );
タイプ「文字列」の新しい列を作成するか、既存の列を返します
public function string( string $ name , int $ length = 255 ) : Column
$name
$length
文字の列の長さの長さこのタイプは、最大255文字に使用する必要があります。より多くの文字には、「テキスト」タイプを使用します。列がまだ存在しない場合は、作成されます。
例:
$ table -> string ( ' testcol ' );
$ table -> string ( ' testcol ' , 32 );
タイプ「テキスト」の新しい列を作成するか、既存の列を返します
public function text( string $ name , int $ length = 0xffff ) : Column
$name
$length
文字の列の長さの長さ「テキスト」列の最大長は2GBです。列がまだ存在しない場合は、作成されます。
例:
$ table -> text ( ' testcol ' );
$ table -> text ( ' testcol ' , 0x7fffffff );
タイプ「時間」の新しい列を作成するか、既存の列を返します
public function time( string $ name ) : Column
$name
列がまだ存在しない場合は、作成されます。このデータタイプは、Oracleデータベースを使用する場合は使用できません。
例:
$ table -> time ( ' testcol ' );
新しい一意のインデックスを作成するか、既存のインデックスを置き換えます
public function unique( $ columns , string $ name = null ) : self
$columns
列の列または列の名前はインデックスを生み出しています$name
index nameまたはnull for autogenerated nameインデックス名の長さは、最大の互換性のために30文字以上になることはありません。
例:
$ table -> unique ( ' testcol ' );
$ table -> unique ( [ ' testcol ' , ' testcol2 ' ] );
$ table -> unique ( ' testcol ' , ' unq_test_testcol ' );
タイプ「GUID」の新しい列を作成するか、既存の列を返します
public function uuid( string $ name ) : Column
$name
この方法は、GUID()のエイリアスです。列がまだ存在しない場合は、作成されます。
例:
$ table -> uuid ( ' testcol ' );
データベーススキーマへの変更を適用します
public function up() : self
例:
$ table -> up ();
移行タスクでcol()
呼び出して取得する列スキーマオブジェクトは、すべての列プロパティにアクセスできます。また、すべてのデータベースでサポートされている列タイプに使用できるショートカットもあります。各列は1つ以上の修飾子メソッドで変更でき、単一の列にインデックスを追加することもできます。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> id ()-> unsigned ( true );
$ table -> string ( ' label ' )-> index ();
$ table -> col ( ' status ' , ' tinyint ' )-> default ( 0 );
} );
この例では、次の列が追加されます。
すべてのデータベースサーバーの実装で利用可能な列タイプのショートカット方法がいくつかあります。
列タイプ | 説明 |
---|---|
Bigid | シーケンス/自動インクリメントと割り当てられた主キーを備えたbigint列 |
ビギント | -922372036854775808から9223372036854775807までの範囲のBigint列 |
バイナリ | 最大255バイトのvarbinary列 |
塊 | 最大2GBのBLOB列 |
ブール | boolean/bit/number colum、「boolean」のエイリアス |
ブール値 | True/False RespのBoolean/Bit/Number Colum。 0/1値 |
文字 | 固定数の文字を持つchar列 |
日付 | ISO日付形式の日付列( "yyyy-mm-dd)時間とタイムゾーンなし |
日時 | ISO日付/時刻形式のDateTime列( "yyyy-mm-dd hh:mm:ss")) |
TablesDateTimetz | ISO日付/時刻形式のDatetimetz列ですが、タイムゾーン形式が変化します |
10進数 | 固定点精度を持つ数値データの小数列(PHPの文字列) |
フロート | 8バイトの浮動小数点精度を持つ数値データのフロート列 |
ガイド | 36バイトのグローバルに一意の識別子 |
ID | シーケンス/自動インクリメントと割り当てられた主キーを備えた整数列 |
整数 | 整数colum、「整数」のエイリアス |
整数 | -2147483648から2147483647までの範囲の整数コルム |
json | UTF-8エンコードされたJSONデータのJSON列 |
smallint | -32768〜32767の範囲の整数コルム |
弦 | 最大255文字のvarchar列 |
文章 | 最大2GB文字のテキスト/CLOB列 |
時間 | 24時間のタイムコラム「HH:mm」から、例えば「05:30」または「22:15」 |
uuid | 36バイトのグローバルに一意の識別子、「GUID」のエイリアス |
データベース固有の列タイプを追加するには、 col()
メソッドを使用します。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> col ( ' status ' , ' tinyint ' );
} );
また、1つ以上の列モディファイアメソッドを呼び出すことにより、列定義を変更することもできます。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> int ( ' number ' )-> null ( true )-> unsigned ( true );
} );
使用可能な列モディファイアメソッドは次のとおりです。
列修飾子 | 説明 |
---|---|
Autoincrement(true) | 整数列を自動インクリメントとして設定します( seq() のエイリアス) |
charset( 'utf8') | 列で使用される文字セット(mysql) |
照合(「バイナリ」) | 列照合(mysql/postgresql/sqlite/sqlserverが互換性がありません) |
コメント(「コメント」) | 列にコメントを追加します(mysql/postgresql/oracle/sqlserver) |
デフォルト(1) | 値が指定されていない場合の列のデフォルト値(デフォルト: NULL ) |
修正(true) | 文字列またはバイナリ列に固定長が必要な場合 |
index( 'idx_col') | 列にインデックスを追加すると、インデックス名はオプションです |
長さ(32) | マックス。文字列とバイナリ列の長さ |
null(true) | null値を列に挿入します |
精度(12) | マックス。 10進数列とフロート列に保存されている数字数を含みます。 10進数 |
プライマリ( 'pk_col') | 列にプライマリキーを追加すると、プライマリキー名はオプションです |
スケール(2) | 10進数列とフロート列で使用される小数桁の正確な数 |
seq(true) | 値が指定されていない場合、整数列を自動インクリメントとして設定します |
Spatial( 'idx_col') | 列に空間(GEO)インデックスを追加すると、インデックス名はオプションです |
ユニーク( 'unq_col') | 列に一意のインデックスを追加すると、インデックス名はオプションです |
符号なし(真) | 符号なしの整数値のみを許可する(mysql) |
列にカスタムスキーマオプションを設定するには、 opt()
メソッドを使用します。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> string ( ' code ' )-> opt ( ' collation ' , ' utf8mb4 ' );
} );
データベースタイプを3番目のパラメーターとして渡すことにより、特定のデータベース実装の列修飾子を設定することも可能です。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> string ( ' code ' )-> opt ( ' collation ' , ' utf8mb4 ' , ' mysql ' );
} );
列が既に存在するかどうかを確認するには、 hasColumn()
メソッドを使用します。
if ( $ this -> db ()-> hasColumn ( ' users ' , ' name ' ) ) {
// The "name" column in the "users" table exists
}
一度に複数の列を確認できます。その場合、 hasColumn()
メソッドは、すべての列が存在する場合にのみtrueを返します。
if ( $ this -> db ()-> hasColumn ( ' users ' , [ ' name ' , ' status ' ] ) ) {
// The "name" and "status" columns in the "users" table exists
}
既にテーブルオブジェクトを持っている場合は、 hasColumn()
も使用できます。
if ( $ table -> hasColumn ( ' name ' ) ) {
// The "name" column in the table exists
}
if ( $ table -> hasColumn ( [ ' name ' , ' status ' ] ) ) {
// The "name" and "status" columns in the table exists
}
列に加えて、列修飾子が設定されているかどうか、どの値を持っているかを確認することもできます。
if ( $ table -> string ( ' code ' )-> null () ) {
// The "code" columns is nullable
}
現在の列修飾子値を取得することは、これらの方法を使用して可能です。
列修飾子 | 説明 |
---|---|
autoincrement() | 列が自動インクリメントの場合(seq()for seq() ) |
charset() | 使用済み文字セット(mysql) |
照合() | 使用済み照合(mysql/postgresql/sqlite/sqlserverが互換性がありません) |
コメント() | 列に関連付けられたコメント(mysql/postgresql/oracle/sqlserver) |
デフォルト() | 列のデフォルト値 |
修理済み() | 文字列またはバイナリ列の長さが固定されている場合はtrue |
長さ() | 文字列またはバイナリ列の最大長 |
null() | NULL値が許可されている場合はTRUE |
精度() | 小数点およびフロートカラムに保存されている最大数桁数を含みます。 10進数 |
規模() | 10進数列とフロート列で使用される小数桁の正確な数 |
seq() | 列が自動インクリメントである場合はtrue |
unsigned() | 符号なしの整数値が許可されている場合のみ(mysql) |
非標準の列修飾子を確認するには、2番目のパラメーターなしでopt()
メソッドを使用します。次に、列修飾子の現在の値を返します。
if ( $ table -> string ( ' code ' )-> opt ( ' charset ' ) === ' utf8 ' ) {
// The "code" columns uses UTF - 8 charset ( MySQL only)
}
文字列列の長さなど、ほとんどの列修飾子を変更することができます。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> string ( ' code ' )-> length ( 64 );
} );
一部の方法では、最も頻繁に使用される修飾子を直接設定するための追加のパラメーターも提供します。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> string ( ' code ' , 64 );
} );
後で行を移行したいために列修飾子をすぐに変更する必要がある場合は、 up()
メソッドを使用して変更を維持します。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> string ( ' code ' , 64 )-> null ( true )-> up ();
// modify rows from "test" table
} );
列タイプの変更は、適切なタイプまたはcol()
メソッドに新しい方法を使用することで可能です。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> text ( ' code ' );
// or
$ table -> col ( ' code ' , ' text ' );
} );
すべての列タイプを別のタイプに変更できるわけではなく、少なくともデータの損失がなければそうではないことに注意してください。整数列を問題なくbigint列に変更できますが、逆の方法は失敗します。 Varchar列(文字列)を整数列に変更する場合も同じことが起こります。
列の名前を変更するには、DBスキーマのrenameColumn()
メソッドを使用します。
// single column
$ this -> db ()-> renameColumn ( ' testtable ' , ' label ' , ' name ' );
// multiple columns
$ this -> db ()-> renameColumn ( ' testtable ' , [ ' label ' => ' name ' , ' stat ' => ' status ' ] );
テーブルオブジェクトが既に利用可能である場合、そのrenameColumn()
メソッドを使用して、1つ以上の列を変更できます。
$ this -> db ()-> table ( ' testtable ' , function ( $ table ) {
// single column
$ table -> renameColumn ( ' label ' , ' name ' );
// multiple columns
$ table -> renameColumn ( [ ' label ' => ' name ' , ' stat ' => ' status ' ] );
} );
すべての場合において、列は存在する場合にのみ削除されます。テーブルに1つ以上の列が存在しない場合、エラーは報告されません。
列をドロップするには、DBスキーマオブジェクトからdropColumn()
メソッドを使用します。
$ this -> db ()-> dropColumn ( ' users ' , ' name ' );
アレイとしてドロップするすべての列の名前を渡すと、複数の列を一度にドロップできます。
$ this -> db ()-> dropColumn ( ' users ' , [ ' name ' , ' status ' ] );
既にテーブルオブジェクトを持っている場合は、 dropColumn()
も使用できます。
// single column
$ table -> dropColumn ( ' name ' );
// multiple columns
$ table -> dropColumn ( [ ' name ' , ' status ' ] );
すべての場合において、列は存在する場合にのみ削除されます。テーブルに1つ以上の列が存在しない場合、エラーは報告されません。
カスタムメソッドを呼び出すか、教義列オブジェクトに不明なメソッド呼び出しを渡す
public function __call( string $ method , array $ args )
$method
メソッドメソッド名$args
メソッドパラメーター例:
Upscheme列オブジェクトのクラスプロパティにアクセスできるカスタムメソッドを登録できます。
Aimeos Upscheme Schema Column:: macro ( ' platform ' , function ( array $ options ) {
return $ this -> to -> setPlatformOptions ( $ options );
} );
$ column -> platform ( [ ' option ' => ' value ' ] );
利用可能なクラスプロパティは次のとおりです。
$this->db
:upscheme dbオブジェクト
$this->table
:教義のテーブルスキーマ
$this->column
:Doctrine Column Schema
さらに、任意のDoctrine Columnメソッドを直接呼び出すことができます。
$ column -> setPlatformOptions ( [ ' option ' => ' value ' ] );
指定された列オプションの値を返します
public function __get( string $ name )
$name
列オプション名使用可能な列オプションのリストは次のとおりです。
例:
$ charset = $ column -> charset ;
// same as
$ charset = $ column -> opt ( ' charset ' );
指定された列オプションの新しい値を設定します
public function __set( string $ name , $ value )
$name
列オプション名$value
列オプション値使用可能な列オプションのリストは次のとおりです。
例:
$ column -> charset = ' utf8 ' ;
// same as
$ column -> opt ( ' charset ' , ' utf8 ' );
列をAutoincrementとして設定するか、現在の値を返します
public function autoincrement( bool $ value = null )
$value
新しいautoincrementフラグまたはnullを返す現在の値を返すこの方法は、 seq()
メソッドのエイリアスです。
例:
$ value = $ column -> autoincrement ();
$ column -> autoincrement ( true );
列のcharsetを設定するか、現在の値を返します
public function charset( string $ value = null )
$value
新しい列のcharsetまたはnull例:
$ comment = $ column -> charset ();
$ column -> charset ( ' utf8 ' );
列の照合を設定するか、現在の値を返します
public function collation( string $ value = null )
$value
新しい列の照合またはnullを返す現在の値を返す例:
$ comment = $ column -> collation ();
$ column -> collation ( ' binary ' );
列のコメントを設定するか、現在の値を返します
public function comment( string $ value = null )
$value
新しい列のコメントまたはnull例:
$ comment = $ column -> comment ();
$ column -> comment ( ' column comment ' );
列のデフォルト値を設定するか、現在の値を返します
public function default( $ value = null )
$value
新しい列のデフォルト値またはnullを返す現在の値を返す例:
$ value = $ column -> default ();
$ column -> default ( 0 );
列の固定フラグを設定するか、現在の値を返します
public function fixed( bool $ value = null )
$value
新しい列固定フラグまたはnullを返す現在の値を返す例:
$ value = $ column -> fixed ();
$ column -> fixed ( true );
列の通常のインデックスを作成します
public function index( string $ name = null ) : self
$name
の名前自動的に生成する例:
$ column -> index ();
$ column -> index ( ' idx_col ' );
列の長さを設定するか、現在の値を返します
public function length( int $ value = null )
$value
新しい列の長さまたはnullの最新値を返す例:
$ value = $ column -> length ();
$ column -> length ( 32 );
列の名前を返します
public function name() : string
例:
$ name = $ column -> name ();
列nullフラグを設定するか、現在の値を返します
public function null( bool $ value = null )
$value
新しい列nullフラグまたはnull現在の値を返す例:
$ value = $ column -> null ();
$ column -> null ( true );
列オプション値を設定するか、現在の値を返します
public function opt( string $ option , $ value = null , $ for = null )
$option
列オプション名$value
新しい列オプション値またはnullを返す現在の値を返す$for
database type例:
$ value = $ column -> opt ( ' length ' );
$ column -> opt ( ' length ' , 64 );
列の精度を設定するか、現在の値を返します
public function precision( int $ value = null )
$value
新しい列の精度値またはnullを返す現在の値を返す例:
$ value = $ column -> precision ();
$ column -> precision ( 10 );
列のプライマリインデックスを作成します
public function primary( string $ name = null ) : self
$name
の名前自動的に生成する例:
$ column -> primary ();
$ column -> primary ( ' pk_col ' );
列スケールを設定するか、現在の値を返します
public function scale( int $ value = null )
$value
新しい列のスケール値またはnullが現在の値を返す例:
$ value = $ column -> scale ();
$ column -> scale ( 3 );
列をAutoincrementとして設定するか、現在の値を返します
public function seq( bool $ value = null )
$value
新しいautoincrementフラグまたはnullを返す現在の値を返す例:
$ value = $ column -> seq ();
$ column -> seq ( true );
列の空間インデックスを作成します
public function spatial( string $ name = null ) : self
$name
の名前自動的に生成する例:
$ column -> spatial ();
$ column -> spatial ( ' idx_col ' );
列タイプを設定するか、現在の値を返します
public function type( string $ value = null )
$value
新しい列タイプまたはnull例:
$ value = $ column -> type ();
$ column -> type ( ' tinyint ' );
列の一意のインデックスを作成します
public function unique( string $ name = null ) : self
$name
の名前自動的に生成する例:
$ column -> unique ();
$ column -> unique ( ' unq_col ' );
列をunsignedフラグを設定するか、現在の値を返します
public function unsigned( bool $ value = null )
$value
new column unsignedフラグまたはnull例:
$ value = $ column -> unsigned ();
$ column -> unsigned ( true );
データベーススキーマへの変更を適用します
public function up() : self
例:
$ column -> up ();
Upschemeは、2つのテーブル間のデータの整合性を強制する外部キーの制約をサポートしています。たとえば、 users_address
テーブルのparentid
列がusers
テーブルのid
列を参照している場合、 users
テーブルに一致する行がなく、 users_address
テーブルに行がありません。 foreign()
メソッドを呼び出すと、そのような制約が作成されます。
$ this -> db ()-> table ( ' users ' , function ( $ table ) {
$ table -> id ();
} );
$ this -> db ()-> table ( ' users_address ' , function ( $ table ) {
$ table -> foreign ( ' parentid ' , ' users ' );
} );
注:列( parentid
)は、参照列( id
)と同じデータ型と列修飾子を持っている必要があります。 foreign()
メソッドは、それを保証し、外部キーの制約と同じ名前の新しいインデックスを自動的に作成します。
users
テーブルのID列の名前が異なる場合は、その名前を3番目のパラメーターとしてforeign()
メソッドに渡します。
$ this -> db ()-> table ( ' users_address ' , function ( $ table ) {
$ table -> foreign ( ' parentid ' , ' users ' , ' uid ' );
} );
外部キーの制約の名前をフォースパラメーターとして渡すことをお勧めします。そのため、後で制約を変更またはドロップする方が簡単です。
$ this -> db ()-> table ( ' users_address ' , function ( $ table ) {
$ table -> foreign ( ' parentid ' , ' users ' , ' id ' , ' fk_test_pid ' );
} );
外部キーに必要な一意の値を取得するために必要な列が複数ある場合は、列名を配列として渡します。
$ this -> db ()-> table ( ' users_address ' , function ( $ table ) {
$ table -> foreign ( [ ' parentid ' , ' siteid ' ], ' users_address ' , [ ' id ' , ' siteid ' ] );
} );
外部キーの制約は、外部テーブルの参照列が更新された場合、異なるアクションを実行できます。標準アクションは、行の削除を制限するか、参照されたID値を更新することです。動作を変更するには、 onDelete()
およびonUpdate()
メソッドを使用します。
$ this -> db ()-> table ( ' users_address ' , function ( $ table ) {
$ table -> foreign ( ' parentid ' , ' users ' )-> onDelete ( ' SET NULL ' )-> onUpdate ( ' RESTRICT ' );
} );
両方の値を同じ値に設定する場合、ショートカットがあります。
$ this -> db ()-> table ( ' users_address ' , function ( $ table ) {
$ table -> foreign ( ' parentid ' , ' users ' )-> do ( ' SET NULL ' );
} );
両方の方法で考えられる値は次のとおりです。
行を削除または更新するときのデフォルトのアクションはカスケードであるため、外部キー列の値は外部テーブルと同じ値に更新されます。
外部キーが既に存在するかどうかを確認するには、 hasForeign()
メソッドを使用します。
if ( $ this -> db ()-> hasForeign ( ' users_address ' , ' fk_usrad_parentid ' ) ) {
// The "fk_usrad_parentid" foreign key in the " users_address " table exists
}
また、一度にいくつかの外部キーの制約をチェックすることも可能です。次に、 hasForeign()
メソッドは、最初の引数として渡されたテーブルにすべての制約が存在する場合にのみtrueを返します。
if ( $ this -> db ()-> hasForeign ( ' users_address ' , [ ' fk_usrad_parentid ' , ' fk_usrad_siteid ' ] ) ) {
// The "fk_usrad_parentid" and "fk_usrad_siteid" foreign keys exist in the "users_address" table
}
テーブルオブジェクトが利用可能な場合、代わりにテーブルのhasForeign()
メソッドを使用できます。
$ this -> db ()-> table ( ' users_address ' , function ( $ table ) {
$ table -> hasForeign ( ' fk_usrad_parentid ' ) ) {
// The "fk_usrad_parentid" foreign key in the " users_address " table exists
}
} );
$ this -> db ()-> table ( ' users_address ' , function ( $ table ) {
$ table -> hasForeign ( [ ' fk_usrad_parentid ' , ' fk_usrad_siteid ' ] ) ) {
// The "fk_usrad_parentid" and "fk_usrad_siteid" foreign keys exist in the "users_address" table
}
} );
既存の制約の現在の値が必要な場合:
$ this -> db ()-> table ( ' users_address ' , function ( $ table ) {
$ fk = $ table -> foreign ( ' parentid ' , ' users ' );
// returns the name of the constraint
$ name = $ fk -> name ()
// returns the action when deleting rows
$ action = $ fk -> onDelete ;
// returns the action when updating the foreign ID
$ action = $ fk -> onUpdate ;
} );
テーブルから外部キーの制約を削除するには、 dropForeign()
メソッドを使用し、テーブルの名前と外部キー名を引数として渡します。
$ this -> db ()-> dropForeign ( ' users_address ' , ' fk_usrad_parentid ' );
また、いくつかの外部キー名を渡してすぐにドロップすることもできます。
$ this -> db ()-> dropForeign ( ' users_address ' , [ ' fk_usrad_parentid ' , ' fk_usrad_siteid ' ] );
table()
メソッドに渡された匿名関数内で、 dropForeign()
メソッドを使用することもできます。
$ this -> db ()-> table ( ' users_address ' , function ( $ table ) {
$ table -> dropForeign ( ' fk_usrad_parentid ' );
} );
$ this -> db ()-> table ( ' users_address ' , function ( $ table ) {
$ table -> dropForeign ( [ ' fk_usrad_parentid ' , ' fk_usrad_siteid ' ] );
} );
カスタムメソッドを呼び出します
public function __call( string $ method , array $ args )
$method
メソッドメソッド名$args
メソッドパラメーター例:
Upscheme外部オブジェクトのクラスプロパティにアクセスできるカスタムメソッドを登録できます。
Aimeos Upscheme Schema Foreign:: macro ( ' default ' , function () {
$ this -> opts = [ ' onDelete ' => ' SET NULL ' , ' onUpdate ' => ' SET NULL ' ];
} );
$ foreign -> default ();
利用可能なクラスプロパティは次のとおりです。
$this->dbaltable
:ドクトリンテーブルスキーマ
$this->table
:upschemeテーブルオブジェクト
$this->localcol
:ローカル列名または名前
$this->fktable
:外国のテーブル名
$this->fkcol
:外国の列名または名前
$this->name
:外部キー名
$this->opts
:外部キーオプションの関連リスト(主に「ondelete」と「onupdate」)
指定された外部キーオプションの値を返します
public function __get( string $ name )
$name
外部キーオプション名利用可能な外部キーオプションのリストは次のとおりです。
両方のオプションの可能な値は次のとおりです。
例:
$ value = $ foreign -> onDelete ;
// same as
$ value = $ foreign -> opt ( ' onDelete ' );
指定された外部キーオプションの新しい値を設定します
public function __set( string $ name , $ value )
$name
外部キーオプション名利用可能な外部キーオプションのリストは次のとおりです。
両方のオプションの可能な値は次のとおりです。
例:
$ foreign -> onDelete = ' SET NULL ' ;
// same as
$ foreign -> onDelete ( ' SET NULL ' );
$ foreign -> opt ( ' onDelete ' , ' SET NULL ' );
指定された外部キーオプションの新しい値を設定します
public function do( string $ action ) : self
$action
実行アクション考えられるアクションは次のとおりです。
例:
$ foreign -> do ( ' RESTRICT ' );
public function name()
例:
$ fkname = $ foreign -> name ();
public function onDelete( string $ value = null )
@param string | null $value
valuedAction actionまたはnull on current値を返す
@return self |値を設定するための同じオブジェクト、パラメーターなしの現在の値
利用可能なアクションは次のとおりです。
例:
$ value = $ foreign -> onDelete ();
$ foreign -> onDelete ( ' SET NULL ' );
// same as
$ foreign -> onDelete = ' SET NULL ' ;
// same as
$ foreign -> opt ( ' onDelete ' , ' SET NULL ' );
$ foreign -> onDelete ( ' SET NULL ' )-> onUpdate ( ' SET NULL ' );
public function onUpdate( string $ value = null )
@param string | null $value
valuedAction actionまたはnull on current値を返す
@return self |値を設定するための同じオブジェクト、パラメーターなしの現在の値
利用可能なアクションは次のとおりです。
例:
$ value = $ foreign -> onUpdate ();
$ foreign -> onUpdate ( ' SET NULL ' );
// same as
$ foreign -> onUpdate = ' SET NULL ' ;
// same as
$ foreign -> opt ( ' onUpdate ' , ' SET NULL ' );
$ foreign -> onUpdate ( ' SET NULL ' )-> onDelete ( ' SET NULL ' );
public function up() : self
例:
$ foreign -> up ();
いくつかのデータベース実装は、自動インクリメント/ID列、つまりOracleとPostgreSQLの代わりにシーケンスを提供します。シーケンスは、新しい行を挿入するときにテーブル列に適用される連続的に増加する数値を作成する関数です。 seq_testという名前の新しいシーケンスを作成するには、 sequence()
メソッドを使用します。
$ this -> db ()-> sequence ( ' seq_test ' );
1
より異なる開始値とステップ幅を使用するには、 start()
とstep()
メソッドを呼び出します。
$ this -> db ()-> sequence ( ' seq_test ' , function ( $ seq ) {
$ seq -> start ( 1000 )-> step ( 2 );
} );
シーケンスが既に存在するかどうかを確認するには、 hasSequence()
メソッドを使用します。
if ( $ this -> db ()-> hasSequence ( ' seq_test ' ) ) {
// The "seq_test" sequence exists
}
また、いくつかのシーケンスを一度にチェックすることも可能です。次に、 hasSequence()
メソッドは、すべてのシーケンスが存在する場合にのみtrueを返します。
if ( $ this -> db ()-> hasSequence ( [ ' seq_id ' , ' seq_test ' ] ) ) {
// The "seq_id" and "seq_test" sequences exist
}
テーブルオプションの現在の値を知る必要がある場合に備えてください。
$ this -> db ()-> sequence ( ' seq_test ' , function ( $ seq ) {
// returns how many generated numbers are cached
$ cache = $ seq -> cache ;
// returns the number the sequence has started from
$ start = $ seq -> start ;
// returns the step width for newly generated numbers
$ step = $ seq -> step ;
} );
シーケンスを削除するには、 dropSequence()
メソッドを使用し、シーケンスの名前を引数として渡します。
$ this -> db ()-> dropSequence ( ' seq_id ' );
また、いくつかのシーケンス名を渡して一度にドロップすることもできます。
$ this -> db ()-> dropSequence ( [ ' seq_id ' , ' seq_test ' ] );
カスタムメソッドを呼び出すか、教義のテーブルオブジェクトに不明なメソッド呼び出しを渡す
public function __call( string $ method , array $ args )
$method
メソッドメソッド名$args
メソッドパラメーター例:
Upschemeシーケンスオブジェクトのクラスプロパティにアクセスできるカスタムメソッドを登録できます。
Aimeos Upscheme Schema Sequence:: macro ( ' default ' , function () {
$ this -> start ( 1 )-> step ( 2 );
} );
$ sequence -> default ();
利用可能なクラスプロパティは次のとおりです。
$this->db
:upscheme dbオブジェクト
$this->sequence
:ドクトリンシーケンススキーマ
指定されたシーケンスオプションの値を返します
public function __get( string $ name )
$name
sequenceオプション名例:
$ value = $ sequence -> getInitialValue ();
// same as
$ value = $ sequence -> start ();
指定されたシーケンスオプションの新しい値を設定します
public function __set( string $ name , $ value )
$name
sequenceオプション名例:
$ value = $ sequence -> setInitialValue ( 1000 );
// same as
$ value = $ sequence -> start ( 1000 );
シーケンスのキャッシュサイズを設定するか、現在の値を返します
public function cache( int $ value = null )
$value
例:
$ value = $ sequence -> cache ();
$ sequence -> cache ( 100 );
シーケンスの名前を返します
public function name()
$ name = $ sequence -> name ();
シーケンスの新しい開始値を設定するか、現在の値を返します
public function start( int $ value = null )
$value
シーケンスまたはnullの新しい開始値現在の値を返す $ value = $ sequence -> start ();
$ sequence -> start ( 1000 );
新しいシーケンス値のステップサイズを設定するか、現在の値を返します
public function step( int $ value = null )
$value
新しいステップサイズシーケンスは、現在の値を返すためにシーケンスが増加または削減されます。 $ value = $ sequence -> step ();
$ sequence -> step ( 2 );
データベーススキーマへの変更を適用します
public function up() : self
$ sequence -> up ();
インデックスはデータベースクエリをスピードアップし、クエリのニーズが正しく使用すると数分からミリ秒に低下する可能性があります。利用可能ないくつかのインデックスタイプがあります:
すべてのインデックスは1つ以上の列で構成できますが、インデックスがクエリに使用されるかどうかにかかわらず、列の順序は大きな影響を及ぼします。
すべてのインデックスは、インデックスがカバーする列を含むテーブルにバインドされています。単一の列でインデックスを作成する最も簡単な方法は、列オブジェクトのindex()
メソッドを使用することです。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> string ( ' label ' )-> index ();
} );
index()
メソッドの2番目のパラメーターを使用すると、インデックスのカスタム名を設定できます。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> string ( ' label ' )-> index ( ' idx_test_label ' );
} );
注:異なるデータベースタイプ間の最大互換性の場合、インデックス名の長さは30文字以下でなければなりません。
プライマリで一意の空間インデックスでも同じことが可能です。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
// primary key
$ table -> int ( ' id ' )-> primary ();
$ table -> int ( ' id ' )-> primary ( ' pk_test_id ' ); // ignored by MySQL , MariaDB , etc .
// unique key
$ table -> string ( ' code ' )-> unique ();
$ table -> string ( ' code ' )-> unique ( ' unq_test_code ' );
// spatial index
$ table -> col ( ' location ' , ' point ' )-> spatial ();
$ table -> col ( ' location ' , ' point ' )-> spatial ( ' idx_test_location ' );
} );
マルチコラムインデックスの場合、 primary()
、 unique()
およびindex()
メソッドは、テーブルオブジェクトで使用できます。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
// primary composite index
$ table -> primary ( [ ' siteid ' , ' code ' ] );
// unique composite index
$ table -> unique ( [ ' parentid ' , ' type ' ] );
// regular composite index
$ table -> index ( [ ' label ' , ' status ' ] );
} );
空間インデックスは複数の列にまたがることはできませんが、テーブルオブジェクトのspatial()
メソッドを使用して作成することも可能です。
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> spatial ( ' location ' );
} );
インデックスが既に存在するかどうかを確認するには、 hasIndex()
メソッドを使用します。
if ( $ this -> db ()-> hasIndex ( ' users ' , ' idx_users_name ' ) ) {
// The "idx_users_name" index in the "users" table exists
}
You can check for several indexes at once too. In that case, the hasIndex()
method will only return TRUE if all indexes exist:
if ( $ this -> db ()-> hasIndex ( ' users ' , [ ' idx_users_name ' , ' idx_users_status ' ] ) ) {
// The "idx_users_name" and "idx_users_status" indexes in the "users" table exists
}
If you already have a table object, you can use hasIndex()
as well:
if ( $ table -> hasIndex ( ' idx_users_name ' ) ) {
// The "idx_users_name" index in the table exists
}
if ( $ table -> hasIndex ( [ ' idx_users_name ' , ' idx_users_status ' ] ) ) {
// The "idx_users_name" and "idx_users_status" indexes in the table exists
}
To rename indexes directly, using the renameIndex()
method of the DB schema:
// single index
$ this -> db ()-> renameIndex ( ' testtable ' , ' idx_test_label ' , ' idx_test_name ' );
// multiple indexes
$ this -> db ()-> renameIndex ( ' testtable ' , [ ' idx_test_label ' => ' idx_test_name ' , ' idx_text_stat ' => ' idx_test_status ' ] );
If a table object is already available, you can use its renameIndex()
method to rename one or more indexes:
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
// single index
$ table -> renameIndex ( ' idx_test_label ' , ' idx_test_name ' );
// multiple indexes
$ table -> renameIndex ( [ ' idx_test_label ' => ' idx_test_name ' , ' idx_text_stat ' => ' idx_test_status ' ] );
} );
In all cases, indexes are only renamed if they exist. No error is reported if one or more indexes doesn't exist in the table.
To drop indexes, use the dropIndex()
method from the DB schema object:
$ this -> db ()-> dropIndex ( ' users ' , ' idx_test_name ' );
You can drop several indexes at once if you pass the name of all indexes you want to drop as array:
$ this -> db ()-> dropIndex ( ' users ' , [ ' idx_test_name ' , ' idx_test_status ' ] );
If you already have a table object, you can use dropIndex()
too:
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
// single index
$ table -> dropIndex ( ' idx_test_name ' );
// multiple indexes
$ table -> dropIndex ( [ ' idx_test_name ' , ' idx_test_status ' ] );
} );
In all cases, indexes are only removed if they exist. No error is reported if one or more indexes doesn't exist in the table.
It's not necessary to pass a custom index name when creating new indexes. Then, the index name is generated automatically but their name will consist of a hash that is hard to read. Also, you don't know which columns the indexes span from the index name.
Upscheme allows you to add your own naming function for indexes which is used if not index name is passed to the methods for creating indexes. Before running the migrations, register your nameing function using the macro()
method in the table objects:
use Aimeos Upscheme Schema Table ;
Table:: marco ( ' nameIndex ' , function ( string $ table , array $ columns , string $ type ) {
return $ type . ' _ ' . $ table . ' _ ' . join ( ' _ ' , $ columns );
} );
Aimeos Upscheme Up:: use ( $ config , ' ./migrations/ ' )-> up ()
For a table "testtable", a column "label" and the type "idx", this will return idx_testtable_label instead of a hash.
Available index types are:
Note: For compatibility to all supported database types, the maximum length of the index names must be not longer than 30 characters!
You can add new methods to all Upscheme objects using the macro()
method. Each custom method has access to the class properties and methods of the class it's registered for including the Doctrine DBAL objects.
To register a method named test()
in the DB schema object with two parameters $arg1
and $arg2
which has access to the same class properties as the DB __call()
method use:
Aimeos Upscheme Schema DB :: marco ( ' test ' , function ( $ arg1 , $ arg2 ) {
// $ this - >conn : Doctrine connection
// $this - >from : Doctrine start schema
// $this - >to : Doctrine current schema
// $ this - >up : Upscheme object
// return $this or a value
} );
$ db -> test ( ' key ' , ' value ' );
Registering a method test()
in the Table schema object with one parameter $arg1
which has access to the same class properties as the Table __call()
method use:
Aimeos Upscheme Schema Table:: marco ( ' test ' , function ( $ arg1 ) {
// $this - >db : Upscheme DB object
// $this - >table : Doctrine Table object
// return $this or a value
} );
$ table -> test ( ' something ' );
Same for a method test()
in the Column schema object with an optional parameter $value
which has access to the same class properties as the Column __call()
method use:
Aimeos Upscheme Schema Column:: marco ( ' test ' , function ( $ value = null ) {
// $this - >db : Upscheme DB object
// $this - >table : Upscheme Table object
// $this - >column : Doctrine Column object
// return $this or a value
} );
$ column -> test ();
To extend the Foreign object for foreign key constraints with a test()
method with no parameter having access to the same class properties as the Foreign __call()
method use:
Aimeos Upscheme Schema Foreign:: marco ( ' test ' , function () {
// $this - >table : Upscheme Table object
// $this - >dbaltable : Doctrine Table object
// $this - >localcol : Array of local column names
// $this - >fktable : Foreign table name
// $ this - >fkcol : Foreign table column names
// $this - >name : Foreign key name
// $ this - >opts : Array of foreign key options ( "onDelete" and "onUpdate")
// return $this or a value
} );
$ foreign -> test ();
Finally, extending the Sequence object with a test()
method having no parameters and access to the same class properties as the Sequence __call()
method use:
Aimeos Upscheme Schema Sequence:: marco ( ' test ' , function () {
// $this - >db : Upscheme DB object
// $this - >sequence : Doctrine Sequence object
// return $this or a value
} );
$ sequence -> test ();
Instead of calling the col()
method of the Table object with all parameters and modifiers each time, you can create your own shortcut methods, eg:
Aimeos Upscheme Schema Table:: marco ( ' utinyint ' , function ( string $ name ) {
return $ this -> col ( $ name , ' tinyint ' )-> unsigned ( true );
} );
It's also possible to create several columns at once if you want to add them to several tables:
Aimeos Upscheme Schema Table:: marco ( ' defaults ' , function () {
$ this -> id ();
$ this -> datetime ( ' ctime ' );
$ this -> datetime ( ' mtime ' );
$ this -> string ( ' editor ' );
return $ this ;
} );
Then, use your custom methods when creating or updating tables:
$ this -> db ()-> table ( ' test ' , function ( $ table ) {
$ table -> defaults ();
$ table -> utinyint ( ' status ' );
} );
Version 0.9+ supports Doctrine DBAL 3.x/4.x and dropped support for Doctrine DBAL 2.x.
DB::type()
returns mariadb
instead of mysql
for MariaDDB databaseDB::type()
returns sqlserver
instead of mssql
for Microsoft SQLServer databaseDB::for()
, DB::view()
and Column::opt
require ['mariadb', 'mysql']
to get the same resultsDB::lastId()
doesn't require/support passing a sequence name because Doctrine DBAL removed it but doesn't support Oracle IDENTITY columns at the moment