Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
Added orderBy method and functionality. (order by a specific field an…
Browse files Browse the repository at this point in the history
…d in a specific direction of DESC/ASC. Added orderBy testing.
  • Loading branch information
timothymarois committed Aug 5, 2017
1 parent 35bb646 commit 8e4e10a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Change Log
==========

### 08/05/2017 - 1.0.4
* Added `orderBy()` (sorting field and direction `ASC` and `DESC`)
* Added `limit()` Limit results returned, includes Limit and Offset options.

### 08/05/2017 - 1.0.3
* Added the `NOT LIKE` operator

Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,18 @@ $users = $db->query()
// You can also use `.` dot delimiter to use on nested keys
$users = $db->query()->where('status.language.english','=','blocked')->results();

// how about find all users that have a gmail account?
// Limit Example: Same query as above, except we only want to limit the results to 10
$users = $db->query()->where('status.language.english','=','blocked')->limit(10)->results();

// Query LIKE Example: how about find all users that have a gmail account?
$usersWithGmail = $db->query()->where('email','LIKE','@gmail.com')->results();

// OrderBy Example: From the above query, what if you want to order the results by nested array (profile name?)
$usersWithGmail = $db->query()->where('email','LIKE','@gmail.com')->orderBy('profile.name', 'ASC')->results();

// or just order the results by email address
$usersWithGmail = $db->query()->where('email','LIKE','@gmail.com')->orderBy('email', 'ASC')->results();

```

To run the query use `results()` or `resultDocuments()`
Expand All @@ -334,6 +343,8 @@ To run the query use `results()` or `resultDocuments()`
- `where()` param `array` for simple "equal to" OR `where($field, $operator, $value)`
- `andWhere()` *optional* see `where()`, uses the logical `AND`
- `orWhere()` *optional* see `where()`, this uses the logical `OR`
- `limit()` *optional* limit/offset results `limit($number, $offset)`
- `orderBy()` *optional* orders the results `orderBy($field, $direction)`, `$direction` = `ASC` or `DESC`
- `results()` This will return all the document data as an array.
- `resultDocuments()` This will return all the document objects

Expand Down
32 changes: 32 additions & 0 deletions src/QueryLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function run()
{
$this->documents = $cached_documents;

$this->sort();
$this->offsetLimit();

return $this;
Expand All @@ -94,6 +95,7 @@ public function run()
}
}

$this->sort();
$this->offsetLimit();

return $this;
Expand Down Expand Up @@ -164,6 +166,36 @@ protected function offsetLimit()
//--------------------------------------------------------------------


/**
* sort
*
*/
protected function sort()
{
$orderBy = $this->orderBy;
$sortBy = $this->sortBy;

if ($orderBy=='')
{
return false;
}

usort($this->documents, function($a, $b) use ($orderBy, $sortBy) {

if ($sortBy == 'DESC')
{
return $b->field($orderBy) <=> $a->field($orderBy);
}

return $a->field($orderBy) <=> $b->field($orderBy);
});

}


//--------------------------------------------------------------------


/**
* matchDocuments
*
Expand Down
58 changes: 56 additions & 2 deletions tests/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,6 @@ public function testLimitOffset()
$user->save();
}

$count = $db->count();

// test that it limits the results to "2" (total query pulls "5")
$test1 = $db->query()->where('rank','=',150)->limit(2)->results();

Expand All @@ -299,6 +297,62 @@ public function testLimitOffset()
//--------------------------------------------------------------------


/**
* testSorting()
*
* TEST CASE:
* - Creates 6 company profiles
* - Sorts them by DESC/ASC
*
*
*/
public function testSorting()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases/users_orderby',
'cache' => false
]);

$db->flush(true);

$companies = ['Google'=>150, 'Apple'=>180, 'Microsoft'=>120, 'Amex'=>20, 'Hooli'=>50, 'Amazon'=>140];

foreach($companies as $company=>$rank)
{
$user = $db->get(uniqid());
$user->name = $company;
$user->rank['reviews'] = $rank;
$user->status = 'enabled';
$user->save();
}

// test that they are ordered by name ASC (check first, second, and last)
$test1 = $db->query()->where('status','=','enabled')->orderBy('name', 'ASC')->results();
$this->assertEquals(['first'=>'Amazon','second'=>'Amex','last'=>'Microsoft'], ['first'=>$test1[0]['name'],'second'=>$test1[1]['name'],'last'=>$test1[5]['name']]);

// test that they are ordered by name ASC (check first, second, and last)
$test2 = $db->query()->where('status','=','enabled')->limit(3)->orderBy('name', 'ASC')->results();
$this->assertEquals(['Amazon','Amex','Apple'], [$test2[0]['name'],$test2[1]['name'],$test2[2]['name']]);

// test that they are ordered by name DESC (check first, second, and last)
$test3 = $db->query()->where('status','=','enabled')->limit(3)->orderBy('name', 'DESC')->results();
$this->assertEquals(['Microsoft','Hooli','Google'], [$test3[0]['name'],$test3[1]['name'],$test3[2]['name']]);

// test that they are ordered by rank nested [reviews] DESC
$test4 = $db->query()->where('status','=','enabled')->limit(3)->orderBy('rank.reviews', 'DESC')->results();
$this->assertEquals(['Apple','Google','Amazon'], [$test4[0]['name'],$test4[1]['name'],$test4[2]['name']]);

// test that they are ordered by rank nested [reviews] ASC
$test5 = $db->query()->where('status','=','enabled')->limit(3)->orderBy('rank.reviews', 'ASC')->results();
$this->assertEquals(['Amex','Hooli','Microsoft'], [$test5[0]['name'],$test5[1]['name'],$test5[2]['name']]);

$db->flush(true);
}


//--------------------------------------------------------------------





Expand Down

0 comments on commit 8e4e10a

Please sign in to comment.