View Issue Details

IDProjectCategoryView StatusLast Update
318PHPOF2Defaultpublic22 Jun 2010 22:25
Reportersamadams83 Assigned Totimj  
PrioritynormalSeverityfeatureReproducibilityN/A
Status closedResolutionfixed 
Product Version0.11.2 
Target Version0.12.0Fixed in Version0.12.0 
Summary318: Add function to search for records using associative array
DescriptionThere is currently a basicSearch() method, which takes a single key and value. It would be good to have a more advanced version that accepted an array of key-value pairs. This would be similar to the getUnique method (in PHPOF2_DBRow), except could return multiple rows.
Additional InformationAttached is a patch file with a new 'find' function.
TagsNo tags attached.
Attached Files
DBTable.php.patch (2,067 bytes)   
--- DBTable.php	2010-01-11 09:13:44.000000000 +0000
+++ DBTable2.php	2010-01-11 09:17:43.000000000 +0000
@@ -570,6 +570,52 @@
 		return $this->_search('LIKE', $field, $value, $orderby, $results_per_page, $page);
 	}
 
+	/**
+	 * The field-value pairs are used to build a like for like comparison query,
+	 * the result set of which is returned.
+	 * Optionally, the fields returned can be specified and/or the results ordered.
+	 * 
+	 * @param array $field_value_pairs
+	 * @param array $fields_to_select
+	 * @param string $orderby
+	 * @return mysqli result object
+	 */
+	public function find(array $field_value_pairs, $fields_to_select = null, $orderby = PHPOF2_DBTable::ORDER_DEFAULT)
+	{
+		if ($orderby == PHPOF2_DBTable::ORDER_DEFAULT) {
+			$orderby = $this->getOrderPreference();
+		}
+		//Check the input
+		if (!is_array($field_value_pairs) || count($field_value_pairs) < 1) {
+			throw new Exception('Field/value pairs array passed to PHPOF2_DBTable::find() was empty, could not retrieve values');
+		}
+
+		// Construct the WHERE clause
+		$conditions = array();
+		foreach ($field_value_pairs as $field_tmp => $value_tmp){
+			$conditions[] = $this->db->quoteIdentifier($field_tmp) . '=' . $this->db->quote($value_tmp) . "\n";
+		}
+		$where_clause = implode(' AND ', $conditions);
+
+		//determine which fields to select
+		if (is_array($fields_to_select) && count($fields_to_select) > 0) {
+			$fields_to_select_array = array();
+			foreach($fields_to_select as $field) {
+				$fields_to_select_array[] = $this->db->quoteIdentifier($field);
+			}
+			$fields_to_select = implode(',', $fields_to_select_array);
+		} else {
+			$fields_to_select = $this->fieldlist;
+		}
+
+		$sql = "SELECT $fields_to_select FROM $this->name WHERE $where_clause";
+
+		//add order by to sql
+		if ($orderby) $sql .= " ORDER BY $orderby";
+
+		return $this->db->query($sql);
+	}
+
 	protected function _search ($clause, $field, $value, $orderby = PHPOF2_DBTable::ORDER_DEFAULT, $results_per_page = 0, $page = 0)
 	{
 		if ($orderby == PHPOF2_DBTable::ORDER_DEFAULT) {
DBTable.php.patch (2,067 bytes)   

Activities

timj

22 Jun 2010 18:09

manager   ~365

Fixed in SVN r2160

Issue History

Date Modified Username Field Change
11 Jan 2010 09:30 samadams83 New Issue
11 Jan 2010 09:30 samadams83 File Added: DBTable.php.patch
12 Jan 2010 22:37 timj Assigned To => timj
12 Jan 2010 22:37 timj Status new => assigned
12 Jan 2010 22:37 timj Target Version => 0.12.0
22 Jun 2010 18:09 timj Note Added: 365
22 Jun 2010 18:09 timj Status assigned => resolved
22 Jun 2010 18:09 timj Resolution open => fixed
22 Jun 2010 18:09 timj Fixed in Version => 0.12.0
22 Jun 2010 18:09 timj Summary Need a function to search for records using associative array => Add function to search for records using associative array
22 Jun 2010 22:25 timj Status resolved => closed