Skip to content

Commit

Permalink
Specify Sort fields (#85)
Browse files Browse the repository at this point in the history
* Specify Sort fields

See yiisoft/data#66

Co-authored-by: Aleksei Gagarin <roxblnfk@ya.ru>
  • Loading branch information
samdark and roxblnfk committed Dec 10, 2020
1 parent babc9b9 commit 2dffc42
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 14 deletions.
1 change: 0 additions & 1 deletion README.md
Expand Up @@ -20,7 +20,6 @@ Read more in the [official Cycle documentation](https://github.com/cycle/docs)
[![static analysis](https://github.com/yiisoft/yii-cycle/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/yii-cycle/actions?query=workflow%3A%22static+analysis%22)
[![type-coverage](https://shepherd.dev/github/yiisoft/yii-cycle/coverage.svg)](https://shepherd.dev/github/yiisoft/yii-cycle)


### Documentation

- [English](docs/en/README.md)
Expand Down
4 changes: 2 additions & 2 deletions docs/en/installation.md
@@ -1,8 +1,8 @@
# Installation

The preferred way to install this package is through [composer](http://getcomposer.org/download/):
The preferred way to install this package is through [Composer](https://getcomposer.org/download/):

```
```bash
composer require yiisoft/yii-cycle
```

Expand Down
8 changes: 4 additions & 4 deletions docs/en/select-data-reader.md
Expand Up @@ -86,8 +86,8 @@ Now we'll query for 20 latest published articles, then for 20 first articles.
$lastPublicReader = $articles->withLimit(20);

// Ordering is specified with Sort object:
$sort = (new \Yiisoft\Data\Reader\Sort([]))->withOrder(['published_at' => 'desc']);
// Note that neither Sort not SelectDataReader would not check field correctness.
$sort = (new \Yiisoft\Data\Reader\Sort(['published_at']))->withOrder(['published_at' => 'desc']);
// Note that SelectDataReader would not check Sort field correctness.
// Specifying non-existing fields would result in an error in Cycle code

// Don't forget about immutability when applying sorting rules
Expand Down Expand Up @@ -136,7 +136,7 @@ class ArticleRepository extends \Cycle\ORM\Select\Repository
*/
public function findPublic(): DataReaderInterface
{
$sort = (new Sort([]))->withOrder(['published_at' => 'desc']);
$sort = (new Sort(['published_at']))->withOrder(['published_at' => 'desc']);
return (new SelectDataReader($this->select()->where(['public' => true])))->withSort($sort);
}
}
Expand All @@ -149,7 +149,7 @@ function index(ArticleRepository $repository)
// Getting SelectDataReader
->findPublic()
// Applying new sorting
->withSort((new Sort([]))->withOrder(['published_at' => 'asc']));
->withSort((new Sort(['published_at']))->withOrder(['published_at' => 'asc']));
}
```
You may refine query conditions with filters. This filtering conditions are adding to original select query conditions, but NOT replace them.
Expand Down
4 changes: 2 additions & 2 deletions docs/ru/installation.md
@@ -1,8 +1,8 @@
# Установка

Предпочтительнее установить этот пакет через [composer](http://getcomposer.org/download/):
Предпочтительнее установить этот пакет через [Composer](https://getcomposer.org/download/):

```
```bash
composer require yiisoft/yii-cycle
```

Expand Down
10 changes: 5 additions & 5 deletions docs/ru/select-data-reader.md
Expand Up @@ -89,9 +89,9 @@ foreach ($paginator->read() as $article) {
$lastPublicReader = $articles->withLimit(20);

// Правила сортировки описываются в объекте класса Sort:
$sort = (new \Yiisoft\Data\Reader\Sort([]))->withOrder(['published_at' => 'desc']);
// Учтите, что ни объект Sort, ни SelectDataReader НЕ БУДУТ проверять правильность
// указанных полей! Указание несуществующих полей приведёт к ошибке в коде Cycle
$sort = (new \Yiisoft\Data\Reader\Sort(['published_at']))->withOrder(['published_at' => 'desc']);
// Учтите, что SelectDataReader НЕ БУДЕТ проверять правильность указанных в Sort полей!
// Указание несуществующих в таблице полей приведёт к ошибке в коде Cycle

// Применяем правила сортировки и не забываем об иммутабельности
$lastPublicReader = $lastPublicReader->withSort($sort);
Expand Down Expand Up @@ -136,7 +136,7 @@ class ArticleRepository extends \Cycle\ORM\Select\Repository
{
public function findPublic(): DataReaderInterface
{
$sort = (new Sort([]))->withOrder(['published_at' => 'desc']);
$sort = (new Sort(['published_at']))->withOrder(['published_at' => 'desc']);
// Параметры сортировки присваиваются объекту DataReader, а не \Cycle\ORM\Select
return (new SelectDataReader($this->select()->where(['public' => true])))->withSort($sort);
}
Expand All @@ -150,7 +150,7 @@ function index(ArticleRepository $repository)
// Получаем объект SelectDataReader
->findPublic()
// Применяем новое правило сортировки
->withSort((new Sort([]))->withOrder(['published_at' => 'asc']));
->withSort((new Sort(['published_at']))->withOrder(['published_at' => 'asc']));
}
```

Expand Down

0 comments on commit 2dffc42

Please sign in to comment.