MV framework logo
  • Architecture
  • Philosophy
  • Admin panel
  • Support
  • Feedback
Documentation
Download .zip version 3.2.0 from 25.12.2024
  • Architecture
  • Philosophy
  • Admin panel
  • Support
  • Feedback
Getting started
  • Installation and launch
  • Folder structure
  • Launching a simple website
  • Pre-installed models
  • SQLite getting started
  • System settings
  • Migrations
  • Debugging
Models
  • General principles of models
  • Data types
  • Model setup
  • Simple models
  • Foreign keys
  • Trees
  • Many to many
  • Group
  • Records management
  • Managing simple models
  • Additional features
Templates and routing
  • General principles of templates
  • Router object
  • MV object
  • Create a new template
  • Output of data in template
  • Record object
  • Files and images
  • Date and time
  • Redirects and http
  • Sending email
  • Special methods
Forms
  • Creating forms
  • Setting up form fields
  • Validating form fields
  • Form security
  • Working with form data
  • Using data from models
  • Form methods
SQL queries
  • Query builder
  • Direct queries
  • Pagination
  • Sorting
  • Filtration
Additional
  • AJAX
  • Plugins
  • Caching
  • Security
  • Admin panel add-ons
Documentation
Getting started
  • Installation and launch
  • Folder structure
  • Launching a simple website
  • Pre-installed models
  • SQLite getting started
  • System settings
  • Migrations
  • Debugging
Models
  • General principles of models
  • Data types
  • Model setup
  • Simple models
  • Foreign keys
  • Trees
  • Many to many
  • Group
  • Records management
  • Managing simple models
  • Additional features
Templates and routing
  • General principles of templates
  • Router object
  • MV object
  • Create a new template
  • Output of data in template
  • Record object
  • Files and images
  • Date and time
  • Redirects and http
  • Sending email
  • Special methods
Forms
  • Creating forms
  • Setting up form fields
  • Validating form fields
  • Form security
  • Working with form data
  • Using data from models
  • Form methods
SQL queries
  • Query builder
  • Direct queries
  • Pagination
  • Sorting
  • Filtration
Additional
  • AJAX
  • Plugins
  • Caching
  • Security
  • Admin panel add-ons
MV tracker

Sorting

When selecting the necessary records from the model table, you can sort the results in the desired order. If sorting is performed in only one direction, then the parameter described in the Query builder section is used.

//Sort by date, newest first
$rows = $mv -> events -> select(['order->desc' => 'date']);

//Sort by date, earliest first
$rows = $mv -> events -> select(['order->asc' => 'date']);

//The earliest record
$record = $mv -> events -> find(['order->asc' => 'date']);

Dynamic sorting

In case the user can sort the results of the selection on the site page, it is necessary to run the sorter. The sorting procedure is controlled by the Sorter class, inside the model an object of this class is created using the runSorter() method.

class Vacancy extends Model
{
    protected $name = 'Vacancies';

    protected $model_elements = [
        ['Activate', 'bool', 'active'],
        ['Name', 'char', 'name', ['required' => true, 'min_max_length' => '2,40']],
        ['Creation date', 'date', 'date'],
        ['Salary', 'int', 'salary', ['positive' => true]],
        ['Description', 'text', 'desc']
    ];
}

//Method for displaying a list of vacancies
public function display($parent, $current_brand)
{
    $rows = $this -> select(['active' => 1]);
    $html = '';
    
    foreach($rows as $row)
    {
        $html .= ...
    }
    
    return $html;
}

The sorter is usually launched from a template and takes a list of possible fields as input, and you can also pass a field and a default sort direction.

//Template file, for example views/view-vacancies.php

//Call without default sorting
$mv -> vacancy -> runSorter(['date', 'name', 'salary']);

//Initial sorting by date is set
$mv -> vacancy -> runSorter(['date', 'name', 'salary'], 'date', 'desc');

Next, we add the sorting parameters to the query for retrieving records.

public function display($parent, $current_brand)
{
    $params = $this -> sorter -> getConditions();
    $params['active'] = 1;
    $rows = $this -> select($params);
    ...
}

In the template, we display links that we can click to launch the desired sorting. The active link will be assigned the corresponding CSS class.

$path = $mv -> root_path.'vacancies';

echo $mv -> vacancy -> sorter -> displayLink('date', 'Date', $path);
echo $mv -> vacancy -> sorter -> displayLink('name', 'Job title', $path);
echo $mv -> vacancy -> sorter -> displayLink('salary', 'Salary', $path);

The example creates one link for each field for sorting in both directions. You can create a link for each type of sorting. The current link will be assigned the active class.

$path = $mv -> root_path.'vacancies';

//If you need to add pagination parameters
$path = $mv -> vacancy -> paginator -> addUrlParams($path);

echo $mv -> vacancy -> sorter -> displaySingleLink('name', 'asc', 'Name A-Z', $path);
echo $mv -> vacancy -> sorter -> displaySingleLink('name', 'desc', 'Name Z-A', $path);

//The default sorting is reversed
echo $mv -> vacancy -> sorter -> displayLink('salary', 'Salary', $path, 'reverse');

Methods of the Sorter object

  • getField(), getOrder() - return the current sorting field and sort order, respectively
  • getConditions() - returns sorting parameters for the query builder
  • setParams($field, $order) - sets the current sorting parameters
  • hasParams() - checks for the presence of sorting parameters, if the field and value are set, then returns true
  • getUrlParams() - returns GET parameters of the current sorting in the form ‘sort-field=price&sort-order=desc’
  • addUrlParams($path) - adds GET parameters of the form 'sort-field=name&sort-order=asc' to the passed URL. Usually used to transfer sorting parameters to other modules: pagination, filtering.
  • displayLink($field, $title [, $path, $reverse]), displaySingleLink($field, $order, $title [, $path]) - output of sorter links
  • getParamsForSQL() - returns part of SQL query like 'ORDER BY `date` DESC' with current sorting parameters

In some cases, the initial sorting order needs to be changed, for example when sorting by price of goods in the catalog, so that when clicking on the 'Sort by price' link, cheaper goods are at the beginning of the list. By default, the integer field is sorted in descending order, to make the primary sorting in ascending order, it is necessary to pass the fourth parameter to the displayLink method in the form of 'reverse'.

$path = $mv -> root_path.'catalog/34';
echo $mv -> products -> sorter -> displayLink('price', 'Sort by price', $path, 'reverse');

Previous section

Pagination

Next section

Filtration
MV workshop banner
MV tracker

© 2014-2025, MV framework team

MV tracker project Github