Location: PHPKode > projects > ArasPhp Framework > ArasPhp-0.03/core/helpers/Query.php
<?php

/**
 * Query helper
 * It's goal it's to make an abstract-query layer able to return models(objects)
 * It handles the queries history as well
 */
 
 class Query {
 
 
 	/**
 	* Query vars
 	*/
 	
 	var $useModel;		// Model to create
 	var $useTable;		// Table to query
 	var $objects = NULL;	// To be returned
 	
 	
 	/**
 	* Set up query
 	*/
 	private function setUp($model)
 	{
 		// Model to use
 		$this->useModel = ucfirst($model);
 		
 		// We load the model in case is not loaded by the controller
 		Core::loadModel($this->useModel);
 		
 		// Table to use. Watch conventions
 		$this->useTable = Core::getTable($this->useModel);
 	}
 
  
 	/**
 	 * Grabs desired number of last entries in the database provided
 	 */
 	 
 	public function getLast($number, $model)
 	{
 		// We set up connection
 		self::setUp($model);
 		
 		// Query to execute
 		$query = "SELECT id FROM `$this->useTable` ORDER BY id DESC LIMIT 0, $number";
 		
 		// Execution
 		$result = mysql_query($query);		
 		
 		// For each result
 		for($it = 0; $it < mysql_num_rows($result); $it++)
 		{
 			$row_array = mysql_fetch_row($result);
 			$this->objects[] = new $this->useModel($row_array[0]);
 		}	
 		
 		return $this->objects;
 	}
 	
 	/**
 	 * Grabs all entries of the desired model from the db
 	 *@param modelname
 	 *@param boolean justids. If enabled, ids instead of object will be returned
 	 *@param numrows it will just return num rows
 	 */
 	 
 	public function getAll($model, $justids = false, $numrows = false, $offset = NULL, $rowcount = NULL)
 	{
 		// We set up connection
 		self::setUp($model);
 		
 		// Query to execute
 		$query = "SELECT id FROM `$this->useTable` ORDER BY id DESC";
 		
 		// If offset and rowcount passed, we add it to the query
 		if(is_numeric($offset) && is_numeric($rowcount))
 		{
 			$query = $query . " LIMIT $offset, $rowcount";
 		}
 		
 		// Execution
 		$result = mysql_query($query);	
 	
 		// If just num rows asked
 		if($numrows == true)
 		{
 			return mysql_num_rows($result);
 		}	
 		
 		// If just ids asked
 		if($justids == true)
 		{
 			for($it = 0; $it < mysql_num_rows($result); $it++)
 			{
 				$row_array = mysql_fetch_row($result);
 				$ids[] = $row_array[0];
 			}	
 			
 			return $ids;
 		}
 		
 		// If object asked
 		for($it = 0; $it < mysql_num_rows($result); $it++)
 		{
 			$row_array = mysql_fetch_row($result);
 			$this->objects[] = new $this->useModel($row_array[0]);
 		}	
 
 		return $this->objects;
 	}
 	
 	/**
 	* Searches the table by field
 	* @param string field name
 	* @param whatever value
 	* @param string model to look at
 	* @param boolean useLike (Will use like instead if =, used by search engine)
 	* @param justids will make the function to return only ids
 	* @param int min
 	* @param int max
 	*/
 	
 	public function searchBy($field, $value, $model, $useLike = false, $justids = false, $offset = NULL, $rowcount = NULL)
 	{
 		// We set up connection
 		self::setUp($model);
 		
 		// Depends on useLike or not
 		if($useLike == false) { $param = "="; } else { $param = "like"; }
 	
 		// Query to execute
 		$query = "SELECT id FROM `$this->useTable` WHERE $field " . $param ." '$value' ORDER BY id DESC";
 		
 		// If offset and rowcount passed, we add it to the query
 		if(is_numeric($offset) && is_numeric($rowcount))
 		{
 			$query = $query . " LIMIT $offset, $rowcount";
 		}

 		// Execution
 		$result = mysql_query($query);		
 		
 		
 		// If we are asking for a bunch of objects
 		if($justids == false)
 		{
 			// For each result we create an object and store it into an array
 			for($it = 0; $it < mysql_num_rows($result); $it++)
 			{
 				// We append a new object to our objects array
 				$row_array = mysql_fetch_row($result);
 				$this->objects[] = new $this->useModel($row_array[0]);
 			}	
 			
 			// Finally return them
 			return $this->objects;
 			
 		// If not, we only store their ids and return them afterwards
 		} else {
 		
 			// For each result we store the id
 			for($it = 0; $it < mysql_num_rows($result); $it++)
 			{
 				// We append a new object to our objects array
 				$row_array = mysql_fetch_row($result);
 				$ids[] = $row_array[0];
 			}	
 			
 			// Return it =)
 			return $ids;
 		}
 	}
 	
 	/**
 	 * @returns mysql_num_rows value for this query
 	 * @ param string field name
 	 * @param whatever value
 	 * @param boolean useLike
 	 * @param  boolean offset
 	 * @param boolean rowcount
 	 */
 	 public function getNumRowsOfSearch($field, $value, $model, $useLike = false, $offset = NULL, $rowcount = NULL)
 	 {
 	 	// We set up connection
 		self::setUp($model);
 		
 		// Depends on useLike or not
 		if($useLike == false) { $param = "="; } else { $param = "like"; }
 	
 		// Query to execute
 		$query = "SELECT id FROM `$this->useTable` WHERE $field " . $param ." '$value' ORDER BY id DESC";
 		
 		// If offset and rowcount passed, we add it to the query
 		if(is_numeric($offset) && is_numeric($rowcount))
 		{
 			$query = $query . " LIMIT $offset, $rowcount";
 		}

 		// Execution
 		$result = mysql_query($query);		
 		
 		// There it goes!
 		return mysql_num_rows($result);
 	 }
 	  
 }
 
 ?>
Return current item: ArasPhp Framework