Installing
There are two primary methods by which you can install the DataTables PHP libraries:
- With
composer - Direct inclusion
Composer installation
Composer is a dependency and package manager for PHP which can be used to easily manage and install external libraries such as DataTables's PHP libraries. Install the DataTables PHP libraries using:
composer install datatables.net/editor-php
Then, in your PHP files ensure that you include require 'vendor/autoload.php'; as normal with composer to have it auto load the classes needed.
Database connection
A connection to the database is made using the DataTables\Database class and passing in information about the database type and the connection reference, or if you already have a PDO connection, you can use that:
$db = new DataTables\Database([
'type' => 'Mysql',
'pdo' => $database
]);
If you don't have an existing PDO connection that can be used, the options array for the DataTables\Database class will accept parameters for user name, password, etc:
$db = new DataTables\Database([
'type' => '', // Database type: 'Mysql', 'Postgres', 'Sqlserver', 'Sqlite'
'user' => '', // User name
'pass' => '', // Password
'host' => '', // Database server
'port' => '', // Database port (can be left empty for default)
'db' => '', // Database name
]);
Composer example
Once you have the database connection configured, you can use the DataTables classes (such as DataTable and Editor) to define how you want the script to interact with the database. The code below shows the use of an Editor class to setup up the reading and writing to the database. See the documentation for each class for more details on how they are used and configured.
<?php
require 'vendor/autoload.php';
// Alias namespaces for easy access
use
DataTables\Database,
DataTables\Editor,
DataTables\Editor\Field;
// New database connection
$db = new Database( array(
"type" => "Postgres",
// ... connection parameters
) );
// Build our Editor instance and process the data coming from $_POST
Editor::inst( $db, 'myDatabase' )
->fields(
Field::inst( 'first_name' ),
// ...
)
->process( $_POST )
->json();
Direct inclusion
Download the Editor demo app for PHP from the Editor download page and copy the PHP files (maintaining the directory structure) to your own server. Alternatively, you can download the library files from the GitHub repository.
Once downloaded and copied into your project, require the DataTables.php file:
require 'lib/DataTables.php';
You will then be able to create a database connection as shown in the Composer examples above.
As you won't want to create a new database object in each route (too much repetition of code!) you might want to store your database connection in a common file and include that. The DataTables PHP libraries will automatically look for a file called config.php and include that for this purpose. It should contain a variable called $sql_details which contains the database connection array described above.
PHP paradigms
There a couple of key programming concepts that the DataTables PHP libraries make use of and it is important to understand them in this context. If you are already comfortable with the ideas of chaining and namespaces in PHP skip this section, otherwise read on for a summary of these PHP features, as they are used above and also in the Editor examples.
::inst
All of the classes that can be instantiated in the DataTables PHP libraries can use a static inst factory method. This creates a new instance and allows you to chain methods with no extra syntax. Alternatively, if you prefer, you can create a new instance of a class using new. The following are all equivalent:
// PHP 8.4+
$editor = new Editor( $db, 'staff' )
->fields( ... )
->process( ... )
->json();
// PHP 8.3 and earlier (note the extra parenthesis)
$editor = (new Editor( $db, 'staff' ))
->fields( ... )
->process( ... )
->json();
// All versions
$editor = Editor::inst( $db, 'staff' )
->fields( ... )
->process( ... )
->json();
// All versions
$editor = new Editor( $db, 'staff' );
$editor
->fields( ... )
->process( ... )
->json();
For compatibility, the documentation and examples for DataTables's PHP libraries will use the ::inst() static method, but keep in mind that whenever you see X::inst() you could substitute new X(), which is likely preferable if you are using PHP 8.4 or newer.
Chaining
Like the DataTables and Editor Javascript API's the DataTables PHP API is fully chainable, allowing potentially complex code to be expressed succinctly. The majority of the methods that the configuration classes provide are chainable - i.e. they return the instance that you are working with. Consider for example:
$editor = Editor::inst( $db, 'staff' );
$editor->fields( ... );
$editor->process( ... );
$editor->json();
Using chained style, the above code block can be rewritten as:
Editor::inst( $db, 'staff' )
->fields( ... )
->process( ... )
->json();
Using a chained style of coding is not required, although you will see it used in this documentation and the examples.
Namespaces
The DataTables PHP libraries make use of PHP namespaces. Namespaces are used in PHP to package software and reduce the chance of a name collision, for example, Editor is a relatively common name and other software that you include in your application might also define an Editor class. Namespacing resolves this issue.
You can use the namespaces directly:
$editor = new \DataTables\Editor( $db, 'staff' );
However, obviously this involves extra typing, particularly when you have a lot of fields which also need to have their namespace specified. In PHP it is possible to shorten the code to just the class name by using PHP's use keyword.
In the examples you will typically see:
// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate;
This means that rather than typing DataTables\Editor or DataTables\Editor\Field every time we want an Editor or Field instance, we can just use Editor or Field respectively. So the above simple example becomes:
// DataTables PHP library
include( "lib/DataTables.php" );
// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate;
$editor = Editor::inst( $db, 'staff' );
Sources
If you are interested in using the PHP libraries directly from its git repository, you can find it on Github. The DataTables PHP source is released under the MIT license.