Example # 1
Code: Select all
$oRecord = new ORM();
$oRecord->FirstName = 'Volo';
$oRecord->LastName = 'Mike';
$oRecord->ID = 'GENERATE_ID';
$oRecord->Unquote('ID');
$oRecord->Save('users','id = ' . $oRecord->ID);The __set() routine would also have to be smart and throw an error if one tries to set a property that is being used as a reserved class method like Save(). Although I haven't tested -- perhaps PHP handles that on its own.
or
Example # 2
Code: Select all
$oRecord = new ORM('users','id = 433');or
Example # 3
Code: Select all
$oRecord = new ORM('users', 'id = 433');
$oRecord->FirstName = 'Mike';
$oRecord->Save('users','id = 433');You could also add class methods like Remove(), Update(), Insert(), and Select() to provide even more control.
Another opportunity could be to handle multiple records like so:
Example # 4
Code: Select all
$oRecords = new ORM('users',"last_name = 'Smith'", MULTIPLE);
foreach ($oRecords as $oRecord) {
// do something with properties or class methods of $oRecord
}And here's some handy functions I wrote that convert object Property Names to/from DB Column Names, and which are used when calling __construct(), Save(), Remove(), Update(), Insert(), or Select() (-- another key point). Note these change the original value and don't return a value, saving you a step.
Code: Select all
public function MakeObjectKeyName(&$sKey) {
$sKey = strtolower($sKey);
$sKey = str_replace('fkey_','fk_',$sKey);
$sKey = str_replace('dt_','Date_',$sKey);
$sKey = str_replace('_',' ',$sKey);
$sKey = ucwords($sKey);
$sKey = str_replace('Ip ','IP ',$sKey);
$sKey = str_replace(' Ip',' IP',$sKey);
$sKey = str_replace('name','Name',$sKey); //not a bug -- I conjoin name this way usually
$sKey = str_replace(' Id',' ID',$sKey);
$sKey = str_replace('Id ','ID ',$sKey);
$sKey = str_replace(' ','',$sKey);
$sKey = ($sKey == 'Id') ? 'ID' : $sKey;
$sKey = str_replace('Fk','FK',$sKey);
}
public function MakeDBKeyName(&$sKey) {
foreach (range('A','Z') as $c) {
$sKey = str_replace($c, "_$c",$sKey);
}
$sKey = str_replace('_F_K_','_fkey_',$sKey);
$sKey = str_replace('_I_D_','_id_',$sKey);
$sKey = str_replace('_I_D','_id',$sKey);
$sKey = str_replace('_I_P_','_ip_',$sKey);
$sKey = str_replace('_I_P','_ip',$sKey);
$sKey = str_replace('_Name','name',$sKey); //not a bug -- I conjoin name this way usually
$sKey = str_replace('_Date_','_dt_',$sKey);
$sKey = str_replace('_Date','_Date',$sKey);
if (strpos($sKey, '_') == 0) {
$sKey = substr($sKey, 1);
}
$sLast = substr($sKey, -1);
if ($sLast == '_') {
$sKey = strrev($sKey);
if (strpos($sKey, '_') == 0) {
$sKey = substr($sKey, 1);
}
$sKey = strrev($sKey);
}
$sKey = strtolower($sKey);
}P.S. Also, some newbies who read this might not know you can usually convert a database record or an array into an object like so:
$oRecord = (object) array('FirstName' => 'Volo', 'LastName' => 'Mike');
and
$oRecord = (object) $rwRow;
..which might be useful in your ORM design.