DataTable
extends Ext
in package
This class let's you define the structure of a database, in order for it to be read and the data returned to DataTables.
Typically you will:
- Create the instance
- Define the columns
- Process the request
- Return JSON to the client-side
You may also wish to add query conditions, or provide extra pre-column options for features such as ColumnControl.
Tags
Table of Contents
Properties
- $version : string
Methods
- __construct() : mixed
- Constructor.
- column() : mixed
- Get / set a column instance.
- columns() : mixed
- Get / set the columns for the table.
- data() : array<string|int, mixed>
- Get the data constructed in this instance.
- db() : mixed
- Get / set the DB connection instance.
- debug() : mixed
- Get / set debug mode and set a debug message.
- idPrefix() : mixed
- Get / set the DOM prefix.
- inst() : static
- Static method to instantiate a new instance of a class (shorthand of 'instantiate').
- instantiate() : static
- Static method to instantiate a new instance of a class.
- join() : mixed
- Get / set join instances. Note that for the majority of use cases you will want to use the `leftJoin()` method. It is significantly easier to use if you are just doing a simple left join!
- json() : mixed
- Get the JSON for the data constructed in this instance.
- leftJoin() : $this
- Add a left join condition to the Editor instance, allowing it to operate over multiple tables. Multiple `leftJoin()` calls can be made for a single Editor instance to join multiple tables.
- pkey() : mixed
- Get / set the primary key.
- process() : $this
- Process a request from the client-side to get / set data.
- table() : mixed
- Get / set the table name.
- where() : mixed
- Where condition to add to the query used to get data from the database.
- _getSet() : mixed
- Common getter / setter function for DataTables classes.
- _propExists() : bool
- Determine if a property is available in a data set (allowing `null` to be a valid value).
- _proxy() : mixed
- Execute a method from a given instance. If the return is that instance, then return self instead, otherwise give the returned value.
- _readProp() : mixed
- Read a value from a data structure, using Javascript dotted object notation. This is the inverse of the `_writeProp` method and provides the same support, matching DataTables' ability to read nested JSON data objects.
- _writeProp() : mixed
- Write the field's value to an array structure, using Javascript dotted object notation to indicate JSON data structure. For example `name.first` gives the data structure: `name: { first: ... }`. This matches DataTables own ability to do this on the client-side, although this doesn't implement implement quite such a complex structure (no array / function support).
Properties
$version
public
string
$version
= \DataTables\Editor::VERSION
Methods
__construct()
Constructor.
public
__construct([Database $db = null ][, string|array<string|int, mixed> $table = null ][, string|array<string|int, mixed> $pkey = null ]) : mixed
Parameters
- $db : Database = null
-
An instance of the DataTables Database class that we can use for the DB connection. Can be given here or with the 'db' method.
- $table : string|array<string|int, mixed> = null
-
The table name in the database to read and write information from and to. Can be given here or with the 'table' method.
- $pkey : string|array<string|int, mixed> = null
-
Primary key column name in the table given in the $table parameter. Can be given here or with the 'pkey' method.
column()
Get / set a column instance.
public
column([Column|string $_ = null ]) : mixed
Parameters
- $_ : Column|string = null
-
The name of the column to get the instance of, or the column instance to add.
Return values
mixed —Column, or self for chaining if used as a setter.
columns()
Get / set the columns for the table.
public
columns([Column|array<string|int, Column> $_ = null ]) : mixed
Parameters
- $_ : Column|array<string|int, Column> = null
-
Instances of the Column class, given as a single instance of Column, an array of Column instances, or multiple Column instance parameters for the function.
Tags
Return values
mixed —Array of columns.
data()
Get the data constructed in this instance.
public
data() : array<string|int, mixed>
This will get the PHP array of data that has been constructed for the command that has been processed by this instance. Therefore only useful after process has been called.
Return values
array<string|int, mixed> —Processed data array.
db()
Get / set the DB connection instance.
public
db([Database $_ = null ]) : mixed
Parameters
- $_ : Database = null
-
DataTable's Database class instance to use for database connectivity. If not given, then used as a getter.
Return values
mixed —The Database connection instance if no parameter is given.
debug()
Get / set debug mode and set a debug message.
public
debug([bool|mixed $_ = null ][, string $path = null ]) : mixed
It can be useful to see the SQL statements that Editor is using. This
method enables that ability. Information about the queries used is
automatically added to the output data array / JSON under the property
name debugSql.
This method can also be called with a string parameter, which will be added to the debug information sent back to the client-side. This can be useful when debugging event listeners, etc.
Parameters
- $_ : bool|mixed = null
-
Debug mode state. If not given, then used as a getter. If given as anything other than a boolean, it will be added to the debug information sent back to the client.
- $path : string = null
-
Set an output path to log debug information
Return values
mixed —Debug mode state if no parameter is given.
idPrefix()
Get / set the DOM prefix.
public
idPrefix([string $_ = null ]) : mixed
Typically primary keys are numeric and this is not a valid ID value in an HTML document - is also increases the likelihood of an ID clash if multiple tables are used on a single page. As such, a prefix is assigned to the primary key value for each row, and this is used as the DOM ID.
Parameters
- $_ : string = null
-
Primary key's name. If not given, then used as a getter.
Return values
mixed —Primary key value if no parameter is given.
inst()
Static method to instantiate a new instance of a class (shorthand of 'instantiate').
public
static inst() : static
This method performs exactly the same actions as the 'instantiate' static method, but is simply shorter and easier to type!
Return values
static —class
instantiate()
Static method to instantiate a new instance of a class.
public
static instantiate() : static
A factory method that will create a new instance of the class that has extended 'Ext'. This allows classes to be instantiated and then chained - which otherwise isn't available until PHP 5.4. If using PHP 5.4 or later, simply create a 'new' instance of the target class and chain methods as normal.
Return values
static —Instantiated class
join()
Get / set join instances. Note that for the majority of use cases you will want to use the `leftJoin()` method. It is significantly easier to use if you are just doing a simple left join!
public
join([Join $_ = null ]) : mixed
Parameters
- $_ : Join = null
-
Instances of the Join class, given as a single instance of Join, an array of Join instances, or multiple Join instance parameters for the function.
Return values
mixed —Array of joins.
json()
Get the JSON for the data constructed in this instance.
public
json([bool $print = true ][, int $options = 0 ]) : mixed
Basically the same as the Editor->data() method, but in this case we echo, or return the JSON string of the data.
Parameters
- $print : bool = true
-
Echo the JSON string out (true, default) or return it (false).
- $options : int = 0
-
JSON encode option https://www.php.net/manual/en/json.constants.php
Return values
mixed —JSON representation of the processed data if false is given as the first parameter.
leftJoin()
Add a left join condition to the Editor instance, allowing it to operate over multiple tables. Multiple `leftJoin()` calls can be made for a single Editor instance to join multiple tables.
public
leftJoin(string $table, string $field1[, string $operator = null ][, string $field2 = null ]) : $this
Parameters
- $table : string
-
Table name to do a join onto
- $field1 : string
-
Field from the parent table to use as the join
- $operator : string = null
-
Join condition (
=, '<`, etc) - $field2 : string = null
-
Field from the child table to use as the join
Return values
$thispkey()
Get / set the primary key.
public
pkey([string|array<string|int, string> $_ = null ]) : mixed
The primary key must be known uniquely identify each row.
Parameters
- $_ : string|array<string|int, string> = null
-
Primary key's name. If not given, then used as a getter. An array of column names can be given to allow composite keys to be used.
Return values
mixed —Primary key value if no parameter is given.
process()
Process a request from the client-side to get / set data.
public
process([array<string|int, mixed> $data = [] ]) : $this
Parameters
- $data : array<string|int, mixed> = []
-
Typically $_POST or $_GET if used with server-side processing mode, but is not required (client-side processing).
Return values
$thistable()
Get / set the table name.
public
table([string|array<string|int, mixed> $_ = null ]) : mixed
The table name designated which DB table will be used as its data source
for working with the database. Table names can be given with an alias,
which can be used to simplify larger table names. The field names would
also need to reflect the alias, just like an SQL query. For example:
users as a.
Parameters
- $_ : string|array<string|int, mixed> = null
-
Table names given as a single string, an array of strings or multiple string parameters for the function.
Return values
mixed —Array of tables names.
where()
Where condition to add to the query used to get data from the database.
public
where([string|callable(Query): void $key = null ][, string $value = null ][, string $op = '=' ]) : mixed
Can be used in two different ways:
- Simple case:
where( field, value, operator ) - Complex:
where( fn )
The simple case is fairly self explanatory, a condition is applied to the
data that looks like field operator value (e.g. name = 'Allan'). The
complex case allows full control over the query conditions by providing a
closure function that has access to the database Query that is being
using, so you can use the where(), or_where(), and_where() and
where_group() methods as you require.
Parameters
- $key : string|callable(Query): void = null
-
Single field name or a closure function
- $value : string = null
-
Single field value.
- $op : string = '='
-
Condition operator: <, >, = etc
Return values
mixed —Where condition array.
_getSet()
Common getter / setter function for DataTables classes.
protected
_getSet(mixed &$prop, mixed $val[, bool $array = false ]) : mixed
This getter / setter method makes building getter / setting methods easier, by abstracting everything to a single function call.
Parameters
- $prop : mixed
-
The property to set
- $val : mixed
-
The value to set - if given as null, then we assume that the function is being used as a getter.
- $array : bool = false
-
Treat the target property as an array or not (default false). If used as an array, then values passed in are added to the $prop array.
_propExists()
Determine if a property is available in a data set (allowing `null` to be a valid value).
protected
_propExists(string $name, array<string|int, mixed> $data) : bool
Parameters
- $name : string
-
Javascript dotted object name to write to
- $data : array<string|int, mixed>
-
Data source array to read from
Return values
bool —true if present, false otherwise
_proxy()
Execute a method from a given instance. If the return is that instance, then return self instead, otherwise give the returned value.
protected
_proxy(mixed $inst, string $method, array<string|int, mixed> $args) : mixed
Parameters
- $inst : mixed
-
Class instance
- $method : string
-
Method to execute
- $args : array<string|int, mixed>
-
The arguments from the function
Return values
mixed —Value from the executed function or self.
_readProp()
Read a value from a data structure, using Javascript dotted object notation. This is the inverse of the `_writeProp` method and provides the same support, matching DataTables' ability to read nested JSON data objects.
protected
_readProp(string $name, array<string|int, mixed> $data) : mixed
Parameters
- $name : string
-
Javascript dotted object name to write to
- $data : array<string|int, mixed>
-
Data source array to read from
Return values
mixed —The read value, or null if no value found.
_writeProp()
Write the field's value to an array structure, using Javascript dotted object notation to indicate JSON data structure. For example `name.first` gives the data structure: `name: { first: ... }`. This matches DataTables own ability to do this on the client-side, although this doesn't implement implement quite such a complex structure (no array / function support).
protected
_writeProp(array<string|int, mixed> &$out, string $name, mixed $value) : mixed
Parameters
- $out : array<string|int, mixed>
-
Array to write the data to
- $name : string
-
Javascript dotted object name to write to
- $value : mixed
-
Value to write