->selectAs() (Previous) (Next) ->set*() and ->get*()

View this page in Last updated: Sun, 07 Sep 2008
English | Dutch | French | German | Hungarian | Japanese | Polish | Russian | Spanish | Plain HTML

->joinAdd()

->joinAdd() -- JOIN クエリを生成するために他の DataObject を追加する

説明

他の DataObject を追加して JOIN クエリを生成します。 このメソッドを使用するときは注意してください。 そのままのクエリの方が joinAddメソッドを使用するよりも 分かりやすいかも知れません。

このメソッドを実装してくれた Stijn de Reede に感謝します。

パラメータ

  • object $obj - 結合されるオブジェクトを 指定します。 (値がない場合、結合がクリアされます)

  • string $joinType - "LEFT" | "INNER " | "RIGHT" | ""

    INNER がデフォルトで、"" は結合やリンクが WHERE 句に追加されず、単純に select ... from a,b,c を実行します。

    注意: 'LEFT' は LEFT OUTER と等価です。

  • string $joinAs - テーブルを別名で扱いたい場合、 二次テーブルからカラムを複数選択したいときに役に立つでしょう

  • string $joinCol - このオブジェクトのテーブルでマッチさせたいカラムを指定します。 このテーブルを子オブジェクトの複数箇所にリンクする場合に必要です。

注意

この関数は、スタティックにコールする ことはできません。

以下の例はテストされていません。DB_DataObject::debugLevel(1) を使用し、 これを使用したときに何が行われているかを理解し、 著者により良い例を送ってください。

->selectAs() (Previous) (Next) ->set*() and ->get*()

Download Documentation Last updated: Sun, 07 Sep 2008
Do you think that something on this page is wrong? Please file a bug report or add a note.
User Notes:
Note by: johng@neutralize.com

This took me ages and ages to figure out...

You can do some nice JOINs, so in the above example we can grab the image, product name and group name:

// -------------------------------------------------
// an example with 2 joins
// get all the images linked with products or productgroups
// and the product name and group name.
$i = new DataObject_Image();
$pi = new DataObject_Product_image();
$pgi = new DataObject_Productgroup_image();
$i->joinAdd($pi);
$i->joinAdd($pgi);

$i->selectAdd();
$i->selectAdd( 'image, product.name as p_name, product_group.name as g_name' );

$i->find();
while ($i->fetch()) {
echo $i->image;
echo $i->p_name;
echo $i->g_name;
}

// -------------------------------------------------

Note how the image object has new attributes called p_name and g_name? How cool is that?!

You may need to look at the syntax of addJoin, because you can also do:

// -------------------------------------------------

$i = DB_DataObject::factory('Items');
$status = DB_DataObject::factory('Status');

// is the join in your database.links.ini file? If not use:
// $i->joinAdd( $status, "LEFT", 'status', 'fk_status' );

// it is in the .ini file so use:
$i->joinAdd( $status, "LEFT" );

$i->joinAdd( $status, "LEFT", 'minor_status', 'fk_minor_status' );

$i->selectAdd( 'item.name as name, status.name as status, minor_status.name as minor_status' );

// -------------------------------------------------

This assumes that Items has two foreign keys, both to status (fk_status and fk_minor_status)

I have used 'LEFT' joins because no all items will have a status (it is null) but we still want to list that item.

Don't forget: DB_DataObject::debugLevel(1); is your best friend ever, please invite him to the party and make sure he has a drink.

I hope this helps someone out. I think these type of JOINs are a basic part of SQL, so it's nice to see them included in DB_DataObject. I have logged this a documentation bug, so I hope this may be fixed one day...

monk.e.boy
http://teethgrinder.co.uk/open-flash-chart/