Wikipedia para siempre
Permalink: http://www.treeweb.es/u/1182/ 20/01/2012

Version 0.1

StubORM

StubORM es un stub que simula un ORM. Esta clase implementa el patrón multiton para almacenar distintas instancias a tablas de un ORM.
<?php class StubORM { private $id = ''; private static $tables = array(); private function __construct($id) { $this->id = $id; } public function SELECT() { return new Query($this->id); } public static function &getTable($table) { if (!isset(self::$tables[$table])) self::$tables[$table] = new StubORM($table); return self::$tables[$table]; } } ?>

La clase Query

Esta clase es la interfaz SQL propiamente. Cada objeto query necesita un identificador para ser inicializado.
<?php class Query { protected $id = ''; protected $where = array(); protected $orderby = array(); protected $in = array(); public function __construct($id) { $this->id=$id; } public function &WHERE($clause) { $this->where[] = $clause; return $this; } public function &ORDERBY($clause, $direction='ASC') { $this->orderby[] = $clause.' '.$direction; return $this; } public function &IN($field, Query &$query) { $this->in[] = array( 'field'=>$field, 'query'=>$query ); return $this; } public function END() { $string = 'SELECT * FROM `'.$this->id.'` '; foreach ($this->in as $I=>$i) { $subquery = $i['query']; $substring = 'SELECT `id` FROM `'.$subquery->id.'` '; if (count($subquery->where)) $substring .= ' WHERE ('.implode(' ) OR (',$subquery->where).') '; $this->where[] = $i['field'].' IN ('.$substring.') '; } if (count($this->where)) $string .= ' WHERE ('.implode(' ) OR (',$this->where).') '; if (count($this->orderby)) $string .= ' ORDER BY '.implode(', ', $this->orderby); return $string; } } ?>