I had already built my own and sincerely i never followed any design patter or quite remembered the difference between them all. I'd actually need a good kick in the arse for that portion to make sure i don't code something that is useless... Since you seem to know A LOT about these, mind giving me a few hints as to how you'd see a good ORM mapped to this tech?
I will obviously take my own, for example, pluqv1 (or psql1) ORM code looked like this: (It was very lame but it served it's purpose)
Code: Select all
if(!defined('CLASSDEF_car')){
define('CLASSDEF_car', true);
class car {
public static function getAllCars($orderfields = array(), $limit = 0, $limitfrom = 0){
$sql = 'SELECT * FROM `racingfever`.`cars` ';
if(isset($orderfields) && is_array($orderfields) && count($orderfields) > 0){
$orderfieldconcat='';
$sql .= 'ORDER BY ';
foreach($orderfields as $orderfielddata){
$sql .= $orderfieldconcat.$orderfielddata['name'];
$orderfieldconcat=', ';
}
}
if(isset($limit) && is_numeric($limit) && $limit > 0){
$sql .= ' LIMIT '.$limit;
if(isset($limitfrom) && is_numeric($limitfrom) && $limitfrom > 0){
$sql .= ', '.$limitfrom;
}
}
$datatable = mysql_query($sql);
$resultingobjects = array();
while($datarow = mysql_fetch_assoc($datatable)){
if(class_exists("carex")){ $newcar = new carex($datarow); }else{ $newcar = new car($datarow); }
$resultingobjects[] = $newcar;
}
return $resultingobjects;
}
public static function getCarById($id){
if(is_null($id) == true || is_numeric($id) == false){ throw new exception('$id is not a valid NUMBER value'); }
$sql = 'SELECT * FROM `racingfever`.`cars` WHERE id = "'.mysql_escape_string($id).'" ';
if(isset($orderfields) && is_array($orderfields) && count($orderfields) > 0){
$orderfieldconcat='';
$sql .= 'ORDER BY ';
foreach($orderfields as $orderfielddata){
$sql .= $orderfieldconcat.$orderfielddata['name'];
$orderfieldconcat=', ';
}
}
if(isset($limit) && is_numeric($limit) && $limit > 0){
$sql .= ' LIMIT '.$limit;
if(isset($limitfrom) && is_numeric($limitfrom) && $limitfrom > 0){
$sql .= ', '.$limitfrom;
}
}
$datatable = mysql_query($sql);
$resultingobjects = array();
while($datarow = mysql_fetch_assoc($datatable)){
if(class_exists("carex")){ $newcar = new carex($datarow); }else{ $newcar = new car($datarow); }
$resultingobjects[] = $newcar;
}
return $resultingobjects[0];
}
protected $data = array();
protected $newrow = true;
protected $deletedrow = true;
public function __construct($data = NULL){
if(is_array($data) == false || is_null($data)){
$this->newrow();
}else{
$this->data = $data;
$this->newrow = false;
$this->deletedrow = false;
}
}
public function newrow(){
$this->data = array(
'id' => 0,
'racerprofileid' => 0,
'basecarid' => 0,
'bought' => date('Y-m-d G:i:s', time()),
'worth' => 0,
'onsale' => 0,
'onsalesince' => '0000-00-00 00:00:00',
'onsaleprice' => 0,
'make' => '',
'model' => '',
'collection' => '',
'instance' => 0,
'year' => '',
'imagefull' => '',
'imagesmall' => '',
'imagethumb' => '',
'basebhp' => 0,
'incbhp' => 0,
'totbhp' => 0,
'baseacl' => 0,
'incacl' => 0,
'totacl' => 0,
'basehdl' => 0,
'inchdl' => 0,
'tothdl' => 0,
'basetot' => 0,
'inctot' => 0,
'tot' => 0,
'curexhaust' => 0,
'curbrake' => 0,
'curtune' => 0,
'curpolish' => 0,
'curbalance' => 0,
'curdispl' => 0,
'curcomp' => 0,
'curnos' => 0,
'curtrans' => 0,
'curclutch' => 0,
'curflywheel' => 0,
'curturbine' => 0,
'curcooler' => 0,
'cursusp' => 0,
'curtires' => 0,
'curreduction' => 0,
'curbrakecontrol' => 0,
'odometer' => 0,
);
$this->deletedrow = false;
$this->newrow = true;
}
public function deleterow(){
$this->deletedrow = true;
}
public function save_changes(){
if($this->newrow == true && $this->deletedrow == true){
$this->newrow();
}elseif($this->newrow == true){
mysql_query('INSERT INTO `racingfever`.`cars`(`racerprofileid`, `basecarid`, `bought`, `worth`, `onsale`, `onsalesince`, `onsaleprice`, `make`, `model`, `collection`, `instance`, `year`, `imagefull`, `imagesmall`, `imagethumb`, `basebhp`, `incbhp`, `totbhp`, `baseacl`, `incacl`, `totacl`, `basehdl`, `inchdl`, `tothdl`, `basetot`, `inctot`, `tot`, `curexhaust`, `curbrake`, `curtune`, `curpolish`, `curbalance`, `curdispl`, `curcomp`, `curnos`, `curtrans`, `curclutch`, `curflywheel`, `curturbine`, `curcooler`, `cursusp`, `curtires`, `curreduction`, `curbrakecontrol`, `odometer`)VALUES('.$this->data['racerprofileid'].', '.$this->data['basecarid'].', "'.$this->data['bought'].'", '.$this->data['worth'].', '.$this->data['onsale'].', "'.$this->data['onsalesince'].'", '.$this->data['onsaleprice'].', "'.mysql_escape_string($this->data['make']).'", "'.mysql_escape_string($this->data['model']).'", "'.mysql_escape_string($this->data['collection']).'", '.$this->data['instance'].', "'.mysql_escape_string($this->data['year']).'", "'.mysql_escape_string($this->data['imagefull']).'", "'.mysql_escape_string($this->data['imagesmall']).'", "'.mysql_escape_string($this->data['imagethumb']).'", '.$this->data['basebhp'].', '.$this->data['incbhp'].', '.$this->data['totbhp'].', '.$this->data['baseacl'].', '.$this->data['incacl'].', '.$this->data['totacl'].', '.$this->data['basehdl'].', '.$this->data['inchdl'].', '.$this->data['tothdl'].', '.$this->data['basetot'].', '.$this->data['inctot'].', '.$this->data['tot'].', '.$this->data['curexhaust'].', '.$this->data['curbrake'].', '.$this->data['curtune'].', '.$this->data['curpolish'].', '.$this->data['curbalance'].', '.$this->data['curdispl'].', '.$this->data['curcomp'].', '.$this->data['curnos'].', '.$this->data['curtrans'].', '.$this->data['curclutch'].', '.$this->data['curflywheel'].', '.$this->data['curturbine'].', '.$this->data['curcooler'].', '.$this->data['cursusp'].', '.$this->data['curtires'].', '.$this->data['curreduction'].', '.$this->data['curbrakecontrol'].', '.$this->data['odometer'].')');
$this->data['id'] = mysql_insert_id();
$this->newrow = false;
$this->deletedrow = false;
}elseif($this->deletedrow == true){
mysql_query('DELETE FROM `racingfever`.`cars` WHERE id = '.$this->data['id'].'');
$this->newrow();
}else{
mysql_query('UPDATE `racingfever`.`cars` SET racerprofileid = '.$this->data['racerprofileid'].', basecarid = '.$this->data['basecarid'].', bought = "'.$this->data['bought'].'", worth = '.$this->data['worth'].', onsale = '.$this->data['onsale'].', onsalesince = "'.$this->data['onsalesince'].'", onsaleprice = '.$this->data['onsaleprice'].', make = "'.mysql_escape_string($this->data['make']).'", model = "'.mysql_escape_string($this->data['model']).'", collection = "'.mysql_escape_string($this->data['collection']).'", instance = '.$this->data['instance'].', year = "'.mysql_escape_string($this->data['year']).'", imagefull = "'.mysql_escape_string($this->data['imagefull']).'", imagesmall = "'.mysql_escape_string($this->data['imagesmall']).'", imagethumb = "'.mysql_escape_string($this->data['imagethumb']).'", basebhp = '.$this->data['basebhp'].', incbhp = '.$this->data['incbhp'].', totbhp = '.$this->data['totbhp'].', baseacl = '.$this->data['baseacl'].', incacl = '.$this->data['incacl'].', totacl = '.$this->data['totacl'].', basehdl = '.$this->data['basehdl'].', inchdl = '.$this->data['inchdl'].', tothdl = '.$this->data['tothdl'].', basetot = '.$this->data['basetot'].', inctot = '.$this->data['inctot'].', tot = '.$this->data['tot'].', curexhaust = '.$this->data['curexhaust'].', curbrake = '.$this->data['curbrake'].', curtune = '.$this->data['curtune'].', curpolish = '.$this->data['curpolish'].', curbalance = '.$this->data['curbalance'].', curdispl = '.$this->data['curdispl'].', curcomp = '.$this->data['curcomp'].', curnos = '.$this->data['curnos'].', curtrans = '.$this->data['curtrans'].', curclutch = '.$this->data['curclutch'].', curflywheel = '.$this->data['curflywheel'].', curturbine = '.$this->data['curturbine'].', curcooler = '.$this->data['curcooler'].', cursusp = '.$this->data['cursusp'].', curtires = '.$this->data['curtires'].', curreduction = '.$this->data['curreduction'].', curbrakecontrol = '.$this->data['curbrakecontrol'].', odometer = '.$this->data['odometer'].' WHERE id = '.$this->data['id'].'');
}
}
public function basecar(){
return single(pluq(from(inres(mysql_query('SELECT * FROM basecars WHERE id = "'.$this->basecarid.'"'), 'basecar'))));
}
public function organisedraceevents(){
return pluq(from(inres(mysql_query('SELECT * FROM organisedraceevents WHERE carid = "'.$this->id.'"'), 'organisedraceevent')));
}
public function organisedraceparticipants(){
return pluq(from(inres(mysql_query('SELECT * FROM organisedraceparticipants WHERE carid = "'.$this->id.'"'), 'organisedraceparticipant')));
}
public function racerprofile(){
return single(pluq(from(inres(mysql_query('SELECT * FROM racerprofiles WHERE id = "'.$this->racerprofileid.'"'), 'racerprofile'))));
}
public function __get($nm){ return $this->data[$nm]; }
public function __set($nm, $val){ $this->data[$nm] = $val; }
public function __isset($nm){ return isset($this->data[$nm]); }
}
}
Not really elegent but it worked very well and is in use in a production environment namely a facebook game called Racing Fever. This version never had any type checking, i had to do everything by myself, but since there was very little input by the user it was quite simple.