Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Wzorzec MVC - model
Evinek
post 19.02.2014, 20:30:21
Post #1





Grupa: Zarejestrowani
Postów: 280
Pomógł: 46
Dołączył: 23.03.2010

Ostrzeżenie: (0%)
-----


Chodzi o model, który pobiera newsy.
Mam na przykład takie coś:
  1. class News extends Model {
  2. public function getAll() {
  3. return $this->db->query('SELECT * FROM `news`')->fetchAll();
  4. }
  5. }

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
  1. class Database {
  2.  
  3. protected $pdo;
  4. protected $stmt;
  5.  
  6. public function __construct($type, $host, $name, $user, $pass) {
  7. $this->pdo = new PDO($type . ':host=' . $host . ';dbname=' . $name, $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
  8. }
  9.  
  10. public function query($sql, $data = array()) {
  11. $this->stmt = $this->pdo->prepare($sql);
  12. $this->stmt->setFetchMode(PDO::FETCH_ASSOC);
  13. foreach($data as $key=>$value) {
  14. if(is_int($value))
  15. $this->stmt->bindValue(':' . $key, $value, PDO::PARAM_INT);
  16. else
  17. $this->stmt->bindValue(':' . $key, $value);
  18. }
  19. $this->stmt->execute();
  20. return $this;
  21. }
  22.  
  23. public function fetch() {
  24. return $this->stmt->fetch();
  25. }
  26.  
  27. public function fetchAll() {
  28. return $this->stmt->fetchAll();
  29. }
  30.  
  31. public function rowCount() {
  32. return $this->stmt->rowCount();
  33. }
  34.  
  35. public function lastId() {
  36. return $this->pdo->lastInsertId();
  37. }
  38.  
  39. public function pdo() {
  40. return $this->pdo;
  41. }
  42. }
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 13:42