Working with table rows and the elxisDbTable class
elxisDbTable is an abstact class that allows you to easily handle database table rows as objects. elxisDbTable extensions for the built-in extensions are stored inside the includes/libraries/elxis/database/tables/ folder. For the third party extensions they can be placed anywhere in Elxis filesystem. Moreover Elxis provides cross-database table definition system.
elxisDbTable
The most important methods of the elxisDbTable class
load
Load a table row as object by providing the value of the primary key column ($id)
public functionload($id)
// Example: Load the category with catid = 4
$row = newcategoriesDbTable();
$row->load(4);
bind
Bind an array (usually $_POST) to elxisDbTable object
public functionbind($array)
// Example:
$row = newcategoriesDbTable();
$row->bind($_POST);
getTable
Get the database table name
public functiongetTable()
// Example:
$row = newcategoriesDbTable();
$row->getTable();
//will return #__categories
getColumns
Get the columms of the current table name as array
public functiongetColumns()
getPrimaryKey
Get the name of the table's primary key column
public functiongetPrimaryKey()
// Example:
$row = newcategoriesDbTable();
$primary_key = $row->getPrimaryKey();
//will return catid
forceNew
Force new row. Be careful for dublicated entries if $reset_primary is false
public functionforceNew($reset_primary=false)
store
Insert or update table row. For new rows an insert is executed, for existing ones an update.
public functionstore()
insert
Insert a new row into the database.
public functioninsert()
// Example: Insert a new row in table #__acl
$row = newaclDbTable();
$row->category = 'module';
$row->element = 'mod_sample';
$row->identity = 4;
$row->action = 'view';
$row->gid = 0;
$row->uid = 0;
$row->aclvalue = 1;
$row->insert();
update
Update a row.
public functionupdate()
// Example: Load the category with catid = 4, change its title and save it into the database
$row = newcategoriesDbTable();
$row->load(4);
$row->title = 'New title';
$row->update();
copy
Copy current row and save it into the database with a new primary key.
public functioncopy()
delete
Delete current row or any row by providing the primary key value.
public functiondelete($id='')
move
Move (re-order) a row one position UP or DOWN. Returns true on success, false on error.
public functionmove($inc, $wheres=array())
$inc should be -1 to move row up and 1 to move it down. $wheres is an array of conditional statements to be passed in the SQL query.
Each element of this array is an array with 3 arguments:
- Column name
- Operator (available options: =, >, >=, <, <= and LIKE)
- Value
The AND operator will be used to join the $wheres elements.
public functionreorder($wheres=array(), $fixall=false)
check
Checks the validity of the row column values before an insert or update and returns true or false.
protected functioncheck()
getErrorMsg
Returns table's or database's last error message.
public functiongetErrorMsg()
Extending elxisDbTable
You can create extensions of the elxisDbTable like this:
classcustomDbTableextendselxisDbTable {
public function__construct() {}
public functioncheck() {}
}
Note the DbTable suffix in the class name. The constructor of the customDbTable class should set the table name and table primary key in the parent class as well as define the table columns. The class can also have a check method that will be called automatically by the parent class during insert/update/store to validate the data before trying to save them.
Defining table columns
In the constructor of the extension class you can define the table columns by providing an array to the columns property. This array must have as keys the column name and as values an array with the column type and initial value. The supported column types are the following:
integer For table columns of type integer (any size) (PDO::PARAM_INT)
bit For columns of type integer having as value 0 or 1 (PDO::PARAM_INT)
numeric For numeric columns, such as floats. (PDO::PARAM_STR)
string For table columns containing strings as values such as VARCHAR. (PDO::PARAM_STR)
text For table columns containing text as values such as TEXT/BLOB. (PDO::PARAM_LOB)
binary For table columns containing binary data (PDO::PARAM_LOB)