RE: [PEAR] How to alias table names in DB_DataObject?
| From: | Andy Crain | Date: | Tue, 05 Aug 2003 19:32:15 +0000 |
| Subject: | RE: [PEAR] How to alias table names in DB_DataObject? | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-7028@lists.php.net to get a copy of this message | ||
OK, I just answered my own question. Sorry.
I hadn't realized there were optional additional parameters to
joinAdd(), whereby you can provide a table alias for the table joined as
the third parameter.
Changed the two lines below to:
$this->joinAdd($owned_by,'INNER','u1');
$this->joinAdd($edited_by,'INNER','u2');
and all is well.
Andy
> -----Original Message-----
> From: Andy Crain [mailto:crain@fuse.net]
> Sent: Tuesday, August 05, 2003 2:52 PM
> To: pear-general@lists.php.net
> Subject: [PEAR] How to alias table names in DB_DataObject?
>
> 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