2
0
mirror of https://github.com/opnsense/docs synced 2024-10-30 21:20:20 +00:00

Development manual / Frontend / Controllers - explain the use of searchRecordsetBase()

This commit is contained in:
Ad Schellevis 2023-05-09 17:00:17 +02:00
parent 7b15bae3ad
commit 4fa1982188

View File

@ -158,3 +158,52 @@ post request to /api/sample/test/echo, using jQuery:
and are named :code:`ApiMutableModelControllerBase`, :code:`ApiMutableServiceControllerBase`. Both extend :code:`ApiControllerBase`
as described in this chapter. The mutable model controller is explained in more detail in :doc:`using grids <../examples/using_grids>`, the
service controller is explained in :doc:`api enable services <../examples/api_enable_services>`
--------------------------------------------------
Searchable recordsets
--------------------------------------------------
The tip in the previous chapter described how to use grids when using models, but in some cases there are datasets
without being bound to a model. For example when traversing legacy data or gathering system statistics.
For this reason we added the method :code:`searchRecordsetBase()` in :code:`ApiControllerBase`.
Using this method offers the ability to hook a recordset into the same search functionality as being available
in model grids.
The following parameters are being offered:
================== ===========================================================================================
Name Description
================== ===========================================================================================
$records array as record set, e.g. [ ['id' => '1'], ['id' => '2'], ... ]
$fields Optional list of fields when not all data should be returned
$defaultSort Optional default sort order (fielndname in recordset)
$filter_funct Optional pluggable filter function, which is call with the record in question
$sort_flags Default set to :code:`SORT_NATURAL | SORT_FLAG_CASE`
================== ===========================================================================================
.. Note::
In order to filter sets on fields, make sure all records contain the requested field. Currently it's not possible
to omit fields when being sorted.
Implementing this into your own controller should be as simple as:
.. code-block:: php
class TestController extends ApiControllerBase
{
/**
* @return array
*/
public function searchAction()
{
$records = [];
$records[] = ['id' => '1', 'description' => 'test 1'];
$records[] = ['id' => '2', 'description' => 'test 2'];
$records[] = ['id' => '3', 'description' => 'test 3'];
return $this->searchRecordsetBase($records);
}
}