
Originally Posted by
mwmitchell
Anyone want to write out an example of a DAO/DataObject class using Eclipse Database?

- would just love to see it! I have tried, and could post some examples of what I've done.
This is probably how I would do it:
PHP Code:
<?php
include_once(APP_ROOT . 'eclipse/MyDatabase.php');
class DAO {
var $dbConn;
function DAO() {
$this->dbConnect();
}
function dbConnect() {
$this->dbConn =& new MyDatabase('news_db', 'localhost');
if(!$this->dbConn->connect('username', 'password')) {
trigger_error('Unable to establish database connection.<br />
Error returned from MySQL: ' . $this->dbConn->getErrorMessage());
}
}
}
class NewsDAO extends DAO {
function NewsDAO() {
parent::DAO();
}
function &getAllHeadlines($offset = 0, $limit) {
include_once(APP_ROOT . 'eclipse/PagedQuery.php');
$query =& new PagedQuery(
$this->dbConn->query('SELECT * FROM news ORDER BY article_id DESC'), $limit
);
if(!$query->isSuccess()) {
trigger_error('Error while retrieving headlines.<br />$
Error message returned from MySQL: ' . $query->getErrorMessage())
}
include_once(APP_ROOT . 'eclipse/QueryIterator.php');
$page = $query->getPage($offset);
return new QueryIterator($page);
}
function getArticleById($id) {
$query =& $this->dbConn->query('SELECT * FROM news WHERE article_id=' . $id);
if(!$query->isSuccess()) {
trigger_error('Error while retrieving article.<br />
Error message returned from MySQL: ' . $query->getErrorMessage());
}
return $query->getRow(0);
}
}
?>
Bookmarks