->getLinks() (Previous) (Next) ->joinAdd()

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

->selectAs()

->selectAs() -- クエリの取得 コンポーネントを構築する (通常は結合のため)

説明

現在のオブジェクトのカラム名、提供されたオブジェクトあるいは配列に基づいた 取得アイテムを自動生成します。

これは、カラム名の衝突を起こしている時 (両方の表に 'id' カラムが存在するなど) 、主に固定フォーマットでカラムの改名を可能にするための joinAdd を用いた結合で使用されます。

selectAs にパラメータを渡さない場合、現在の SELECT (通常デフォルトの *) をリセットし、現在のオブジェクトのカラム名に基づいた 取得リストを構築します。

パラメータ

  • object | array $column_or_object - データオブジェクトもしくはカラム名の配列を指定します

  • string $format - カラムが現れたときに使用するフォーマットで、sprintf フォーマットを指定します。 %s はカラム名に置換されますので、car_%s は SQL 中で 'car.name as car_name' が生成されることなります。

  • string $tableName - これは1番目の引数として配列が指定された、 あるいはテーブルを他の名前で結合している場合に使用されます。

注意

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

例 39-1selectAs() の使用


<?php
// ok lets see what is going on..
DB_DataObject::debugLevel(1);

$person = new DataObjects_Person;


// to generate "person.id as id , person.name as name ......."
$person->selectAs();

// to generate a restricted list.. "person.age as age , person.name as name"
$person->selectAs(array('age','name'));

// using another object.
$car = new DataObjects_Car;

// this is the first car (
$car->use 'first';


// using the joinadd add the car..
$person->joinAdd($car);

// now add all the select columns for the car eg. "car.id as car_id, car.name as car_name ...."
$person->selectAs($car'car_%s');


// select only a few columns from the car table.
// note you have to use the table name at the end..
$person->selectAs(array('color','topspeed'), 'car_%s','car');




// now the user can have a second car....
$secondcar = new DataObjects_Car;
$secondcar->use 'second';

// now since we alreay have one car, the sql statement would get messy
// so we are joining this as the second car "FROM person INNER JOIN car ..... , car as secondcar WHERE .....
$person->joinAdd($secondcar,'','secondcar');

// and we can now add all the columns
// "secondcar.id as secondcar_id, secondcar.name as secondcar_name ........
// note that you must use the last field as the SECONDCAR.ID format uses the 'AS' name, rather than the
// objects real table name 'car'
$person->selectAs($secondcar'secondcar_%s','secondcar');



// ok fire of a query...
$person->find();
while ($person->fetch()) {
  ......
}


?>

->getLinks() (Previous) (Next) ->joinAdd()

Download Documentation Last updated: Sun, 31 Aug 2008
Do you think that something on this page is wrong? Please file a bug report or add a note.
User Notes:
Note by: user@example.com
To have columns prefixed for primary table :
$do->selectAs(null, 'prefix_%s');