Skip to content

Commit

Permalink
add json order by (#728)
Browse files Browse the repository at this point in the history
* add json order by

* changelog
  • Loading branch information
nadar authored Aug 11, 2022
1 parent 1a07d86 commit f5c1960
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ In order to read more about upgrading and BC breaks have a look at the [UPGRADE
+ [#719](https://github.com/luyadev/luya-module-admin/pull/719) Prepare for PHP 8.1. Increased minium PHP Version to 7.4. Replaced `sizeg/jwt` with `bizley/jwt`.
+ [#726](https://github.com/luyadev/luya-module-admin/pull/726) The signature of `luya\admin\base\JwtIdentityInterface` has changed from: `loginByJwtToken(Lcobucci\JWT\Token $token)` to `loginByJwtToken(Lcobucci\JWT\Token\Plain $token)`
+ [#728](https://github.com/luyadev/luya-module-admin/pull/728) Added new `NgRestActiveQuery` method `jsonOrderBy()` to sort by a field which contains a json object.

## 4.4.1 (20. July 2022)

Expand Down
27 changes: 27 additions & 0 deletions src/ngrest/base/NgRestActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use yii\base\InvalidConfigException;
use yii\db\ActiveQuery;
use yii\db\Exception;
use yii\db\Expression;

/**
* NgRest Active Query.
Expand Down Expand Up @@ -74,6 +75,32 @@ public function jsonWhere($operator, $field, $key, $value)
{
return $this->andWhere([$operator, "JSON_EXTRACT({$field}, \"$.{$key}\")", $value]);
}


/**
* Order by a given json key inside a field.
*
* ```
* ->orderBy('title_json', 'de', 'asc')
* ```
*
* Assuming the database content of `title_json` looks like this:
*
* ```
* {'de': 'foobar', 'en' : 'foobar'}
* ```
*
* @param string $field The field name (attribute) which contains the json
* @param string $key The json key inside the json object to sort after
* @param string $direction Either `desc` or `asc`
* @see https://stackoverflow.com/a/70909842/4611030
* @return NgRestActiveQuery
* @since 4.5.0
*/
public function jsonOrderBy($field, $key, $direction)
{
return $this->orderBy(new Expression($field . '->"$.'.$key.'" ' . $direction));
}

/**
* Where condition for a field inside an array.
Expand Down
12 changes: 12 additions & 0 deletions tests/admin/ngrest/base/NgRestActiveQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ public function testI18nWhere()
], $query->i18nWhere('key', 'value')->where);
}

public function testJsonOrderBy()
{
new NgRestModelFixture([
'modelClass' => Lang::class,
]);

$query = new NgRestActiveQuery(Lang::class);
$exp = $query->jsonOrderBy('attribute', 'key', 'asc')->orderBy;

$this->assertSame('attribute->"$.key" asc', $exp[0]->expression);
}

public function testInPool()
{
new NgRestModelFixture([
Expand Down

0 comments on commit f5c1960

Please sign in to comment.