The simplest web widget to the most complex online e-commerce website have one thing in
common: they deal with data. So much of programming revolves around data access and
manipulation. With the massive proliferation of the Internet, cheaper storage devices, improved understanding of analytics, and greater expectations for information access, data is being leveraged in more interesting and unique ways. The Data Access Object Design Pattern aims to help construct objects that can work easily (transparently) with all of this data.
Programming typically can be a lot of repetition. This was especially true before more popular frameworks started being released. Most PHP programmers can count into the double digits the number of CRUD (create, read, update, delete) applications they’ve had to make. One of the major portions of repetition in the standard create/update application is the data source manipulation. For the rest of the discussion, I’m going to stop generalizing the data source and refer to it as SQL.
In the application, a SQL statement has to be written to create the entity in the database. Next, an additional SQL statement must be written in order to provide updates to any of the individual features of that entity. The repetition involved in creating these SQL statements is not only boring but also not best practice.
Code example
First create your own database and create following table
CREATE TABLE task (
id INT PRIMARY KEY AUTO_INCREMENT,
subject VARCHAR(255),
description text
);
Create php file called "
baseDao" and put following code
<?php
class baseDao
{
private $connection;
public function __construct() {
$this->connectToDb(DB_USER, DB_PASS, DB_HOST, DB_NAME);
}
public function connectToDb($user, $pass, $host, $database) {
$this->connection = mysql_connect($host, $user, $pass);
mysql_select_db($database, $this->connection);
}
public function fetch($value, $key = NULL)
{
if (is_null($key)) {
$key = $this->_primaryKey;
}
$sql = "SELECT * FROM {$this->_tableName} WHERE {$key} = '" . $value . "';";
$results = mysql_query($sql, $this->connection);
$rows = array();
while ($result = mysql_fetch_array($results)) {
$rows[] = $result;
}
return $rows;
}
public function update($keyedArray)
{
$sql = "UPDATE {$this->_tableName} SET ";
$updates = array();
foreach ($keyedArray as $column=>$value) {
$updates[] = "{$column}='" . $value . "'" ;
}
$sql .= implode(",", $updates);
$sql .= " where {$this->_primaryKey}='". $keyedArray[$this->_primaryKey] . "';";
mysql_query($sql, $this->connection);
}
public function save($keyedArray)
{
$sql = "INSERT INTO {$this->_tableName} ";
$updates = array();
foreach ($keyedArray as $column=>$value) {
$updates_columns[] = "{$column}";
$updates_values[] = "'" . $value . "'" ;
}
$sql .= "(";
$sql .= implode(",", $updates_columns);
$sql .= ")";
$sql .= " VALUES (";
$sql .= implode(",", $updates_values);
$sql .= ");";
if (!mysql_query($sql, $this->connection))
echo mysql_error();
}
}
Finally you can use the dao class as follows:
<?php
//DB_USER, DB_PASS, DB_HOST, DB_NAME
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_HOST', 'localhost');
define('DB_NAME', 'blog_samples');
//include "baseDao.php";
include "taskDao.php";
$taskDao = new taskDao();
$updates=array('subject' => 'testSubjectValue', 'description' => 'testDescriptionValue');
$taskDao->save($updates);
echo "<br/>Dao pattern !<br/>";
Good luck !