Building a Model Class for the HMVC Design Pattern - Adding methods to save and delete database rows
(Page 4 of 4 )
To get the previous model class up and running as expected, it’s necessary to code within it a couple of additional methods that save, update and delete records in the selected MySQL table.
With that requirement in mind, below I listed the complete definition of the file that contains the model, this time including those brand new methods. Here’s the finished version of that file:
(Model.php)
<?php
class Model
{
private $_db = NULL;
private $_cache = NULL;
private $_table = 'default';
// factory method (chainable)
public static function factory($table = '')
{
return new self($table);
}
// constructor
public function __construct($table = '')
{
// store instance of MySQL class
$this->_db = MySQL::getInstance();
// store instance of Cache class
$this->_cache = Cache::getInstance();
if ($table !== '')
{
$this->_table = $table;
}
}
// get all rows from the specified table
public function fetchAll()
{
// if cache is valid, fetch rows from the cache file
if ($this->_cache->valid($this->_table))
{
return $this->_cache->get($this->_table);
}
// otherwise, fetch rows from the database
else
{
$rows = array();
$this->_db->query('SELECT * FROM ' . $this->_table);
while ($row = $this->_db->fetch())
{
$rows[] = $row;
}
$this->_cache->set($this->_table, $rows);
return $rows;
}
}
// update/insert row
public function save(array $data, $id = NULL)
{
if (!empty($data))
{
foreach ($data as $field => $value)
{
// quote strings
$value = mysql_escape_string($value);
if (!is_numeric($value))
{
$data[$field] = ''' . $value . ''';
}
}
// update row
if ($id !== NULL)
{
$set = '';
foreach($data as $field => $value)
{
$set .= $field .'=' . $value . ',';
}
$set = substr($set, 0, -1);
$this->_db->query('UPDATE ' . $this->_table . ' SET ' . $set . ' WHERE id=' . (int)$id);
$this->_db->query('DELETE FROM ' . $this->_table . ' WHERE id=' . (int)$id); (int)$id);
}
}
}// End Model class
There you have it. Now, the model has been given the ability to perform CRUD operations against a selected MySQL table via the “save()” and “delete()” methods coded above. Of course, this is only an example class that can be easily enhanced to support additional features; keep that in mind if you’re planning to use it in production environments.
And with this last code sample, we’ve come to the end of this tutorial. At this stage, the framework is almost ready to be used for developing web applications that follow the HMVC design pattern, except for one small -- yet crucial --component: a view class. However, this will be built in the next part of the series.
Final thoughts
Over this fifth part of the series, I added to the example framework a generic model class, which will allow us to implement in future applications a fully-working HMVC schema. As you saw before, the method of this class, which fetches all of the rows from a specified MySQL table, also caches this data. However, if this approach doesn’t fit your requirements, feel free to modify the method in question and tweak it at will.
Now that the framework includes a decent model, the next step that must be taken toward the implementation of the HMVC design pattern is building a view class that sends some kind of output to users, generally in the form of HTML pages.
The development of this class will be covered in depth in the upcoming tutorial, so don’t miss it!
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.