That's where things get tricky. The example is meant purely as the simplest MVC demo I could come up with and perhaps the moment you ask that question, the example is already inadequate, if you want a well structured, nicely abstract application. For starters, if you're headed this way, you'd probably have a seperate object for connecting to the database, rather than doing so within the NewsReader...
Once you start talking about a seperate News class though, you start introducing ideas of having some kind of domain model. The big problem is how you connect this type of object oriented design to a relational database. Martin Fowler in PoEAA spends alot of time on this subject and suggests a number of alternative ways to approach the problem. In other words I can't give you a right answer.
Here's one way it could be done (hacked together off the top of my head);
PHP Code:
<?php
class NewsReader {
var $db; // Stores database connection resource
var $result;
function NewsReader() {
// Connect to database
$this->db = & mysql_connect(DB_HOST,DB_USER,DB_PASS);
mysql_select_db(DB_NAME,$this->db);
}
/**
* Get the top 10 news items by date
*/
function & findLatestNews () {
$sql = "SELECT *
FROM news
ORDER BY date_published
DESC
LIMIT 0,10;";
$this->result = & mysql_query($sql,$this->db);
}
/**
* Get a single news item
*/
function & findNewsItemById ($id) {
$id = mysql_escape_string($id);
$sql = "SELECT *
FROM news
WHERE id ='$id';";
$this->result = & mysql_query($sql,$this->db);
}
function & read() {
$row = mysql_fetch_array($this->result,MYSQL_ASSOC);
if ( $row ) {
$item = & new NewsItem($db);
$item->populate($row);
return $item;
} else {
@mysql_data_seek($this->result,0);
return false;
}
}
}
class NewsItem {
var $db;
var $row;
function NewsItem(& $db) {
$this->db = & $db;
}
function populate($row) {
$this->row = $row;
}
function getId() {
return $this->row['id'];
}
function getTitle() {
return $this->row['title'];
}
function getDate() {
return date('H:i:s d-m-Y',$this->row['date']);
}
function getBody() {
return $this->row['body'];
}
function getAuthor() {
return $this->row['author'];
}
function create() {
$row = array_map('mysql_escape_string',$this->row);
$sql = "INSERT INTO
news
SET
title='{$row['title']}',
date='".time()."',
body='{$row['body']}',
author='{$row['author']}';";
return mysql_query($sql,$this->db);
}
function update() {
$row = array_map('mysql_escape_string',$this->row);
$sql = "UPDATE
news
SET
title='{$row['title']}',
date='".time()."',
body='{$row['body']}',
author='{$row['author']}'
WHERE
id='{row['id']}';";
return mysql_query($sql,$this->db);
}
function delete() {
$row = array_map('mysql_escape_string',$this->row);
$sql = "DELETE FROM
news
WHERE
id='{$row['id']}';";
return mysql_query($sql,$this->db);
}
}
?>
To use it, you might have;
PHP Code:
$reader = & new NewsReader();
$reader->findLatestNews();
while ( $item = & $reader->read() ) {
echo ( $item->title() );
}
Also to update an item you might have (this is just example - the NewsItem class needs to have a better API for modifying / checking the contents of a row)
PHP Code:
$reader = & new NewsReader();
$reader->findNewsItemById(3);
$item = & $reader->read();
$item->row['date'] = time();
$item->update();
But again I stress, that's only one way to do it.
Bookmarks