Location: PHPKode > scripts > MySQL Model > mysql_model.class.php
<?php
	class MySQL_Model
	{
		var $table;
		var $columns;
		var $fields;
		var $id;
		var $auto;
		var $result;
		var $has_one;
		var $has_many;
		var $belongs_to_one;
		var $belongs_to_many;
		var $errors;
		
		function init($id = 0, $auto = "")
		{
			$this -> table = strtolower(get_class($this));
			$this -> columns = array();
			$this -> fields = array();
			$this -> result = array();
			$this -> has_one = array();
			$this -> belongs_to_one = array();
			$this -> errors = array();
		
			$result = mysql_query("
				show columns from `". $this -> table ."`
			");
			
			while($record = mysql_fetch_assoc($result))
			{
				$this -> columns[$record["Field"]] = array(
					"name" => $record["Field"],
					"type" => $record["Type"],
					"null" => $record["Null"],
					"key" => $record["Key"],
					"default" => $record["Default"],
					"extra" => $record["Extra"]
				);
				
				if(strpos($record["Extra"],"auto_increment") !== false)
				{
					$this -> auto = $record["Field"];
				}
			}
			
			if($auto)
			{
				$this -> auto = $auto;
			}
			
			if($id)
			{
				$this -> load($id);
			}
		}
		
		function load($id)
		{
			$this -> id = $id;
			
			$result = mysql_query("
				select
					*
				from
					`". $this -> table ."`
				where
					`". $this -> auto ."` = '". $this -> id ."'
			");
			
			$record = mysql_fetch_assoc($result);
			
			$this -> fields = $record;
		}
		
		function get($field)
		{
			if(is_array($this -> fields[$field]))
			{
				return $this -> fields[$field];
			}

			return stripslashes($this -> fields[$field]);
		}
		
		function set($field, $value)
		{
			return $this -> fields[$field] = addslashes($value);
		}
		
		function get_table()
		{
			return $this -> table;
		}
		
		function get_columns()
		{
			return $this -> columns;
		}
		
		function get_fields()
		{
			$fields = array();
			
			foreach($this -> fields as $key => $value)
			{
				$fields[$key] = $this -> get($key);
			}
			
			return $fields;
		}
		
		function valid()
		{
			$error = false;
			
			foreach($this -> fields as $key => $value)
			{
				if(!trim($value))
				{
					if($this -> columns[$key]["null"] != "YES")
					{
						$this -> error($key);
						$error = true;
					}
				}
			}
			
			if($error)
			{
				return false;
			}
			
			return true;
		}
		
		function save()
		{
			if($this -> valid())
			{
				$count = 0;
				$length = count($this -> fields) - 1;
					
				if($this -> id)
				{
					$query = "";
					$query .= " update `". $this -> table ."` set ";
					
					$count = 0;
					foreach($this -> fields as $key => $value)
					{
						$query .= " `". $key ."` = '". $value ."' ";
						
						if($count != $length)
						{
							$query .= " , ";
						}
						
						$count++;
					}
					
					$query .= " where `". $this -> auto ."` = '". $this -> id ."' ";
					
					mysql_query($query);
				}
				else
				{
					$query = "";
					$query .= " insert into `". $this -> table ."` ";
					$query .= " ( ";
					
					$count = 0;
					foreach($this -> fields as $key => $value)
					{
						$query .= " `". $key ."` ";
						
						if($count != $length)
						{
							$query .= " , ";
						}
						
						$count++;
					}
					
					$query .= " ) values ( ";
					
					$count = 0;
					foreach($this -> fields as $key => $value)
					{
						$query .= " '". $value ."' ";
						
						if($count != $length)
						{
							$query .= " , ";
						}
						
						$count++;
					}
					
					$query .= " ) ";
					
					mysql_query($query);
					
					$this -> load(mysql_insert_id());	
				}
				
				return true;
			}

			return false;
		}
		
		function remove($where = "", $limit = "")
		{
			if(!$where)
			{
				$where = "where `". $this -> auto ."` = '". $this -> id ."'";
			}
			
			if(!$limit)
			{
				$limit = "limit 1";
			}
			
			mysql_query("
				delete from 
					`". $this -> table ."`
				$where
				$limit
			");
		}
		
		function has_one($table, $key)
		{
			// Cache for find().
			$this -> has_one = array(
				"table" => $table,
				"key" => $key
			);
			
			if(count($this -> fields) > 0)
			{
				$this -> fields[$table] = array();
				
				$result = mysql_query("
					select
						*
					from
						`". $table . "`
					where
						`". $key ."` = '". $this -> id ."'
				");
								
				$this -> fields[$table] = mysql_fetch_assoc($result);
			}
		}
		
		function belongs_to_one($table, $foreign_key, $key)
		{
			// Cache for find().
			$this -> belongs_to_one = array(
				"table" => $table,
				"foreign_key" => $foreign_key,
				"key" => $key
			);
			
			if(count($this -> fields) > 0)
			{
				$this -> fields[$table] = array();
				
				$result = mysql_query("
					select
						*
					from
						`". $table . "`
					where
						`". $foreign_key ."` = '". $this -> get($key) ." '
				");
								
				$this -> fields[$table] = mysql_fetch_assoc($result);
			}
		}
		
		function has_many($table, $key)
		{
			if(count($this -> fields) > 0)
			{
				$this -> fields[$table] = array();
				
				$result = mysql_query("
					select
						*
					from
						`". $table . "`
					where
						`". $key ."` = '". $this -> id ."'
				");
				
				while($record = mysql_fetch_assoc($result))
				{
					$this -> fields[$table][] = $record;
				}
			}
		}
		
		function belongs_to_many($table, $foreign_key, $key)
		{
			if(count($this -> fields) > 0)
			{
				$this -> fields[$table] = array();
				
				$result = mysql_query("
					select
						*
					from
						`". $table . "`
					where
						`". $foreign_key ."` = '". $this -> get($key) ."'
				");
								
				while($record = mysql_fetch_assoc($result))
				{
					$this -> fields[$table][] = $record;
				}
			}
		}
		
		function find($where = "", $order = "", $limit = "")
		{		
			$result = mysql_query("
				select
					*
				from
					`". $this -> table ."`
				$where
				$order
				$limit
			");
			
			while($record = mysql_fetch_assoc($result))
			{
				if(count($this -> has_one) > 0)
				{
					$has = mysql_query("
						select
							*
						from
							`". $this -> has_one["table"] . "`
						where
							`". $this -> has_one["key"] ."` 
								= 
							'". $record[$this -> auto] ." '
					");
									
					$record[$this -> has_one["table"]] = mysql_fetch_assoc($has);
				}
				
				if(count($this -> belongs_to_one) > 0)
				{
					$belongs = mysql_query("
						select
							*
						from
							`". $this -> belongs_to_one["table"] . "`
						where
							`". $this -> belongs_to_one["foreign_key"] ."` 
								= 
							'". $record[$this -> belongs_to_one["key"]] ." '
					");
									
					$record[$this -> belongs_to_one["table"]] = mysql_fetch_assoc($belongs);
				}
			
				$this -> result[] = $record;
			}
			
			return $this -> result;
		}
		
		function error($field = "")
		{
			if($field)
			{
				$this -> error[$field] = true;
			}
			else
			{
				return $this -> error;
			}
		}
	}
?>
Return current item: MySQL Model