/** * Finds object in database and returns (an array of) objects with values. */ public static function Find($where = null, $query = null) { static::Init(); // Build query if no custom query specified if (is_null($query)) { $query = 'SELECT * FROM "' . static::$table . '"'; // $where is an array with key/value pairs if (is_array($where) && count($where) > 0) { $fields = array(); foreach ($where as $key => $value) { $fields[] = static::$conn->WhereField($key, static::$columns[$key]['type'] == static::TYPE_TEXT); } $query .= ' WHERE (' . implode(') AND (', $fields) . ') '; $args = array_values($where); } elseif (!is_null($where)) { $query .= ' WHERE ' . static::KeyStatic(); $args = $where; } } else { // Get arguments (for prepared statements) if (func_num_args() > 2) { $args = func_get_args(); if (isset($args[2]) && is_array($args[2])) { $args = array_values($args[2]); } else { $args = array_slice($args, 2); } // Use all arguments as values (except for where and query of course) } } if (isset($args)) { $rs = static::$conn->Execute($query, $args); } else { $rs = static::$conn->Execute($query); } // No rows = raise error if ($rs->num_rows == 0) { throw new ActiveRecord_NotFoundException('Row not found in table <i>' . SafeHTML(static::$table) . '</i>'); } // One row = return single instance if (!is_null($where) && $rs->num_rows == 1) { $row = $rs->Fetch(); $instance = new static(); $instance->Load($row); return $instance; } // Else: return array of instances $result = array(); while ($row = $rs->Fetch()) { $instance = new static(); $instance->Load($row); $result[] = $instance; } // Do not return array for single result if (is_array($result) && count($result) == 1) { $result = reset($result); } return $result; }
public static function Match($authorid, $subject, $date) { static::Init(); // Build query $query = 'SELECT * FROM "' . static::$table . '" ' . 'WHERE ("subject" = ?) ' . 'AND ("authorid" = ?) ' . 'AND ("post_date" > ?) ' . 'AND ("post_date" < ?) ' . 'LIMIT 0,1'; $rs = static::$conn->Execute($query, $subject, $authorid, $date - self::MATCH_RANGE, $date + self::MATCH_RANGE); // No rows = raise error if ($rs->num_rows == 0) { throw new ActiveRecord_NotFoundException('Row not found in table <i>' . SafeHTML(static::$table) . '</i>'); } $row = $rs->Fetch(); $instance = new static(); $instance->Load($row); return $instance; }