I am wondering what are the advantages of having a database abstraction layer like the one explained Here (in Devshed)
Don't you get more efficient queries writing them down in SQL?
As far as i can understand, i just see an advantage whenever the underlying database were to change (and even then, SQL isn't the same on each RDBMS?)
I actually wrap my queries into methods like this:
Expand|Select|Wrap|Line Numbers
- public function getUserBm()
- {
- $sql =<<<_SQL
- SELECT * FROM bm LEFT JOIN bm_sections
- ON bm.section_id = bm_sections.id
- WHERE user_id = '$this->_userId'
- _SQL;
- $result = $this->_db->query($sql);
- if($this->_db->numRows($result) > 0){
- while($row = $this->_db->fetchArray($result)){
- foreach($row as $key => $val){
- $dataArray[][$key] = $val;
- }
- }
- return $dataArray;
- }
- }
-I make a class to hide the RDBMS specific functions
-then i instantiate that class into a property of another class that will use the abstracted functions. (here: $this->_db)
-i try to create a layer that keeps SQL code separate from the objects that use the information fetched from db. (the code above is from one of the classes that are used for separating SQL from rest)
is this a very bad practice?
If someone can bring light on this topic, i would be very greatful.
Thank you very much
bilibytes