YADAL - Yet Another Database Abstraction Layer

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: YADAL - Yet Another Database Abstraction Layer

Post by alex.barylski »

I did something similar a while back but used PDO as the database 'access' layer

A database 'abstraction' layer (would be IMHO) an API that abstracted you from any particular DBE, such as the differences between MSSQL and MySQL when it comes to SELECT and so forth.

Nice work - always interested in seeing how others skin the cat :)

EDIT | http://code.google.com/p/pdowrapper/

Cheers,
Alex
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: YADAL - Yet Another Database Abstraction Layer

Post by Christopher »

pickle wrote:It is quite useful actually. Say, for example, I've got a calendar of events, with each event being it's own object. Each Event object needs to access the database to retrieve it's information. With a singleton DAL, each unique Event object uses the same database connection, rather than making one connection for each object.
But because it only allows you to do one connection per class, I think it is better to use another mechanism -- like Corbyn's registry -- to achieve this functionality.
(#10850)
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: YADAL - Yet Another Database Abstraction Layer

Post by thinsoldier »

But in relation to the original post:

Code: Select all

else if(defined('CALLING_WEBAPP_NAME'))
      {
          global $SESSION_GLOBALS;
          $host = (isset($SESSION_GLOBALS[CALLING_WEBAPP_NAME]['host'])) ? $SESSION_GLOBALS[CALLING_WEBAPP_NAME]['host'] : 'localhost';
          $username = $SESSION_GLOBALS[CALLING_WEBAPP_NAME]['user'];
          $password = $SESSION_GLOBALS[CALLING_WEBAPP_NAME]['password'];
          $db = $SESSION_GLOBALS[CALLING_WEBAPP_NAME]['db'];
      }
I've done something like this before.
We call ours $CONFIG instead of $SESSION_GLOBALS.

What I did once was to have my constructor do nothing.
Then immediately call whatever startup function I wanted.

Code: Select all

$obj = new tooMuchStuffInOneClass();
$obj->setupFromCONFIG($CONFIG); // sometimes there'll be a redefined CONFIG in the current file
// $obj->setupFromCONFIG(); // this will global the original CONFIG set in the central include file
// or normal usage
// $obj->construct('tubetop','salami','bodyswing');
Warning: I have no idea what I'm talking about.
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: YADAL - Yet Another Database Abstraction Layer

Post by thinsoldier »

pickle wrote:It is quite useful actually. Say, for example, I've got a calendar of events, with each event being it's own object. Each Event object needs to access the database to retrieve it's information. With a singleton DAL, each unique Event object uses the same database connection, rather than making one connection for each object.
What if the Events area of your site is to allow Events to belong to (multiple?) event categories. So you have a categories table and another table that shows which event records belong to which event categories. How do you approach that?

And is there a common naming convention for these in-between tables that link one table to another?
Warning: I have no idea what I'm talking about.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: YADAL - Yet Another Database Abstraction Layer

Post by pickle »

thinsoldier wrote:What if the Events area of your site is to allow Events to belong to (multiple?) event categories. So you have a categories table and another table that shows which event records belong to which event categories. How do you approach that?

And is there a common naming convention for these in-between tables that link one table to another?
I think that's getting into the realm of database design. How you design the database & queries is separate from these classes.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply