Re: [Maypole-dev] Maypole/View/Base.pm patch, table -> table_alias

From: Tony Bowden (tony at kasei.com)
Date: Mon Oct 25 2004 - 08:33:42 BST


On Mon, Oct 25, 2004 at 09:07:19AM +0200, Marcus Ramberg wrote:
> If they are interchangeable, I think Class::DBI should return the alias
> when it's set, I suggest you post about this on the CDBI list. However,
> if that route is not possible, I guess here would be to somehow make
> Maypole::Model::CDBI::table return the aliased name. This might be as
> easy as overriding "table" in ::CDBI, but I'm not sure of the
> implications for setup.

There seems to be some misunderstanding here as to what table_aliase
does.

CDBI allows you to encapsulate the name of your table in one place -
the Class->table line. You never need to refer to it ever again.. If
you're dealing with a legacy system where your orders are stored in a
table called MYARU62, you can set that in MySite::Order->table() up and
forget it. When you need to write SQL you can just refer to it as __TABLE__.
The problem comes when you want to write a join. When you use CDBI's
inbuilt JOIN syntax, it will create SQL along the lins of:

  SELECT order.id (from Essential)
    FROM MYARU62 as order, MYARU79 as customer
   WHERE order.customer = customer.id
     AND ...

i.e. it allows you to write the SELECT part of your query using the
names you're used to dealing with rather than the MYARU62.id you'd need
to know otherwise.

It does this by using table_alias, which defaults to the moniker of your
class.

However, in the above example, the SQL is invalid, as ORDER is a reserved
word. To get around this you need to set your table_alias to something
else (say, orders).

I'm pretty sure this is the only place table_alias ever gets used within
CDBI itself. That doesn't mean you can't use it for other things, as it
shouldn't break anything. And using it in Myapole to represent a name
that gets displayed is possibly a good idea.

But it's certainly not interchangable with 'table' - override table to be
orders, and all your SQL will fail as it'll go looking for the wrong
table.

Tony

_______________________________________________
maypole-dev mailing list
maypole-dev at lists.netthink.co.uk
http://lists.netthink.co.uk/listinfo/maypole-dev



This archive was generated by hypermail 2.1.3 : Thu Feb 24 2005 - 22:25:56 GMT