# BackendPro User Guide Version 0.3.1

Search User Guide

# Base Model

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.

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.

Important: This class is initialized automatically by the system so there is no need to do it manually.

## Using the Base Model Class

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:

`class Custom_model extends `Base_model`
{
function Custom_model()
{
// Call parent constructor
parent::`Base_model`();
// Setup tables
$this->_TABLES = array(`'Table`' => `'complex_table_name`');
}
}`

From the code you can see the variable $this->_TABLES 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:

`$this->custom_model->fetch('Table');`

Here we are doing a default fetch on Table. 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.

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 `table` would be the table identifier you assigned for a given table in the $this->_TABLES class variable.

- function _fetch_`table`(fields,limit,where){...}
- function _insert_`table`(data){...}
- function _update_`table`(data,where){...}
- function _delete_`table`(where){...}
- function _addRow_`table`(data){...}
- function _count_condition_`table`(where){...}

To call your newly created method you would just call the method as normal as explained below.

## Function References

### $this->fetch()

Fetch table rows:

`$this->fetch('Table',fields,limit,where,orderby)`

The first parameter is the Table string name you assigned in $this->_TABLES for the table you wish to query.

The second optional parameter is the fields you wish returned.

The third optional parameter is an array defining the limit of the query. An example array may be as follows: `array('rows'=>10,'offset'=>0)`.

The forth optional parameter is a where clause for which rows to return.

The fifth optional parameter is an orderby clause that specifies the sort order.

### $this->insert()

Insert table rows:

`$this->insert('Table',data)`

The first parameter is the Table string name you assigned in $this->_TABLES for the table you wish to query.

The second parameter is the data array you want to insert into the table.

### $this->update()

Update table rows:

`$this->update('Table',data,where)`

The first parameter is the Table string name you assigned in $this->_TABLES for the table you wish to query.

The second parameter is the data array you want to update the table with.

The third parameter is a where clause for which rows to update

### $this->delete()

Delete table rows:

`$this->delete('Table',where)`

The first parameter is the Table string name you assigned in $this->_TABLES for the table you wish to query.

The second parameter is a where clause for which rows to delete.

### $this->addRow()

Insert table row and return the id:

`$this->addRow('Table',data)`

The first parameter is the Table string name you assigned in $this->_TABLES for the table you wish to query.

The second parameter is the data array you want to insert into the table.

### $this->count_condition()

Count the number of rows in a table that match a condition:

`$this->count_condition('Table',where)`

The first parameter is the Table string name you assigned in $this->_TABLES for the table you wish to query.

The second parameter is a where clause for which rows to count.