rendered paste body<?php
abstract class Object
{
/**
* The table to read from
*
* @var String
*/
protected $tablename;
/**
* The default values of the row
*
* @var Array<Varchar>
*/
protected $values;
/**
* Constructor. Can take an associative array or integer id value
*
* @param Integer/Array $id
* @return Object
*/
public function Object($id)
{
Debug::out(0, "Constructor for Object($id)");
if (is_integer($id))
return $this->load(Database::selectItem($this->tablename, $id));
if (is_array($id))
$this->load($id);
$array = Database::select($this->tablename, $id);
return $this->load($array[0]);
}
/**
* Returns number of rows
*
* @return Integer
*/
public static function count($tablename, $where)
{
if (!$where) $where = '(1)';
return Database::selectCount($tablename, $where);
}
/**
* Loads values into memory
*
* @param Array $array
*/
protected function load($array)
{
if (!$array) return;
foreach ($array as $key => $value)
$this->$key = $value;
$this->values = $array;
}
/**
* Returns an array of objects
*
* @param String $object
* @param String $where
*
* @return Array<Object>
*/
public static function get($object, $where)
{
$objects = array();
$rows = Database::select(strtolower($object), $where);
for ($i = 0; $i < sizeof($rows); $i++)
{
$objects[$i] = new $object($rows[$i]);
}
return $objects;
}
/**
* Returns whether the object is initialized
*
* @return Boolean
*/
public function isInitialized()
{
return $this->id > 0;
}
/**
* Creates a new object and returns it.
*
* @param Array<Varchar> $array
*
* @return Object
*/
public static function create($tablename, $array)
{
return new $tablename(Database::insert($tablename, $array));
}
/**
* Updates the recordset.
*
* @return Boolean
*/
public function save()
{
foreach ($this->values as $key => $value) {
if ($this->$key != $value)
$params[$key] = $this->$key;
}
return Database::update($this->tablename, $this->id, $params);
}
/**
* Convert to a string (JSON-style)
*
* @return String
*/
public function toString()
{
$json = new Json();
foreach ($this->values as $key => $value) {
$json->addParam($key, $value);
}
return $json->toString();
}
public function __toString()
{
return $this->toString();
}
}
?>