Loading data with KenedoModel::getRecords

You have seen how to load a specific record by ID using KenedoModel::getRecord(), here we talk about getting multiple records with filters and ordering. For use in paginated views, you can set start and limit.

Quick example:

$model = KenedoModel::getModel('ConfigboxModelProducts'); // Returns data of all ConfigBox products with no filters or specific ordering. $products = $model->getRecords(); // with all parameters (see descriptions below) $products = $model->getRecords($filters, $pagination, $ordering, $languageTag);

 

Parameters

Filtering

First parameter is $filters. It defaults to an empty array and makes the method return any records it finds.

Syntax:

As array keys you put in the property names to filter for and the array values are the values for the corresponding property. Your array value can be an array as well to filter for one or the other value.

Examples:

  • $filter = array( 'published' => '1' ) searches for records with property 'published' being '1'.

  • $filter = array( 'some_property' => array('A', 'B') ) searches for records with 'some_property' being 'A' or 'B'. An empty array gets ignored.

  • $filter = array( 'prop_a' => 1, 'prop_b' => 2) searches for records with prop_a = 1 AND prop_b = 2

Notes:

  • Query escaping is done within getRecords, you do not have to escape values yourself.

Pagination

Pagination lets you slice the results for easy use in paginated output.

Syntax:

$pagination is an array with two key/value pairs ('start' and 'limit'), just as MySQL takes it.

Examples:

  • $pagination = array('limit'=>20, 'start'=>0) to show 20 records starting with the first record

Notes:

  • 'start' is zero based, start 10 would make you start with the eleventh record

Ordering

You can order the returned records by one or more columns.

Syntax:

$ordering is an array or arrays with ordering instructions. Each ordering instruction array has key 'propertyName' and 'direction' ('ASC' or 'DESC').

Examples:

  • $ordering = array( array('propertyName' => 'ordering', 'direction' => 'ASC') ) sorts the records by property 'ordering' ascending

  • $ordering = array( array('propertyName' => 'title', 'direction' => 'ASC'), array('propertyName' => 'sub_title', 'direction' => 'ASC')) sorts by title first and sub_title second.

Language

You can choose translatable data to show in a specific language or leave the $language parameter empty to use the user's current language.

$languageTag has the form 'en-GB'. 

Return value

You get an array of stdClass objects. The structure is the same as described in KenedoModel::getRecord().

Complete Example:

Loading all products that are published and have 'Car' in their title and order them by title.

$model = KenedoModel::getModel('ConfigboxModelProducts'); $filters = array('published' => '1', 'title' => 'Car'); $ordering = array( array('propertyName'=>'title', 'direction' => 'ASC')); $products = $model->getRecords($filters, array(), $ordering);