« Resizing Interface | ^ Main | 1st C# App: Project Calculator »


Simple DOA with Base class

Recently I had to do a set of DOA’s (data access objects) for a project using PHP. Well, I started noticing the sql was the exact same except for the table name. One of my biggest peeves is having to rewrite the same code over and over and over. So, I quickly broke everything out of the DOA classes and created a Base.php class which contained several functions.

Initially, it just calls the initDB function to setup the connection and stores it in a class var. Now we are ready to query the db. The only function I was able to truly get 100% dynamic, for this app, was the get() which was merely a SELECT statement. The get() function was somewhat geared towards the app simply because it had a set WHERE statement but it can be easily changed to work as needed.

So, checkout the class below and you will see how I chose to save me a little time. It needs some cleaning up so hardcore PHP lovers don’t stone me. The main part I wanted to share was how you could make a 5 line class read from the database.

//AssetTypes.php
<?
class AssetTypesDB extends Base{
 var $table = "assettypes";
}
?>


//implementation of AssetTypes class
<?
include_once("AssetsTypes.php");
$types = $assetTypes->get();
print_r($types);
?>

That’s it. You can replicate the AssetTypesDB class to other classes and all you will need to do is change the $table var. For classes with more needs like INSERT/UPDATE sql I had to handwrite them but I’m sure there is a way to do the same with those. Although I highly doubt you would want to. It could add bloat and a lot of unneeded overhead to the Base.php class (btw, I HATE bloat).

One last note, you can even use the Base.php class directly instead of as an extended class. I did this several times in the project and it worked out great!

<?
include_once("Base.php");
$base = new Base();
$base->query("SELECT * FROM myTable", false);
?>

So, peep the code below. Tell me what you think.

<?
   //a few global functions
   function niceEcho($data){
       print "
 
 <pre>" . print_r($data, true) . "</pre>
 
 ";
    }

   class Base{
       var $ds = array("host"=>"",
                 "db"=>"",
                 "user"=>"",
                 "pwd"=>"",
                 "conn"=>"");
    
       function Base(){
           $this->initDB();
        }
       
       function initDB(){
           $this->ds["conn"] = mysql_connect($this->ds["host"], $this->ds["user"], $this->ds["pwd"]);
           mysql_select_db($this->ds["db"], $this->ds["conn"]);
        }
       
       //Database connectivity
          function query($sql, $toArray=true){
              $result = mysql_query($sql, $this->ds['conn']) or die('Query error: ' . mysql_error());
              if(is_bool($result) && $result==true){
                  return mysql_insert_id();
               }else{
                  if($toArray){
                      return $this->queryToArray($result);
                   }else{
                      return $result;
                   }
               }
           }
          
          function queryToArray($vars){
              $result = array();
              $count    = 0;
              while($row = mysql_fetch_array($vars, MYSQL_ASSOC)){
                  $result[$count] = $row;
                  $count++;
               }
              return $result;
           }
          
       //class specific functions
          function get($str="", $select="*"){
              $sql = "   SELECT   " . $select . " 
                    FROM      " . $this->table . "
                    WHERE   isActive=1";
              if(strlen($str) > 0){
                  $sql .= " AND " . $str;
               }
              
              return $this->query($sql);
           }
    }
?>

Posted by John C. Bland II on January 22, 2006 1:29 AM |

TrackBack

TrackBack URL for this entry:
http://mt.katapultmedia.com/mt-tb.cgi/9

Comments


hcgp wjcmbzir obwucrl xfyez yfkz wqhuo hqtrza

Posted by: dmqvwokyj pvubec | August 23, 2007 1:13 PM

Post a comment