PEAR Manual
Prev |  | Next

# ->joinAdd()

->joinAdd() -- add another dataobject to build a create join query

## Synopsis

``void $DB_DataObject->joinAdd` ([object $dataobject [, string $joinType [, string $joinAs [, string $joinCol]]]])`

## Description

Builds a Join Query, by adding another dataobject to this one. Be careful when using this, raw queries may be clearer than using joinAdd.

Thanks to Stijn de Reede for the implementation of this.

## Parameter

- `object $obj` - joining object (no value resets the join)

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

  INNER is default, "" indicates just select ... from a,b,c with no join and links are added as where items.

  Note: 'LEFT' is the same as LEFT OUTER.

- `string $joinAs` - if you want to select the table as anther name useful when you want to select multiple columns from a secondary table.

- `string $joinCol` - The column on This objects table to match,needed if this table links to the child object in multiple places eg.

  Example 33-1. using specific join Columns
      user->friend (is a id of a person)
      user->mother (is a id of another person)

## Note

This function can not be called statically.

The Examples below are not tested, use DB_DataObject::debugLevel(1), to see what exactly is going on when you use this, and send the author some better examples..

## Example

Example 33-2. Simple simple Join
    // (requires links.ini to be set up correctly)
    // get all the images for product 24

    $i = new DataObject_Image();
    $pi = new DataObjects_Product_image();
    $pi->product_id = 24; // set the product id to 24
    $i->joinAdd($pi); // add the product_image connectoin
    $i->find();
    while ($i->fetch()) {
    // do stuff
    }

Example 33-3. Resulting SQL
    SELECT * FROM image,product_image
      LEFT JOIN image ON product_image
      WHERE product_image.id = 24
        AND product_image.pid = product.id

Example 33-4. More Complex Join query
    // an example with 2 joins
    // get all the images linked with products or productgroups
    $i = new DataObject_Image();
    $pi = new DataObject_Product_image();
    $pgi = new DataObject_Productgroup_image();
    $i->joinAdd($pi);
    $i->joinAdd($pgi);
    $i->find();
    while ($i->fetch()) {
    // do stuff
    }

Example 33-5. Resulting SQL
    SELECT * FROM image,product_image
      LEFT JOIN image ON product_image
      LEFT JOIN image ON productgroup_image
      WHERE product
        AND product.id = product_image.pid
        AND product.id = productgroup_image.pid

Prev | Home | Next
->selectAs() | Up | ->set*() and ->get*()