ADBTable
in component
Provides declaration of tables and their structure for easy inserting/updating operations.
It was mostly made for easy form creation. The class introduces an additional abstraction level between models and database functions to avoid writing sql queries directly.
Basic rules:
- all class properties (not starting with $_) are treated as table values. Methods 'update','save','insert', etc. submit all these fields.
- internal variables are started with '_'
Here is a typical class declaration for a table. Only fields declared in the class will be affected by insert/update operations.
namespace extension\ka_extensions\ka_multivendor\dbtable;
use extension\ka_extensions\ADBTable as ADBTable;
class Customer extends ADBTable {
const TABLE_NAME = 'customer';
public function getFields() {
$fields = array(
'customer_id' => [
'primary_key' => true,
],
'firstname' => [],
'lastname' => [],
'email' => [],
);
return $fields;
}
}
Table of Contents
Constants
- TABLE_NAME = ''
- This constant should be overwritten in inhertior. Contains a table name without DB_PREFIX like 'product'.
Methods
- __construct() : mixed
- delete() : mixed
- Executes a deletion operation against the table with the specified condition.
- empty() : mixed
- Unsets defined values (without updating the database) for starting a new record.
- getEmptyRecord() : mixed
- Returns an empty record (for a new form).
- getFields() : mixed
- Returns a list of fields from the table.
- getPrimaryKeys() : mixed
- returns an array of primary key fields from the db declaration table.
- getTableName() : mixed
- getValues() : mixed
- Return already defined values
- hasPrimaryRecord() : mixed
- Checks if the primary record already exists. The primary keys are taken from the fields declaration.
- insert() : mixed
- The method inserts assigned values to the database.
- query() : mixed
- Executes a selection query for the table. When any fields are set, they are used as a condition.
- setValues() : mixed
- Set a batch of values at once from an array.
- update() : mixed
- The method updates database with assigned values.
Constants
TABLE_NAME
This constant should be overwritten in inhertior. Contains a table name without DB_PREFIX like 'product'.
public
mixed
TABLE_NAME
= ''
Methods
__construct()
public
__construct() : mixed
delete()
Executes a deletion operation against the table with the specified condition.
public
delete([mixed $where = null ]) : mixed
When the condition is not passed, the condition is generated from fields assigned to the table class.
Parameters
- $where : mixed = null
empty()
Unsets defined values (without updating the database) for starting a new record.
public
empty() : mixed
getEmptyRecord()
Returns an empty record (for a new form).
public
getEmptyRecord() : mixed
getFields()
Returns a list of fields from the table.
public
getFields() : mixed
This is an optional method redeclared in child classes. A user may work with any fields by default, but it is possible to restrict access by these fields only.
getPrimaryKeys()
returns an array of primary key fields from the db declaration table.
public
getPrimaryKeys() : mixed
getTableName()
public
getTableName() : mixed
getValues()
Return already defined values
public
getValues() : mixed
hasPrimaryRecord()
Checks if the primary record already exists. The primary keys are taken from the fields declaration.
public
hasPrimaryRecord() : mixed
insert()
The method inserts assigned values to the database.
public
insert([mixed $is_replace = false ]) : mixed
Parameters: $is_replace - tells the function to use 'REPLACE' operation instead of 'INSERT'. Default value = false.
Returns: record identifier. (internally it uses $db->getLastId())
Parameters
- $is_replace : mixed = false
query()
Executes a selection query for the table. When any fields are set, they are used as a condition.
public
query() : mixed
setValues()
Set a batch of values at once from an array.
public
setValues(mixed $values) : mixed
Returns: none.
Parameters
- $values : mixed
update()
The method updates database with assigned values.
public
update([mixed $where = null ]) : mixed
Parameters: $where - array or string
Returns: result of update operation.
Parameters
- $where : mixed = null