Skip to content

Commit

Permalink
Merge pull request #8 from ghonijee/development
Browse files Browse the repository at this point in the history
Update Feature SortBy Relation data
  • Loading branch information
ghonijee authored Nov 4, 2021
2 parents bdd0ec4 + 2b586aa commit 5936c15
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 16 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ All notable changes to `QueryAdapter` will be documented in this file

## 1.0.4-Beta - 2021-11-03

- Fixing Bug Pagination Request
- Fixing Bug Pagination Request

## 1.0.5-Beta - 2021-11-04

- Bug Fixed sorted by relation data
2 changes: 1 addition & 1 deletion Models/TestComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class TestComment extends Model

public function test()
{
return $this->belongsTo(TestModel::class);
return $this->belongsTo(TestModel::class, 'test_model_id', 'id');
}
}
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Laravel Query Adapter

[![Latest Version on Packagist](https://img.shields.io/packagist/v/ghonijee/laravel-query-adapter.svg?style=flat-square)](https://packagist.org/packages/ghonijee/query-adapter)
[![Total Downloads](https://img.shields.io/packagist/dt/ghonijee/laravel-query-adapter.svg?style=flat-square)](https://packagist.org/packages/ghonijee/query-adapter)
[![Latest Version on Packagist](https://img.shields.io/packagist/v/ghonijee/query-adapter.svg?style=flat-square)](https://packagist.org/packages/ghonijee/query-adapter)
[![Total Downloads](https://img.shields.io/packagist/dt/ghonijee/query-adapter.svg?style=flat-square)](https://packagist.org/packages/ghonijee/query-adapter)
[![Actions Status](https://github.com/ghonijee/laravel-query-adapter/actions/workflows/main.yml/badge.svg)](https://github.com/ghonijee/laravel-query-adapter/actions)

## Installation
Expand All @@ -15,10 +15,13 @@ composer require ghonijee/query-adapter
## Usage

```php
// Use Facade
$data = QueryAdapter::for(TestModel::class)->get();
// or
$data = QueryAdapter::load(TestModel::query())->get();
//or


//or Use Class DxAdapter
$data = DxAdapter::for(TestModel::class)->get();
// or
$data = DxAdapter::load(TestModel::query())->get();
Expand Down
31 changes: 30 additions & 1 deletion src/Builders/SortByQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
use GhoniJee\DxAdapter\Data\SortData;
use GhoniJee\DxAdapter\SortClass\BuilderSortData;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use ReflectionClass;
use ReflectionMethod;

trait SortByQuery
{
Expand Down Expand Up @@ -35,7 +38,33 @@ protected function buildSortQuery()
$this->sort = BuilderSortData::fromRequest($this->sort);

$this->sort->each(function (SortData $item) {
$this->query->orderBy($item->field, $item->type);
if ($item->isRelation) {
if (!is_a($this->query->getModel()->{$item->relationMethod}(), Relation::class)) {
return true;
}
// Get Model instance
$model = $this->query->getModel();

// Get Relation Instance
$relation = $model->{$item->relationMethod}();

// Get TableName of Model Class
$tableNameFrom = $model->getTable();

// Get TableName of Relation Model
$tableRelation = $relation->getRelated()->getTable();

// Get Instance of Relation Target Model
$relationModel = $relation->getRelated();

// Add Order Query with SubQuery
$this->query->orderBy(
$relationModel::select($item->field)
->whereColumn($tableRelation . '.' . $relation->getOwnerKeyName(), $tableNameFrom . '.' . $relation->getForeignKeyName()) // targetTable , FromTable
);
} else {
$this->query->orderBy($item->field, $item->type);
}
});
}
}
23 changes: 22 additions & 1 deletion src/Data/SortData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace GhoniJee\DxAdapter\Data;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class SortData
Expand All @@ -10,14 +11,34 @@ class SortData

public $type;

public $relationMethod;

public bool $isRelation = false;

public function __construct(array $data)
{
$this->field = $data['selector'];
$this->isRelationField();
$this->type = $data['desc'] ? 'DESC' : 'ASC';
}

public static function fromRequest(array $data)
public static function fromRequest($data)
{
$data = (array) $data;
return new self($data);
}

public function isRelationField()
{
if (Str::contains($this->field, '.')) {
$this->isRelation = true;
[$this->relationMethod, $this->field] = collect(explode('.', $this->field))
->pipe(function (Collection $parts) {
return [
$parts->except(count($parts) - 1)->implode('.'),
$parts->last(),
];
});
}
}
}
3 changes: 2 additions & 1 deletion tests/Feature/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,9 @@
$query = TestModel::query();

$data = DxAdapter::load($query, $this->request)->get();
$expect = TestModel::where('name', 'like', 'ahmad')->get();

expect($data)->toHaveCount(2);
expect($data)->toEqual($expect);
});

test('can multi filter with conjungtion AND', function () {
Expand Down
24 changes: 24 additions & 0 deletions tests/Feature/SortByTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use GhoniJee\DxAdapter\DxAdapter;
use GhoniJee\DxAdapter\Models\TestComment;
use GhoniJee\DxAdapter\Models\TestModel;
use Illuminate\Http\Request;

Expand Down Expand Up @@ -63,3 +64,26 @@

expect($query)->toEqual($queryExpectation);
});

test('can build query for order by relation fieldname with select', function () {
$sort = ['desc' => true, 'selector' => 'test.name'];
$select = ['test_model_id', 'comment'];

$this->request->replace(['sort' => $sort, 'select' => $select]);

$result = DxAdapter::load(TestComment::query()->with('test'), $this->request)->get();
$expected = TestComment::select(['comment', 'test_model_id'])->with('test')->orderBy(
TestModel::select('name')->whereColumn('test_models.id', 'test_comments.test_model_id')
)->get();
expect($result)->toEqual($expected);
});

test('can build query for order by relation fieldname without select', function () {
$sort = ['desc' => true, 'selector' => 'test.name'];

$this->request->replace(['sort' => $sort]);

$result = DxAdapter::for(TestComment::class, $this->request)->first();
$expected = TestComment::where('id', 4)->first();
expect($result)->toEqual($expected);
});
16 changes: 8 additions & 8 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected function createTestModel()

$data = [
[
'name' => "Ahmad",
'name' => "Jee",
'last_name' => "Yunus",
'address' => "Jl. Pendidikan",
'gender' => "L",
Expand Down Expand Up @@ -112,31 +112,31 @@ protected function createTestComment()
$comment = [
[
'test_model_id' => 1,
'comment' => "hahsdhshadas"
'comment' => "ccc"
],
[
'test_model_id' => 3,
'comment' => "hahsdhshadas"
'comment' => "ddd"
],
[
'test_model_id' => 1,
'comment' => "hahsdhshadas"
'comment' => "bbb"
],
[
'test_model_id' => 2,
'comment' => "hahsdhshadas"
'comment' => "aaa"
],
[
'test_model_id' => 1,
'comment' => "hahsdhshadas"
'comment' => "eee"
],
[
'test_model_id' => 2,
'comment' => "hahsdhshadas"
'comment' => "ffff"
],
[
'test_model_id' => 3,
'comment' => "hahsdhshadas"
'comment' => "gggg"
]
];
TestComment::insert($comment);
Expand Down

0 comments on commit 5936c15

Please sign in to comment.