aboutsummaryrefslogtreecommitdiff
path: root/classes/Model.php
diff options
context:
space:
mode:
Diffstat (limited to 'classes/Model.php')
-rw-r--r--classes/Model.php47
1 files changed, 46 insertions, 1 deletions
diff --git a/classes/Model.php b/classes/Model.php
index b4f258d..4417074 100644
--- a/classes/Model.php
+++ b/classes/Model.php
@@ -135,9 +135,10 @@ abstract class Model {
* @return self The new item
*/
public static function create($pdo, $values) {
+ $class = get_called_class();
+
$columns = array_combine(static::$fillable_columns, $values);
$questions = [];
- $class = get_called_class();
foreach ($columns as $column => $value) {
$columns[$column] = $class::mutator($column, $value);
@@ -154,6 +155,50 @@ abstract class Model {
}
/**
+ * Search for rows
+ *
+ * @param PDO $pdo Database connection
+ * @param string[] $where Where clauses, to be ANDed
+ * @param mixed[] $values Variables to bind to the where clauses
+ *
+ * @throws PDOException Database error
+ *
+ * @return self[] Array of rows
+ */
+ public static function search($pdo, $where = [], $values = []) {
+ $class = get_called_class();
+
+ $stmt = $pdo->prepare("SELECT `id` FROM `".static::table()."`" . ((count($where) > 0) ? (" WHERE (" . implode(') AND (', $where) . ")") : ""));
+ $stmt->execute($values);
+
+ $items = [];
+ foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
+ $items[] = new $class($pdo, $row['id']);
+ }
+ return $items;
+ }
+
+ /**
+ * Number of matching rows
+ *
+ * @param PDO $pdo Database connection
+ * @param string[] $where Where clauses, to be ANDed
+ * @param mixed[] $values Variables to bind to the where clauses
+ *
+ * @throws PDOException Database error
+ *
+ * @return int The number of matching rows
+ */
+ public static function count($pdo, $where = [], $values = []) {
+ $class = get_called_class();
+
+ $stmt = $pdo->prepare("SELECT COUNT(*) FROM `".static::table()."`" . ((count($where) > 0) ? (" WHERE (" . implode(') AND (', $where) . ")") : ""));
+ $stmt->execute($values);
+
+ return $stmt->fetchColumn();
+ }
+
+ /**
* Post-__get() hook to modify the value
*
* @param string $key The column