データベースの設計時、しばしばいくつかの表が他と関連付けられます - 会員テーブルは個人の ID とそれらがメンバーであるグループ ID への参照を含んでいます。 リンクメソッド群を使用することで、 親変数にオブジェクトを自動的にフェッチする事ができます。
自動化されたリンクは、databasename.links.ini ファイルによってサポートされます。 これには表同士の関連と、 ある表のカラムから他方へのマッピングがストアされます。 この databasename.links.ini ファイルは、 主キーオブジェクトの関連情報を処理する、 または素早く複雑な複数テーブルを使用したクエリを構築するために getLinks() と joinAdd() メソッドで使用されます。
リンクを使用する他の方法は、getLink() メソッドを経由することです。 これは、カラムを明示し、それが他の表とカラムにどの様に関連しているかを database.links.ini を使わずに手動で指定する事ができます。
getlinks と joinAdd のゴールは、 コードが合理的で分かりやすい事を保証する限り、 2つのテーブルの接続を可能な限りシンプルで高速に作成することです。 以下の例では、初期フェッチ後のオブジェクトでさらなるデータを フェッチするために getlinks() がどのように使用できるかを例示しています。 また、完全な大きな結合クエリを構築する前に links ファイルをテストするためにも使用することができます。
リンクと結合のシンプルな導入
<?php
// just loop and fetch more information
$person = new DataObjects_Person;
$person->eyeColour = 'red';
$person->find();
while ($person->fetch()) {
// this will look up in the cars table for the id matching the person->car value.
$car = $person->getLink('car','cars','id');
echo "{$person->name} has red eyes and owns a {$car->type}\n";
}
// now if you create a database.links.ini file with the car example
// the example would look like this.
$person = new DataObjects_Person;
$person->eyeColour = 'red';
$person->find();
while ($person->fetch()) {
// use the links.ini file to automatically load
// the car object into $person->_car
$person->getLinks();
echo "{$person->name} has red eyes and owns a {$person->_car->type}\n";
}
// and finally the most complex, using SQL joins and select as.
// the example would look like this.
$person = new DataObjects_Person;
$person->eyeColour = 'red';
// first, use selectAs as to make the select clauses match the column names.
$person->selectAs();
// now lets create the related element
$car = new DataObjects_Car;
// and for fun.. lets look for red eys and red cars..
$car->colour = 'red';
// add them together.
$person->joinAdd($car);
// since both tables have the column id in them, we need to reformat the query so
// that the car columns have a different name.
$person->selectAs($car,'car_%s');
$person->find();
while ($person->fetch()) {
echo "{$person->name} has red eyes and owns a {$person->car_type}\n";
}
?>
DB_DataObject バージョン 0.3 はリンク ini ファイルの生成機能を取り入れました。これにより、ini ファイルを使用してカラムと他のデータベースカラムをマップする事ができます。この ini ファイルは 'databasename.links.ini' という名前を持ち、createTables.php によって意自動生成されるデータベーススキーマ ini ファイル 'databasename.ini' と同じフォルダに配置されます。
databasename.links.ini は各表に対するセクションを含んでおり、 リンクされるカラムと、表と関連付けられるカラムを等号で結びます。 以下の例では、person.owner が grp.id にリンクされるように 非主キーから主キーへの関連となっています。 これは person オブジェクトで getLinks() メソッドを実行すると、 3 つのテーブル - colurs, grp, attachments からデータをフェッチする事になるでしょう。
もし、キー (カラムからのリンク) に 'full stop' を使用する場合、 getLinks() メソッドは 'full stop' の左側と文字列がマッチするカラム名を表内から検索します。 そして、'full stop' をアンダースコアに置換し、 オブジェクト変数をその名前に割り当てます。もしくは、(joinAdd() メソッドを使用している場合) 他のオブジェクトから返されるべきカラムをどの様にしたいかを selectAs() メソッドを使用して決定することができます。
databasename.links.ini ファイルの例
; for table person [person] ; link value of eycolor to table colors, match name column eyecolor = colors:name ; link value of owner to table grp, match id column owner = grp:id ; link value of picture to table attachments, match id column picture = attachments:id ; for a sales example with multiple links of a single column [sales] ; for autoloading the car object into $sales->_car_id car_id = car:id ; for autoloading the part number object into $sales->_car_id_partnum car_id.partnum = part_numbers:car_id
複数のカラムからなる複合キーによる結合も可能です。 以下の構文を使用します。
複合キーによるテーブルのリンク
[table_b] field1,field2 = table_a:field1,field2
これは、次のような select 文になります (INNER JOIN 構文を使用します)。
結果の SQL
<?php
SELECT * FROM table_b INNER JOIN table_a ON table_b.field1 = table_a.field1 AND table_b.field2 = table_a.field2
?>