diff options
author | Camil Staps | 2016-08-01 13:48:14 +0200 |
---|---|---|
committer | Camil Staps | 2016-08-01 13:53:12 +0200 |
commit | efa2936ec538c236dcec71f419ad714524c7d2b9 (patch) | |
tree | 1d7e1903851fc4c37c72cf8ee8de006979bb8b4e /classes | |
parent | Paypal not on by default in braintree (diff) |
Removed BusinessAdmin::get* methods, added Model::search and
Model::count
Diffstat (limited to 'classes')
-rw-r--r-- | classes/BusinessAdmin.php | 306 | ||||
-rw-r--r-- | classes/File.php | 6 | ||||
-rw-r--r-- | classes/Model.php | 47 |
3 files changed, 51 insertions, 308 deletions
diff --git a/classes/BusinessAdmin.php b/classes/BusinessAdmin.php index f6abe14..7acb4ca 100644 --- a/classes/BusinessAdmin.php +++ b/classes/BusinessAdmin.php @@ -25,312 +25,6 @@ * Provides basic functions like adding elements to the database */ class BusinessAdmin { - //------------------------------------------------------------------------------ - // Getters and setters - //------------------------------------------------------------------------------ - - /** - * Get all user ids - * - * @see BusinessAdmin::getUsers() This funtion returns instances of the user class instead of just the ids - * - * @param PDO $pdo The PDO class for database connection - * @param string[] $where An array of WHERE clauses that will be AND-ed into a prepared statement - * @param mixed[] $variables An array of variables that should go into the prepared statement - * - * @throws PDOException Is something went wrong with the database - * - * @return int[] The ids - */ - public static function getUserIds($pdo, $where = [], $variables = []) { - $ids = []; - $users = $pdo->prepare("SELECT `id` FROM `".Constants::db_prefix."user`" . ((count($where) > 0) ? (" WHERE (" . implode(') AND (', $where) . ")") : "")); - $users->execute($variables); - $users = $users->fetchAll(PDO::FETCH_ASSOC); - foreach ($users as $user) { - $ids[] = $user['id']; - } - return $ids; - } - - /** - * Get all users - * - * @see BusinessAdmin::getUserIds() This function returns just the ids of the users, and not instances of the user class - * - * @param PDO $pdo The PDO class for database connection - * @param string[] $where An array of WHERE clauses that will be AND-ed into a prepared statement - * @param mixed[] $variables An array of variables that should go into the prepared statement - * - * @throws PDOException If something went wrong with the database - * - * @return user[] An array indexed by id of instances of the user class - */ - public static function getUsers($pdo, $where = [], $variables = []) { - $ids = self::getUserIds($pdo, $where, $variables); - $users = []; - foreach ($ids as $id) { - $users[$id] = new User($pdo, $id); - } - return $users; - } - - /** - * Get all file ids - * - * @see BusinessAdmin::getFiles() This funtion returns instances of the file class instead of just the ids - * - * @param PDO $pdo The PDO class for database connection - * @param string[] $where An array of WHERE clauses that will be AND-ed into a prepared statement - * @param mixed[] $variables An array of variables that should go into the prepared statement - * - * @throws PDOException Is something went wrong with the database - * - * @return int[] The ids - */ - public static function getFileIds($pdo, $where = [], $variables = []) { - $ids = []; - $files = $pdo->prepare("SELECT `id` FROM `".Constants::db_prefix."file`" . ((count($where) > 0) ? (" WHERE (" . implode(') AND (', $where) . ")") : "")); - $files->execute($variables); - $files = $files->fetchAll(PDO::FETCH_ASSOC); - foreach ($files as $file) { - $ids[] = $file['id']; - } - return $ids; - } - - /** - * Get all files - * - * @see BusinessAdmin::getFileIds() This function returns just the ids of the files, and not instances of the file class - * - * @param PDO $pdo The PDO class for database connection - * @param string[] $where An array of WHERE clauses that will be AND-ed into a prepared statement - * @param mixed[] $variables An array of variables that should go into the prepared statement - * - * @throws PDOException If something went wrong with the database - * - * @return file[] An array indexed by id of instances of the file class - */ - public static function getFiles($pdo, $where = [], $variables = []) { - $ids = self::getFileIds($pdo, $where, $variables); - $files = []; - foreach ($ids as $id) { - $files[$id] = new File($pdo, $id); - } - return $files; - } - - /** - * Get all client ids - * - * @see BusinessAdmin::getClients() This funtion returns instances of the client class instead of just the ids - * - * @param PDO $pdo The PDO class for database connection - * - * @throws PDOException If something went wrong with the database - * - * @return int[] The ids - */ - public static function getClientIds($pdo) { - $ids = array(); - $clients = $pdo->query("SELECT `id` FROM `".Constants::db_prefix."client`")->fetchAll(PDO::FETCH_ASSOC); - foreach ($clients as $client) { - $ids[] = $client['id']; - } - return $ids; - } - - /** - * Get all clients - * - * @see BusinessAdmin::getClientIds() This function returns just the ids of the clients, and not instances of the client class - * - * @param PDO $pdo The PDO class for database connection - * - * @throws PDOException If something went wrong with the database - * - * @return client[] An array indexed by id of instances of the client class - */ - public static function getClients($pdo) { - $ids = self::getClientIds($pdo); - $clients = array(); - foreach ($ids as $id) { - $clients[$id] = new Client($pdo, $id); - } - return $clients; - } - - /** - * Get all contact ids - * - * @see BusinessAdmin::getContacts() This funtion returns instances of the contact class instead of just the ids - * - * @param PDO $pdo The PDO class for database connection - * - * @throws PDOException Is something went wrong with the database - * - * @return int[] The ids - */ - public static function getContactIds($pdo) { - $ids = array(); - $contacts = $pdo->query("SELECT `id` FROM `".Constants::db_prefix."contact`")->fetchAll(PDO::FETCH_ASSOC); - foreach ($contacts as $contact) { - $ids[] = $contact['id']; - } - return $ids; - } - - /** - * Get all contacts - * - * @see BusinessAdmin::getContactIds() This function returns just the ids of the contacts, and not instances of the contact class - * - * @param PDO $pdo The PDO class for database connection - * - * @throws PDOException If something went wrong with the database - * - * @return contact[] An array indexed by id of instances of the contact class - */ - public static function getContacts($pdo) { - $ids = self::getContactIds($pdo); - $contacts = array(); - foreach ($ids as $id) { - $contacts[$id] = new Contact($pdo, $id); - } - return $contacts; - } - - /** - * Get all offer ids - * - * @see BusinessAdmin::getOffers() This funtion returns instances of the offer class instead of just the ids - * - * @param PDO $pdo The PDO class for database connection - * @param string[] $where An array of WHERE clauses that will be AND-ed - * - * @throws PDOException Is something went wrong with the database - * - * @return int[] The ids - */ - public static function getOfferIds($pdo, $where = array()) { - $ids = array(); - $offers = $pdo->query("SELECT `id` FROM `".Constants::db_prefix."offer`" . ((count($where) > 0) ? (" WHERE (" . implode(') AND (', $where) . ")") : ""))->fetchAll(PDO::FETCH_ASSOC); - foreach ($offers as $offer) { - $ids[] = $offer['id']; - } - return $ids; - } - - /** - * Get all offers - * - * @see BusinessAdmin::getOfferIds() This function returns just the ids of the offers, and not instances of the offer class - * - * @param PDO $pdo The PDO class for database connection - * @param string[] $where An array of WHERE clauses that will be AND-ed - * - * @throws PDOException If something went wrong with the database - * - * @return offer[] An array indexed by id of instances of the offer class - */ - public static function getOffers($pdo, $where = array()) { - $ids = self::getOfferIds($pdo, $where); - $offers = array(); - foreach ($ids as $id) { - $offers[$id] = new Offer($pdo, $id); - } - return $offers; - } - - /** - * Get all assignment ids - * - * @see BusinessAdmin::getAssignments() This funtion returns instances of the assignment class instead of just the ids - * - * @param PDO $pdo The PDO class for database connection - * @param string[] $where An array of WHERE clauses that will be AND-ed - * - * @throws PDOException Is something went wrong with the database - * - * @return int[] The ids - */ - public static function getAssignmentIds($pdo, $where = array()) { - $ids = array(); - $assignments = $pdo->query("SELECT `id` FROM `".Constants::db_prefix."assignment`" . ((count($where) > 0) ? (" WHERE (" . implode(') AND (', $where) . ")") : ""))->fetchAll(PDO::FETCH_ASSOC); - foreach ($assignments as $assignment) { - $ids[] = $assignment['id']; - } - return $ids; - } - - /** - * Get all assignments - * - * @see BusinessAdmin::getAssignmentIds() This function returns just the ids of the assignments, and not instances of the assignment class - * - * @param PDO $pdo The PDO class for database connection - * @param string[] $where An array of WHERE clauses that will be AND-ed - * - * @throws PDOException If something went wrong with the database - * - * @return assignment[] An array indexed by id of instances of the assignment class - */ - public static function getAssignments($pdo, $where = array()) { - $ids = self::getAssignmentIds($pdo, $where); - $assignments = array(); - foreach ($ids as $id) { - $assignments[$id] = new Assignment($pdo, $id); - } - return $assignments; - } - - /** - * Get all discount ids - * - * @see BusinessAdmin::getDiscounts() This funtion returns instances of the discount class instead of just the ids - * - * @param PDO $pdo The PDO class for database connection - * @param string[] $where An array of WHERE clauses that will be AND-ed - * - * @throws PDOException Is something went wrong with the database - * - * @return int[] The ids - */ - public static function getDiscountIds($pdo, $where = array()) { - $ids = array(); - $discounts = $pdo->query("SELECT `id` FROM `".Constants::db_prefix."discount`" . ((count($where) > 0) ? (" WHERE (" . implode(') AND (', $where) . ")") : ""))->fetchAll(PDO::FETCH_ASSOC); - foreach ($discounts as $discount) { - $ids[] = $discount['id']; - } - return $ids; - } - - /** - * Get all discounts - * - * @see BusinessAdmin::getDiscountIds() This function returns just the ids of the discounts, and not instances of the discount class - * - * @param PDO $pdo The PDO class for database connection - * @param string[] $where An array of WHERE clauses that will be AND-ed - * - * @throws PDOException If something went wrong with the database - * - * @return discount[] An array indexed by id of instances of the discount class - */ - public static function getDiscounts($pdo, $where = array()) { - $ids = self::getDiscountIds($pdo, $where); - $discounts = array(); - foreach ($ids as $id) { - $discounts[$id] = new Discount($pdo, $id); - } - return $discounts; - } - - //------------------------------------------------------------------------------ - // Other functions - //------------------------------------------------------------------------------ - /** * Format a date nicely * diff --git a/classes/File.php b/classes/File.php index ad44448..68d6791 100644 --- a/classes/File.php +++ b/classes/File.php @@ -30,7 +30,11 @@ class File extends Model { $table = 'file', $fillable_columns = ['filename', 'secret_key']; - /** {@inheritDoc} */ + /** + * {@inheritDoc} + * @param PDO $pdo {@inheritDoc} + * @param mixed[] $values {@inheritDoc} + */ public static function create($pdo, $values) { $filename = $values[0]; 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 |