Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Accessing custom tables with template globals

Quote Reply
Accessing custom tables with template globals
Hi all.

I've created a new table, "Links_Sponsors".

I'd like to create a Template Global that accesses this table, say something like:

sub {
my $acctname = 'whatever';
my $sponsor_db = $DB->table('Links_Sponsors');
my $sth = $sponsor_db->select ({ AcctName => $acctname });
my $sponsor = $sth->fetchrow_hashref;
return $sponsor;
}

(This is just an example)

How would I define the $DB->table('Links_Sponsors'); part so that the table is actually accessed? I tried just using $DB->table('Sponsors'); but that caused the entire global code to show up on the page.

Any help is appreciated.

Thanks!

Matt Glaspie
Quote Reply
Re: Accessing custom tables with template globals In reply to
How did you create the table?

Did you also create a .def file for it?

if your global is called link_sponsor, then <%link_sponsor%> should be all you'd need to insert in your template to make it work.



PUGDOGŪ Enterprises, Inc.
FAQ:http://LinkSQL.com/FAQ
Forum:http://LinkSQL.com/forum
Quote Reply
Re: Accessing custom tables with template globals In reply to
Pugdog, thanks for the reply...

I created the table using MySQLMan... is there another way to do it (via LinksSQL, that is)? I did not create a def file for it... are there instructions for doing so somewhere? In the resources section, I saw a tutorial for the "DBSQL Mini Guide" which talks about using the module to create .def files, but I don't think that is applicable for version 2.x (although I'm not sure about that). I don't think the DBSQL.pm module exists...

As far as using <%global name%>, I'm aware of that, I just need to figure out how to call the external table from within the global. So, I guess I need to know how to create the .def file (syntax & what have you). Besides that, is it just a matter of calling it like:

my $sponsor_db = $DB->table('Sponsors');

assuming the table name is Links_Sponsors ?

Thanks,

Matt Glaspie
Quote Reply
Re: Accessing custom tables with template globals In reply to
I don't know if there is an official way, but I've always just done it by copying the resync URL:

http://yourserver.com/cgi-bin/path/to/LinkSQL/admin/admin.cgi?db=Category&do=editor_update_def

and replacing the "db=Category" with whatever the name of your database is:

db=Link_Sponsors

in this case.



PUGDOGŪ Enterprises, Inc.
FAQ:http://LinkSQL.com/FAQ
Forum:http://LinkSQL.com/forum
Quote Reply
Re: Accessing custom tables with template globals In reply to
Thanks for the help Pugdog!

BTW, for anyone else interested, there are a couple issues to get this to work. First, the correct URL should be:
http://yourserver.com/cgi-bin/path/to/LinkSQL/admin/db.cgi?db=TableName&do=editor_update_def

Also I believe LinksSQL will append the "Links_" table prefix to your table name automatically, so when you first set up your table, you should name it Links_TableName, then call it in the above URL with just "TableName".

But before you do that, this won't work unless the .def file already exists. So, first, just create a blank file and save it off to the /path/to/LinksSQL/admin/defs directory as Links_TableName.def, then run the above URL.

Thanks again, Pugdog, your input was very helpful! Smile

Matt Glaspie
Quote Reply
Re: Accessing custom tables with template globals In reply to
Ah... good points. The frame/framesets distort the URL a bit, I missed that.

But, I hope (have hoped?) that Alex would include a "create new table" option in links, that will set it up with the .def file, and other things. It makes quick hacks easier.

I think the mindset was that scripts/plugins would be creating the tables, and it's an easy function call to create .def files, and resync. The fact people might want to create simple, quick tables that use the higher-level links functions for access was over looked.

BTW... this is something that will make the FAQ, once I find a few minutes to update it. This is something that will keep coming up unless/until a "create table" button is added.

PUGDOGŪ Enterprises, Inc.
FAQ:http://LinkSQL.com/FAQ
Forum:http://LinkSQL.com/forum