diff options
author | Camil Staps | 2016-08-01 14:03:44 +0200 |
---|---|---|
committer | Camil Staps | 2016-08-01 14:03:44 +0200 |
commit | 4394857949ce8004fbf81c819f1e774cadad9b3f (patch) | |
tree | c8450147fd727d48177f85f4e712042b06e7df01 | |
parent | Makefile dependencies (diff) |
Use Model::search in child::get* methods
-rw-r--r-- | classes/Client.php | 14 | ||||
-rw-r--r-- | classes/Model.php | 29 | ||||
-rw-r--r-- | classes/Offer.php | 56 |
3 files changed, 38 insertions, 61 deletions
diff --git a/classes/Client.php b/classes/Client.php index acae192..0f0a2f8 100644 --- a/classes/Client.php +++ b/classes/Client.php @@ -40,12 +40,7 @@ class Client extends Model { * @return int[] The ids */ public function getContactIds() { - $ids = array(); - $contacts = $this->pdo->query("SELECT `id` FROM `".Constants::db_prefix."contact` WHERE `clientId`={$this->id}")->fetchAll(PDO::FETCH_ASSOC); - foreach ($contacts as $contact) { - $ids[] = $contact['id']; - } - return $ids; + return Contact::searchIds($this->pdo, ['`clientId`=?'], [$this->id]); } /** @@ -58,12 +53,7 @@ class Client extends Model { * @return contact[] An array indexed by id of instances of the contact class */ public function getContacts() { - $ids = $this->getContactIds(); - $contacts = array(); - foreach ($ids as $id) { - $contacts[$id] = new Contact($this->pdo, $id); - } - return $contacts; + return Contact::search($this->pdo, ['`clientId`=?'], [$this->id]); } //------------------------------------------------------------------------------ diff --git a/classes/Model.php b/classes/Model.php index 4417074..0cd1fd6 100644 --- a/classes/Model.php +++ b/classes/Model.php @@ -155,6 +155,28 @@ abstract class Model { } /** + * Search for rows, return only ids + * + * @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[] Array of ids + */ + public static function searchIds($pdo, $where = [], $values = []) { + $stmt = $pdo->prepare("SELECT `id` FROM `".static::table()."`" . ((count($where) > 0) ? (" WHERE (" . implode(') AND (', $where) . ")") : "")); + $stmt->execute($values); + + $ids = []; + foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) { + $ids[] = $row['id']; + } + return $ids; + } + + /** * Search for rows * * @param PDO $pdo Database connection @@ -168,12 +190,9 @@ abstract class Model { 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']); + foreach (self::searchIds($pdo, $where, $values) as $id) { + $items[] = new $class($pdo, $id); } return $items; } diff --git a/classes/Offer.php b/classes/Offer.php index a4d6b7f..8da2095 100644 --- a/classes/Offer.php +++ b/classes/Offer.php @@ -79,12 +79,7 @@ class Offer extends Model{ * @return int[] The ids */ public function getAssignmentIds() { - $ids = array(); - $assignments = $this->pdo->query("SELECT `id` FROM `".Constants::db_prefix."assignment` WHERE `offerId`={$this->id}")->fetchAll(PDO::FETCH_ASSOC); - foreach ($assignments as $assignment) { - $ids[] = $assignment['id']; - } - return $ids; + return Assignment::searchIds($this->pdo, ['`offerId`=?'], [$this->id]); } /** @@ -97,12 +92,7 @@ class Offer extends Model{ * @return assignment[] An array indexed by id of instances of the assignment class */ public function getAssignments() { - $ids = $this->getAssignmentIds(); - $assignments = array(); - foreach ($ids as $id) { - $assignments[$id] = new Assignment($this->pdo, $id); - } - return $assignments; + return Assignment::search($this->pdo, ['`offerId`=?'], [$this->id]); } /** @@ -115,12 +105,7 @@ class Offer extends Model{ * @return int[] The ids */ public function getDiscountIds() { - $ids = array(); - $discounts = $this->pdo->query("SELECT `id` FROM `".Constants::db_prefix."discount` WHERE `offerId`={$this->id}")->fetchAll(PDO::FETCH_ASSOC); - foreach ($discounts as $discount) { - $ids[] = $discount['id']; - } - return $ids; + return Discount::searchIds($this->pdo, ['`offerId`=?'], [$this->id]); } /** @@ -133,12 +118,7 @@ class Offer extends Model{ * @return discount[] An array indexed by id of instances of the discount class */ public function getDiscounts() { - $ids = $this->getDiscountIds(); - $discounts = array(); - foreach ($ids as $id) { - $discounts[$id] = new Discount($this->pdo, $id); - } - return $discounts; + return Discount::search($this->pdo, ['`offerId`=?'], [$this->id]); } /** @@ -162,12 +142,12 @@ class Offer extends Model{ * @return int|null The id, or null if no payment exists */ public function getPaymentId() { - $ids = array(); - $payments = $this->pdo->query("SELECT `id` FROM `".Constants::db_prefix."payment` WHERE `offerId`={$this->id}")->fetchAll(PDO::FETCH_ASSOC); - foreach ($payments as $payment) { - return $payment['id']; + $ids = Payment::searchIds($this->pdo, ['`offerId`=?'], [$this->id]); + if (count($ids) == 0) { + return null; + } else { + return $ids[0]; } - return null; } /** @@ -181,11 +161,7 @@ class Offer extends Model{ */ public function getPayment() { $id = $this->getPaymentId(); - if (is_null($id)) { - return null; - } else { - return new Payment($this->pdo, $id); - } + return is_null($id) ? null : new Payment($this->pdo, $id); } /** @@ -195,11 +171,7 @@ class Offer extends Model{ */ public function getPaymentReceived() { $payment = $this->getPayment(); - if (is_null($payment)) { - return null; - } else { - return $payment->date; - } + return is_null($payment) ? null : $payment->date; } /** @@ -210,11 +182,7 @@ class Offer extends Model{ * @return file|null The file, or null if it doesn't exist */ public function getInvoiceFile() { - if ($this->invoice_fileId != null) { - return new File($this->pdo, $this->invoice_fileId); - } else { - return null; - } + return is_null($this->invoice_fileId) ? null : new File($this->pdo, $this->invoice_fileId); } /** |