<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>BackendPro User Guide : Base Model</title>
<style type='text/css' media='all'>@import url('../userguide.css');</style>
<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='robots' content='all' />
</head>
<body>
<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"></div></div>
<div id="nav2"><a name="top"> </a></div>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>BackendPro User Guide Version 0.3.1</h1></td>
<td id="breadcrumb_right"><a href="../contents.html">Table of Contents</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->
<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="http://www.kaydoo.co.uk/projects/backendpro">BackendPro Home</a> ›
<a href="../index.html">User Guide Home</a> ›
Base Model
</td>
<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="kaydoo.co.uk/projects/backendpro/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
</tr>
</table>
<!-- END BREADCRUMB -->
<br clear="all" />
<!-- START CONTENT -->
<div id="content">
<h1>Base Model</h1>
<p>The Base Model Class provides you with basic functions to perform fetches/updates/inserts/deletes
on database tables. Instead of having huge model files with different functions for
updating each table, you can use a single line. What if that 'default' function
dosn't do what you want for a certain table? Well you can overwrite it.</p>
<p>Two new functions were added on 11/7/2008 by Leane Verhulst. 1. addRow - this fuction works just like the insert function except it returns the id for the new row instead of just a true/false status. 2. count_condition - this function will return a count of the number of rows in a table that match a passed in condition.</p>
<p class="important"><strong>Important:</strong> This class is initialized automatically by the system so there is no need to do it manually.</p>
<h2>Using the Base Model Class</h2>
<p>The Base Model Class is an abstract class, which means you only use it through
extending it. All you need to start using it is with following code:</p>
<code>
class Custom_model extends <kbd>Base_model</kbd>
<br />{
<br /> function Custom_model()
<br /> {
<br /> // Call parent constructor
<br /> parent::<kbd>Base_model</kbd>();
<br />
<br /> // Setup tables
<br /> <var>$this->_TABLES</var> = array(<kbd>'Table</kbd>' => <kbd>'complex_table_name</kbd>');
<br /> }
<br />}
</code>
<p>From the code you can see the variable <dfn>$this->_TABLES</dfn> being assigned
an array. This defines what table's your model has access to. It also means you can
assign a nice string to mean a complex table name. So lets see an example of this:</p>
<code>$this->custom_model->fetch(<var>'Table'</var>);</code>
<p>Here we are doing a default fetch on <var>Table</var>. The advantage of using
strings to represent the table name instead of using the actual table name is
so you won't have to remember possibly long complex table names.</p>
<p>As said before say you may want to implement further features than the basic functions
provide you with. Its possible in your model file to overwrite a certain action for a given
table. In the examples below <kbd>table</kbd> would be the table identifier you assigned for a given
table in the <dfn>$this->_TABLES</dfn> class variable.</p>
<ul>
<li><dfn>function _fetch_<kbd>table</kbd>(<var>fields</var>,<var>limit</var>,<var>where</var>){...}</dfn></li>
<li><dfn>function _insert_<kbd>table</kbd>(<var>data</var>){...}</dfn></li>
<li><dfn>function _update_<kbd>table</kbd>(<var>data</var>,<var>where</var>){...}</dfn></li>
<li><dfn>function _delete_<kbd>table</kbd>(<var>where</var>){...}</dfn></li>
<li><dfn>function _addRow_<kbd>table</kbd>(<var>data</var>){...}</dfn></li>
<li><dfn>function _count_condition_<kbd>table</kbd>(<var>where</var>){...}</dfn></li>
</ul>
<p>To call your newly created method you would just call the method as normal as explained below.</p>
<h2>Function References</h2>
<a name="fetch"></a>
<h3>$this->fetch()</h3>
<p>Fetch table rows:</p>
<code>$this->fetch('<var>Table</var>',<var>fields</var>,<var>limit</var>,<var>where</var>,<var>orderby</var>)</code>
<p>The first parameter is the Table string name you assigned in <dfn>$this->_TABLES</dfn>
for the table you wish to query.</p>
<p>The second <strong>optional</strong> parameter is the fields you wish returned.</p>
<p>The third <strong>optional</strong> parameter is an array defining the limit of
the query. An example array may be as follows: <kbd>array('rows'=>10,'offset'=>0)</kbd>.</p>
<p>The forth <strong>optional</strong> parameter is a where clause for which rows to
return.</p>
<p>The fifth <strong>optional</strong> parameter is an orderby clause that specifies the sort order.</p>
<a name="insert"></a>
<h3>$this->insert()</h3>
<p>Insert table rows:</p>
<code>$this->insert('<var>Table</var>',<var>data</var>)</code>
<p>The first parameter is the Table string name you assigned in <dfn>$this->_TABLES</dfn>
for the table you wish to query.</p>
<p>The second parameter is the data array you want to insert into the table.</p>
<a name="update"></a>
<h3>$this->update()</h3>
<p>Update table rows:</p>
<code>$this->update('<var>Table</var>',<var>data</var>,<var>where</var>)</code>
<p>The first parameter is the Table string name you assigned in <dfn>$this->_TABLES</dfn>
for the table you wish to query.</p>
<p>The second parameter is the data array you want to update the table with.</p>
<p>The third parameter is a where clause for which rows to update</p>
<a name="delete"></a>
<h3>$this->delete()</h3>
<p>Delete table rows:</p>
<code>$this->delete('<var>Table</var>',<var>where</var>)</code>
<p>The first parameter is the Table string name you assigned in <dfn>$this->_TABLES</dfn>
for the table you wish to query.</p>
<p>The second parameter is a where clause for which rows to delete.</p>
<a name="addRow"></a>
<h3>$this->addRow()</h3>
<p>Insert table row and return the id:</p>
<code>$this->addRow('<var>Table</var>',<var>data</var>)</code>
<p>The first parameter is the Table string name you assigned in <dfn>$this->_TABLES</dfn>
for the table you wish to query.</p>
<p>The second parameter is the data array you want to insert into the table.</p>
<a name="count_condition"></a>
<h3>$this->count_condition()</h3>
<p>Count the number of rows in a table that match a condition:</p>
<code>$this->count_condition('<var>Table</var>',<var>where</var>)</code>
<p>The first parameter is the Table string name you assigned in <dfn>$this->_TABLES</dfn>
for the table you wish to query.</p>
<p>The second parameter is a where clause for which rows to count.</p>
</div>
<!-- END CONTENT -->
<div id="footer">
<p>
<a href="#top">Top of Page</a> ·
<a href="../index.html">User Guide Home</a>
</p>
<p><a href="http://www.kaydoo.co.uk/projects/backendpro">BackendPro</a> · Copyright © 2008 · <a href="http://www.kaydoo.co.uk">Adam Price</a></p>
</div>
</body>
</html>