<?php
class Product implements ActiveRecordInterface
{
private $ProductID;
private $Name;
private $Price;
// Enzovoort...
public function __construct($id = null)
{
if (isset($id)) {
$this->select($id);
}
}
/**
* @param int $id
* @return bool
*/
public function select($id)
{
if (is_numeric($id) && $id <= PHP_INT_MAX) {
$id = (int) abs($id);
} else {
return false;
}
$query = 'SELECT * FROM products WHERE product_id = ' . $id;
$dbh = new Database();
if ($result = $dbh->query($query)) {
if ($result->num_rows == 1) {
$record = $result->fetch_assoc();
$result->free();
$dbh->close();
unset($result, $dbh);
$this->ProductID = $id;
// Beslissingsregels
if ($record['product_name_nl'] != null) {
$this->Name = $record['product_name_nl'];
} else {
$this->Name = $record['product_name_en_us'];
}
if ($record['sale_price'] != null) {
$this->Price = (float) $record['sale_price'];
} elseif ($record['price'] != null) {
$this->Price = (float) $record['price'];
} else {
$this->Price = null;
}
// Enzovoort...
return true;
}
}
return false;
}
}
?>
Voor uiteenlopende lijsten met meerdere producten (tabellen, prijslijsten, zoekresultaten, enzovoort) is het onpraktisch om meerdere complete productobjecten te laden die elk een eigen query uit te voeren. Wat is hiervoor een efficiënte oplossing waarbij de beslissingsregels niet verloren gaan?