All pastes #352052 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-02-12 15:39 UTC
rendered paste body
<?php

class InventoryItem {
	protected $vars, $orig_vars;
	protected $is_new = true;
	protected $skip_columns;

	private function __construct () {
		$this->skip_columns[]="inventory_uid";
	}

	public function getInstance () {
		static $object;
		if (!$object) {
			$object = new self ();
		}
		return $object;
	}

	public function setVars ($varset) {
		foreach ($varset as $var => $value) {
			$this->vars[$var]=$value;
		}
		return true;
	}

	public function getAllVars () {
		return $this->vars;
	}

	public function load ($inventory_uid) {
		$querystr="
			SELECT *
			FROM inventory
			WHERE inventory_uid='".$inventory_uid."'";
		$db=DB::getInstance ();
		if (!$db->query ($querystr))
			die ("Failed to query inventory item");
		$this->vars=$db->fetchRow ();
		$this->orig_vars=$this->vars;
		$this->is_new=false;
		return true;
	}

	public function update () {
		// check to see if were a new inventory item (not loaded from the database)
		if ($this->is_new) {
			// Yep, ok, then insert all the data.
			$querystr="
				INSERT INTO inventory SET (";
			$values="
				VALUES (";

			// Loop over all the vars and pupulate the $querystr and $values strings.
			$some=false;
			foreach ($this->vars as $var => $value) {
				//  skip columns that we dont want to insert/update in the db (let the db do the work).
				if (in_array ($var, $this->skip_columns)) continue;
				// If we have some columns already on there, then we need to add a ','
				if ($some) {
					$querystr.=", ";
					$values.=", ";
				}
				// add the $var (or column name) to the $querystr
				$querystr.=$var;
				$values.="'".$value."'";
				$some=true;
			}
			// close and combine the strings
			$querystr.=") ".$values.")";

			// send query to the db to insert the data.
			$db=DB::getInstance ();
			if (!$db->query ($querystr))
				die ("Failed to insert the inventory item.");

			// now we need to get the ID back from the db that it just created for that row
			$querystr="SELECT LAST_INSERT_ID() AS inventory_uid";
			if (!$db->query ($querystr))
				die ("Failed to get the last inventory item uid.");
			$row=$db->fetchRow ();
			$this->vars['inventory_uid']=$row['inventory_uid'];

			// mirror the vars onto the orig_vars
			$this->orig_vars=$this->vars;
			// mark this no longer new
			$this->is_new=false;
			return true;
		}

		// not new, so we need to get the differences between the vars we loaded and the vars we changed
		$diff=array_difference_assoc ($this->orig_vars, $this->vars);
		// if there are no differences, then theres nothing to do.
		if (!$diff || !count ($diff)) return true;

		// start the update
		$querystr="
			UPDATE inventory
			SET ";

		// loop over the changed vars and add them to the update query
		$some=false;
		foreach ($diff as $var => $value) {
			// skip columns we dont want to send.
			if (in_array ($var, $this->skip_columns)) continue;
			// if we appended some then we need to seperate by a ','
			if ($some) $querystr.=", ";
			// append var (column name) and value
			$querystr.=$var."='".$value."'";
			$some=true;
		}

		// specify what row were updating
		$querystr.="WHERE inventory_uid='".$this->vars['inventory_uid']."'";

		// send query to the db
		$db=DB::getInstance ();
		if (!$db->query ($querystr))
			die ("Failed to update user.");

		// mirror the vars onto the orig_vars for later updates
		$this->orig_vars=$this->vars;
		return true;
	}
}

?>