Chodzi o model, który pobiera newsy.
Mam na przykład takie coś:
class News extends Model {
public function getAll() {
return $this->db->query('SELECT * FROM `news`')->fetchAll();
}
}
Muszę również pobierać newsy:
a) tylko 5 najnowszych
b) tylko usunięte (delete = 1)
c) tylko te, które mają datę publikacji mniejszą niż aktualna (publish < time())
d) tylko widoczne (visible = 1)
e) połączenie a + b, a + c, b + c, a + b + c + d i tak dalej, i tak dalej.
I pytanie, jak rozwiązać to aby nie tworzyć wielu metod, a najlepiej żeby była jedna czy dwie?
Klasa bazy danych
class Database {
protected $pdo;
protected $stmt;
public function __construct($type, $host, $name, $user, $pass) {
$this->pdo = new PDO
($type . ':host=' . $host . ';dbname=' . $name, $user, $pass, array(PDO
::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); }
public function query
($sql, $data = array()) { $this->stmt = $this->pdo->prepare($sql);
$this->stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach($data as $key=>$value) {
$this->stmt->bindValue(':' . $key, $value, PDO::PARAM_INT);
else
$this->stmt->bindValue(':' . $key, $value);
}
$this->stmt->execute();
return $this;
}
public function fetch() {
return $this->stmt->fetch();
}
public function fetchAll() {
return $this->stmt->fetchAll();
}
public function rowCount() {
return $this->stmt->rowCount();
}
public function lastId() {
return $this->pdo->lastInsertId();
}
public function pdo() {
return $this->pdo;
}
}