Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a personal account of how I approach designing my own framework classes when working with a database.</p> <p>Typically, when designing an MVC framework, I lay out two core concepts for developing the classes:</p> <ul> <li>a single database class that manages various aspects such as query building, row count return, insert ID, etc.</li> <li>an interface data class to ensure that each class remains consistent (for example, each class must have all four <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete" rel="nofollow">CRUD</a> operations)</li> </ul> <p><b>Database class</b></p> <p>My database class usually looks something like the following. You will notice towards the bottom that there are mutator functions like you have specified. These are specifically designed only to <b>set</b> and <b>get</b> data from the class itself. There is minor validation (<code>if(is_resource(...))</code> in get_connection), though I have heard that this sort of code should be in the Model level. <pre><code> public $error; private $location = ''; private $username = ''; private $password = ''; private $database = ''; public function __construct() { $connection = mysql_connect($this-&gt;location,$this-&gt;username,$this-&gt;password); $this-&gt;set_connection($connection); $db_select = mysql_select_db($this-&gt;database,$this-&gt;get_connection()); } public function query() { $query = mysql_query($this-&gt;query,$this-&gt;connection); $this-&gt;set_insert_id(mysql_insert_id($this-&gt;connection)); switch(substr(strtolower($this-&gt;query),0,6)) { case 'select': $this-&gt;set_row_count(mysql_num_rows($query)); $row_count = $this-&gt;get_row_count(); if($row_count == 0) { $this-&gt;set_results(0); } else { while($result = mysql_fetch_assoc($query)) { $results = $this-&gt;get_results(); $results[] = $result; $this-&gt;set_results($results); } } break; case 'insert': case 'update': case 'delete': case 'replace': default: $this-&gt;set_results(($query === true)); break; } } public function get_query() { return $this-&gt;query; } public function set_query($query) { $this-&gt;query = $query; } public function get_insert_id() { return $this-&gt;insert_id; } public function set_insert_id($insert_id) { if(is_numeric($insert_id)) { $this-&gt;insert_id = $insert_id; } } public function get_results() { return $this-&gt;results; } public function set_results($results) { $this-&gt;results = $results; } public function get_row_count() { return $this-&gt;row_count; } public function set_row_count($row_count) { if(is_numeric($row_count)) { $this-&gt;row_count = $row_count; } } public function get_connection() { return $this-&gt;connection; } public function set_connection($connection) { if(is_resource($connection)) { $this-&gt;connection = $connection; } else { $this-&gt;connection = false; } } } ?&gt; </code></pre> <p><b>Data class</b></p> <p>The interface data class is simply a guideline for other classes, so its very straightforward. When doing this approach, it's easy to manipulate with your data.</p> <pre><code>&lt;?php interface Data { public function create(); public function read(); public function update(); public function delete(); } ?&gt; </code></pre> <p><b>An example class</b></p> <p>Here is an example class that I would typically use with the previous stated classes that manipulates the Database and Data classes. When using a typical CRUD operation, I do a call to the Database class, set the query, query the database, then return the results. That's as simple as it can get, and any other further requirements can be appended into each function.</p> <pre><code>&lt;?php require_once('classes/class.Data.php'); require_once('classes/class.Database.php'); class Settings implements Data { private $profile_id; private $settings = array(); public function create() { $settings_instance = new Database; $sql = 'INSERT INTO settings (' . implode(',',array_keys($this-&gt;settings)) . ') VALUES ("' . implode('","',array_values($this-&gt;settings)) . '");'; $settings_instance-&gt;set_query($sql); $settings_instance-&gt;query(); return $settings_instance-&gt;get_results(); } public function read() { $settings_instance = new Database; $sql = 'SELECT * FROM settings WHERE profile_id = "' . $this-&gt;profile_id . '"'; $settings_instance-&gt;set_query($sql); $settings_instance-&gt;query(); $settings_results = $settings_instance-&gt;get_results(); if($settings_results) { $this-&gt;set_settings($settings_results[0]); } } public function update() { $keys = array_keys($this-&gt;settings); $values = array_values($this-&gt;settings); $settings_instance = new Database; $sql = 'UPDATE settings SET '; foreach($keys as $index =&gt; $key) { if($keys[$index] == "profile_id") continue; $sql .= $keys[$index] . ' = "' . $values[$index] . '", '; } $sql = rtrim($sql,', '); $sql .= ' WHERE profile_id = "' . $this-&gt;profile_id . '"'; $settings_instance-&gt;set_query($sql); $settings_instance-&gt;query(); return $settings_instance-&gt;get_results(); } public function delete() { $settings_instance = new Database; $sql = 'DELETE FROM settings WHERE profile_id = "' . $this-&gt;profile_id . '"'; $settings_instance-&gt;set_query($sql); $settings_instance-&gt;query(); return $settings_instance-&gt;get_results(); } public function get_profile_id() { return $this-&gt;profile_id; } public function set_profile_id($profile_id) { if(is_numeric($profile_id)) { $this-&gt;profile_id = $profile_id; } } public function get_setting($index) { if(isset($this-&gt;settings[$index])) { return $this-&gt;settings[$index]; } } public function set_setting($index,$new_value) { if(isset($this-&gt;settings[$index])) { $this-&gt;settings[$index] = $new_value; } } public function get_settings() { return $this-&gt;settings; } public function set_settings($settings) { if(is_array($settings)) { $this-&gt;settings = $settings; } } public function get_setting_types() { return $this-&gt;setting_types; } public function set_setting_types($setting_types) { if(is_array($setting_types)) { $this-&gt;setting_types = $setting_types; } } } ?&gt; </code></pre> <p><b>Some example code</b></p> <p>Here is some example Model code to go along with this approach. I find this to be quite readable. It will create a new settings row in the database with the profile_id = 1.</p> <pre><code>require_once('class.Settings.php'); $Settings = new Settings; $Settings-&gt;set_profile_id(1); $Settings-&gt;create(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload