Location: PHPKode > projects > Todo Manager > todo/classes/activerecord.class.php
<?php
require_once $_SERVER["DOCUMENT_ROOT"]."/".FOLDER."/library/util.lib.php";	
require_once $_SERVER["DOCUMENT_ROOT"]."/".FOLDER."/classes/database.class.php";

class ActiveRecord {
	var $db;
	var $table_name;
	var $id;
	var $fields;
	
	function ActiveRecord($db, $table_name, $id = NULL) {
		$this->db = $db;
		$this->table_name = $table_name;
		$this->id = $id;
		$this->retrieveFields();
		if (!util_empty($id))
			$this->refresh();
	}
	
	function get($name) {
		return $this->fields[$name];
	}
	
	function set($name, $value) {
		$this->fields[$name] = $value;
	}
	
	function retrieveFields() {
		$this->fields = array();
		$result = $this->db->getResult("show columns from ".$this->db->quotename($this->table_name));
		while ($row = $this->db->getRow($result))
			$this->fields[$row["Field"]] = NULL;
	}
	
	function delete() {
		$sql = "delete from ".$this->db->quotename($this->table_name)." where id = ".$this->db->quote($this->id);
		$this->db->execute($sql);
		$this->id = NULL;
	}
	
	function post() {
		$this->beforePost();
		if (util_empty($this->id)) {
			// insert a new record
			$sql = "insert into ".$this->db->quotename($this->table_name)."(\n";
			
			$str = NULL;
			foreach ($this->fields as $key => $value) {
				if ($key == "id")
					continue;
				if (!util_empty($str))
					$str .= ",\n";
				$str .= "\t".$this->db->quotename($key);
			}
			$sql .= $str."\n) values (\n";
			
			$str = NULL;
			foreach ($this->fields as $key => $value) {
				if ($key == "id")
					continue;
				
				if (!util_empty($str))
					$str .= ",\n";
				if (($key == "created_on") || ($key == "updated_on"))
					$str .= "\tcurrent_timestamp()";
				else
					$str .= "\t".$this->db->quote($value);
			}
			$sql .= $str."\n)";
			
			$this->beforeInsert();
			$this->db->execute($sql);
			$this->afterInsert();
			
			// retrieve the inserted row
			$this->id = $this->db->getInsertId();
			$this->refresh();
		} else {
			// update the current record
			$sql = "update ".$this->db->quotename($this->table_name)." set\n";
			
			$str = NULL;
			foreach ($this->fields as $key => $value) {
				if (($key == "id") || ($key == "created_on"))
					continue;
				if (!util_empty($str))
					$str .= ",\n";
				if ($key == "updated_on")
					$str .= "\t".$this->db->quotename($key)." = current_timestamp()";
				else
					$str .= "\t".$this->db->quotename($key)." = ".$this->db->quote($value);
			}
			$sql .= $str."\nwhere id = ".$this->db->quote($this->id);
			
			$this->beforeUpdate();
			$this->db->execute($sql);
			$this->afterUpdate();
			
			// reload the row
			$this->refresh();
		}
		$this->afterPost();
	}
	
	function refresh() {
		$sql = "
		select *
		from ".$this->db->quotename($this->table_name)."
		where id = ".$this->db->quote($this->id);
		$result = $this->db->getResult($sql);
		$row = $this->db->getRow($result);
		foreach($this->fields as $key => $value)
			$this->set($key, $row[$key]);
	}
	
	// override this function in the extended class
	function beforeDelete() {}
	
	// override this function in the extended class
	function afterDelete() {}
	
	// override this function in the extended class
	function beforeInsert() {}
	
	// override this function in the extended class
	function afterInsert() {}
	
	// override this function in the extended class
	function beforeUpdate() {}
	
	// override this function in the extended class
	function afterUpdate() {}
	
	// override this function in the extended class
	function beforePost() {}
	
	// override this function in the extended class
	function afterPost() {}
}
?>
Return current item: Todo Manager