I am creating a class (Datamap) that will help me simplify my queries in my model class. Basically, this class (Datamap) is use in conjunction with my db class.
Previously for an insert statement in my model I need.
$db->query("INSERT INTO test(id,name) VALUES("id","name")");
Now, with the Datamap class, it will be like
$datamap->create("test","id=id&name=name");
Below is snippet code from the Datamap class that I created.
function create($tablename,$variables){
$id=0;
$tbl = $this->creturnval($variables);
$tblVal = $tbl[0];
$tblData = $tbl[1];
$this->db->execute("INSERT into $tablename($tblVal) VALUES($tblData);");
$id = $this->db->lastInsertedId();
return $id;
}
function creturnval($variables){
$vars = explode("&",$variables);
$count = sizeOf($vars);
$tblVal = "";
$tblData = "";
for($i=0;$i<$count;$i++){
$d = explode("=",$vars[$i]);
$tblVal.= $d[0].",";
$tblData.= "'".$d[1]."'".",";
}
$tblVal = rtrim($tblVal, ',');
$tblData = rtrim($tblData, ',');
$return[0] = $tblVal;
$return[1] = $tblData;
return $return;
}
My concern is actually the performance issue. Previously in my model class, if I just used $db->query(), it seems to be faster because I do not have to go through 2 functions.
Now, in order to do an insert, I will need to parse the variables, split & etc, then process it.
My idea of using this Datamap class is actually to provide more maintainability and standardization. This is to prevent all my model classes from having sql statements over the place which looks untidy.
Do you think this will pose a possible performance issue?
PS: I am using my own framework that I build for my web application
Appreciate all advice.
I think a more common way of writting SQL wrappers is like this:
$datamap->create();return an object that will hold the values for a record.setValueandgetValueallow you to access it.updatepushes new values to the database. Similarly, you could implement$datamap->get("test", 1234);where1234is the id of the record you want to fetch.getreturns the same object ascreate, with the same functions.updatewill then cause anUPDATEinstead of anINSERT. After inserting a record usingupdateyou can usemysql_last_insert_id()to fetch the id and set it in the object so you can performUPDATEs on it after that point. (The object has to keep track of it’s ‘state’).