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) {