Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In my case I have a database class that handle all the direct database interaction such as querying, fetching, and such. So if I had to change my database from <a href="http://en.wikipedia.org/wiki/MySQL" rel="nofollow">MySQL</a> to <a href="http://en.wikipedia.org/wiki/PostgreSQL" rel="nofollow">PostgreSQL</a> there won't be any problem. So adding that extra layer can be useful.</p> <p>Each table can have its own class and have its specific methods, but to actually get the data, it lets the database class handle it:</p> <h3>File <code>Database.php</code></h3> <pre><code>class Database { private static $connection; private static $current_query; ... public static function query($sql) { if (!self::$connection){ self::open_connection(); } self::$current_query = $sql; $result = mysql_query($sql,self::$connection); if (!$result){ self::close_connection(); // throw custom error // The query failed for some reason. here is query :: self::$current_query $error = new Error(2,"There is an Error in the query.\n&lt;b&gt;Query:&lt;/b&gt;\n{$sql}\n"); $error-&gt;handleError(); } return $result; } .... public static function find_by_sql($sql){ if (!is_string($sql)) return false; $result_set = self::query($sql); $obj_arr = array(); while ($row = self::fetch_array($result_set)) { $obj_arr[] = self::instantiate($row); } return $obj_arr; } } </code></pre> <p>Table object classL</p> <pre><code>class DomainPeer extends Database { public static function getDomainInfoList() { $sql = 'SELECT '; $sql .='d.`id`,'; $sql .='d.`name`,'; $sql .='d.`shortName`,'; $sql .='d.`created_at`,'; $sql .='d.`updated_at`,'; $sql .='count(q.id) as queries '; $sql .='FROM `domains` d '; $sql .='LEFT JOIN queries q on q.domainId = d.id '; $sql .='GROUP BY d.id'; return self::find_by_sql($sql); } .... } </code></pre> <p>I hope this example helps you create a good structure.</p>
 

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