A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearWhat's the best way to abstract the database from a PHP application? - page 1
User InfoPosts
What's the best way to abstract the database from a PHP application?#1
My question is how does one abstract a database connection from the model layer of an application? The primary concern is to be able to easily change from different types of databases. Maybe you start with a flat file, comma-delimited database. Then you want to move to a SQL database. Then later you decide an LDAP implementation would be better. How can a person easily plan for something like this?

For a simple example, let(s say you have a user with a first name, last name, and email. A very simple PHP class representing it might look like this (please ignore the problems with public instance variables):

<?php

class User {
public $first;
public $last;
public $email;
}

?>


I have frequently seen where people have a DAO class which has the SQL embedded in it as such:

<?php

class UserDAO {
public $id;
public $fist;
public $last;
public $email;

public function create( &$db ) {
$sql = "INSERT INTO user VALUES( ($first(, ($last(, ($email( )";
$db->query( $sql );
}
}

?>


My problem with strategies like this is when you want to change your database, you have to change every DAO class( create, update, load, delete functions to deal with your new type of database. Even if you have a program to auto-generate them for you (which I am not particularly a fan of), you would have to edit this program to make it work now.

What are your suggestions for how to handle this?

My current idea is to create a super class for DAO objects with its own create, delete, update, load functions. However, these functions would take arrays of the attributes of the DAO and generate the query itself. In this manner, the only SQL is in the SuperDAO class rather than being scattered about several classes. Then if you wanted to change your database layer, you would only have to change how the SuperDAO class generates the queries. Advantages? Disadvantages? Foreseeable problems? The good, the bad, and the ugly?

posted date: 2009-04-15 11:05:00


Re: What's the best way to abstract the database from a PHP application?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-15 11:05:01


Re: What's the best way to abstract the database from a PHP application?#3
It sounds good in theory but in all likelyhood YAGNI.You would be better off using an SQL library such as PDO and not worrying about LDAP until you get there.

posted date: 2009-04-15 11:10:00


Re: What's the best way to abstract the database from a PHP application?#4
You can use various frameworks such as PDO, PEAR::MDB2 or Zend_Db, but to be honest in 12 years of PHP development, I(ve never had to transition from one type of data storage infrastructure to another.Its exceedingly rare to even go from something quite similar like Sqlite, to MySQL. If you did do more than that, you(d have far larger problems anyway.

posted date: 2009-04-15 11:11:00


Re: What's the best way to abstract the database from a PHP application?#5
This is a common maintenance problem and will most likely be needed eventually. Database schemas are rarely set in stone.

posted date: 2009-04-15 11:12:00


Re: What's the best way to abstract the database from a PHP application?#6
He isn't abstracting the DB away in his example. Just accessing it through precreated functions.

posted date: 2009-04-15 11:13:00


Re: What's the best way to abstract the database from a PHP application?#7
He's not talking about the schema, he's talking about seamlessly switching between CSV and relational databases. How often do you do that?

posted date: 2009-04-15 11:14:00


Re: What's the best way to abstract the database from a PHP application?#8
Or LDAP or any new type of database that gets invented. I wouldn't be asking the question if I didn't have a need.

posted date: 2009-04-15 11:15:00


Re: What's the best way to abstract the database from a PHP application?#9
Even changing database engine is rare (say, from MySQL to Oracle) but that could be taken care of with a database abstraction layer such as PEAR::MDB2 (though maybe there is a CSV driver for that, or one could be written...)

posted date: 2009-04-15 11:16:00


Re: What's the best way to abstract the database from a PHP application?#10
Generally speaking, if you(re going to the trouble of using a database then your application will benefit by using features specific to a "brand" of database, and will be a more solid app for it.It is very rare to move from one database system to another. The only time you might realistically consider that aa feature worth implementing is if you(re writing some kind of loosely coupled system or framework intended for mass consumption (like Zend Framework, or Django).

posted date: 2009-04-15 11:20:00


Re: What's the best way to abstract the database from a PHP application?#11
The best way is to use an ORM (Object-relational mapping) library. There are plenty of them for PHP. I(ve personally used and can recommend doctrine orm (I(ve used it in combination with silex, which is a minimalistic php framework). Here is an StackOverflow thread about PHP ORMs where you can find some alternatives if you like: Good PHP ORM Library?

posted date: 2009-04-15 11:21:00


Re: What's the best way to abstract the database from a PHP application?#12
I always liked using ADOdb. From what I(ve seen, it looks like it(s capable of switching between vastly different platforms.http://adodb.sf.net

posted date: 2009-04-15 13:37:00


select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.