Implement DataTables with FuelPHP's ORM
$ composer require synergitech/fuel-datatables
You need to call the setAllowedColumns()
function to define which columns can be accessed and returned.
class API extends Controller_Rest
{
public function get_index()
{
$datatable = \SynergiTech\DataTables\DataTable::fromGet(\Model\YourModel::class);
$datatable->setAllowedColumns([
'id',
'uuid',
'name',
'related_model.name',
'created_at',
'updated_at'
]);
return $this->response($datatable->getResponse());
}
}
If you wish to do more complex ORM queries, you can simply call the getQuery()
function which will return the FuelPHP ORM's query object, which you can then manipulate as you need to.
$datatable->getQuery()
->where('id', ">", 0)
->related("group")
->where("group.name", "!=", "guests")
->related("another_relation");
You can provide custom callbacks that will be executed for each row to be returned in the response. You can use this to manipulate each row in the response.
$datatable->addRowFormatter(function ($model, $outputRow) {
$outputRow['example'] = count($model->a_many_relation);
return $outputRow;
});
To make it easier for you to manage filtering your output, you can ask for all or specific rows to be encoded on output. By default, we leave XSS filtering up to you.
$datatable->setEscapedColumns();
With exceptions:
$datatable->setEscapedColumns()
->setRawColumns(['html_body']);
$datatable->setEscapedColumns(['id', 'slug']);
$datatable->setEscapedColumns([]);
- FuelPHP ORM (SynergiTech Fork)
- jQuery
- DataTables