How to alias table names in DB_DataObject?
| From: | Andy Crain | Date: | Tue, 05 Aug 2003 18:51:33 +0000 |
| Subject: | How to alias table names in DB_DataObject? | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-7027@lists.php.net to get a copy of this message | ||
Is there a way in DB_DataObject to select from a table with an alias,
e.g. "select stuff, more_stuff from table as my_table"?
I ask because I'm attempting to execute a query with two joins, each of
which is to the same table, aliased differently, e.g:
"select main_table.stuff, main_table.more_stuff, table_1.stuff,
table_2.stuff
from main_table
inner join other_table table_1 on main_table.blah = table_1.blah
inner join other_table table_2 on main_table.blah = table_2.blah"
Problem is, using DataObject's joinAdd(), I can't alias the linked
tables, so I end up with something like the following, which produces an
error:
"select main_table.stuff, main_table.more_stuff, table_1.stuff,
table_2.stuff
from main_table
inner join other_table on main_table.blah = other_table.blah
inner join other_table on main_table.blah = other_table.blah"
I'm using DataObject like below to attempt to generate the query:
require_once('DB/DataObject/Classes/Users.php');
$owned_by = new DataObjects_Users;
$edited_by = new DataObjects_Users;
//join up
$this->joinAdd($owned_by);
$this->joinAdd($edited_by);
//rename fields, otherwise there'd be naming conflict
$this->selectAs();
$this->selectAs($owned_by,'owned_by_%s','u1');
$this->selectAs($edited_by,'edited_by_%s','u2');
The third parameter in the selectAs calls works to prefix the selected
fields with appropriately aliased table names, but the tables aren't
aliased in the FROM clause. I suppose I could just use query() and raw
SQL, but I'd rather use the methods. Can I?
Thanks,
Andy