Guys, thanks for the prompt response. I don't think I've made myself entirely clear even though all your responses are aimed in the right direction. I think the closest one to where I think I'm headed is slick101's response.
I guess it will be easier to show an example of what I mean in code rather than words.
I'm including a class used to interact with the Menu table. This is an extremely simplified version of a table that has two fields: id, and name. The thing I love about this class is that I made it so that as long as your table's primary key is called "id" and you fill the $_fields array with all your table fields, it will provide you with decent database functionality.
As you can see, the db access code is hard coded into the class and when you have many tables, there is a lot of repeated code. I'm happy with the code I wrote. I'm looking for a way to extract this code from each class and put it somewhere else so that when I have to maintain it, I can do it in one place and not in EVERY class that accesses a table. Not sure what "the correct" way of doing this is. OOP is sort of a new field for me.
Here it is:
<?php
class Menu {
// DB connection
protected $_db;
// Table name
protected $_tableName = 'menu';
// Table fields
protected $_id;
protected $_name;
protected $_fields = array (
'id',
'name'
);
public function __construct() {
$this->_db = DB::getInstance();
}
public function getById($id) {
$s_id = doubleval($id);
$sql = "SELECT * FROM `" . $this->_tableName . "` WHERE `id` = $s_id LIMIT 1";
$result = $this->_db->query($sql);
if (mysql_num_rows($result) == 0) {
return true;
}
$row = mysql_fetch_array($result, MYSQL_ASSOC);
// Populate this class' properties with query results
foreach ($row as $k=>$v) {
$method = 'set_'.$k;
$this->$method($v);
}
if (mysql_error() == '') {
return true;
} else {
return false;
}
}
public function insert() {
$sql = "INSERT INTO `" . $this->_tableName . "` (";
// Remember to filter out the 'id' field while inserting rows
// as we want the DB to generate new ids
foreach ($this->_fields as $k) {
if ($k != 'id') {
$sql .= "`$k`, ";
}
}
// strip last comma from query
$sql = substr($sql, 0, -2);
$sql .= ') VALUES (';
foreach ($this->_fields as $k) {
if ($k != 'id') {
$method = 'get_'.$k;
$v = mysql_real_escape_string($this->$method());
$sql .= "'$v', ";
}
}
// strip last comma from query
$sql = substr($sql, 0, -2);
$sql .= ')';
$result = mysql_query($sql);
if (mysql_error() != '') {
return false;
} else {
$this->getById(mysql_insert_id());
return true;
}
}
public function update() {
$sql = "UPDATE `" . $this->_tableName . "` SET ";
foreach ($this->_fields as $k) {
if ($k != 'id') {
$method = 'get_'.$k;
$v = mysql_real_escape_string($this->$method());
$sql .= "`$k` = '$v', ";
}
}
// strip last comma from query
$sql = substr($sql, 0, -2);
$sql .= " WHERE `id` = ".doubleval($this->get_id());
$result = mysql_query($sql);
if (mysql_error() != '') {
return false;
} else {
return true;
}
}
public function delete() {
$s_id = doubleval($this->get_id());
$sql = "DELETE FROM `" . $this->_tableName . "` WHERE `id` = '$s_id' LIMIT 1";
$result = mysql_query($sql);
if (mysql_error() != '') {
die (mysql_error());
return false;
} else {
$this->reset();
return true;
}
}
protected function reset() {
foreach ($this->_fields as $k) {
$method = 'set_'.$k;
$this->$method('');
}
}
/**
* Returns $_id.
* @see Menu::$_id
*/
public function get_id() {
return $this->_id;
}
/**
* Returns $_name.
* @see Menu::$_name
*/
public function get_name() {
return $this->_name;
}
/**
* Sets $_id.
* @param object $_id
* @see Menu::$_id
*/
public function set_id($_id) {
$this->_id = $_id;
}
/**
* Sets $_name.
* @param object $_name
* @see Menu::$_name
*/
public function set_name($_name) {
$this->_name = $_name;
}
}
?>