We don't bite newbies here... much | |
PerlMonks |
Re: Handling near-redundant SQL queriesby blokhead (Monsignor) |
on Sep 01, 2004 at 01:08 UTC ( [id://387367]=note: print w/replies, xml ) | Need Help?? |
I wrote Class::Tables just for this kind of situation. You have a schema sitting there in the database, and you want a nice interface with minimal effort. In this case, the schema might need a minor change -- unless you're using SQLite, you'll also need an 'id' column for each of the many-to-many mapping tables. Apart from that little change, the module will automatically do the right thing here... Even match up the singular "company_id" column with the plural "companies" table, a feature I'm quite proud of. All it takes is 2 extra lines of code, and you have the proper object persistence classes available. Check out the pod.
The newest development version also supports many-to-many accessors directly (thanks to contributions from elusion) which your schema will greatly benefit from. If you need more complex functionality, Class::DBI is the way to go, so check them both out to see which is a better fit for your goals. Either way, with object-persistence modules like this, you can perform a query and get back a collection of objects. Then you should really be sending the results to an HTML templating system. It should be the current template that decides what fields from the objects should be shown (phone number, etc). The core application logic shouldn't have to be burdened with that. Certainly you wouldn't want a separate function in your code for each possible combination of columns to fetch/show. blokhead
In Section
Seekers of Perl Wisdom
|
|