Skip to content

Commit

Permalink
implementing getResult method
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauro Cassani committed Jan 5, 2018
1 parent aa20837 commit 9ba7782
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ $first = $qb->getFirstResult();

// get last result
$last = $qb->getLastResult();

// get a result by index
$thirdResult = $qb->getResult(3);
```

### Avaliable criteria operators
Expand Down Expand Up @@ -221,7 +224,7 @@ foreach ($qb->getResults() as $element){
}
```

## Shuffled Results
## Shuffled results

You can shuffle query results by using `getShuffledResults` method:

Expand Down
8 changes: 8 additions & 0 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ public function getResults()
return array_map([$this, 'castElementToArray'], $results);
}

/**
* @return array
*/
public function getResult($n)
{
return $this->getResults()[$n-1] ?: [];
}

/**
* @return array
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,22 @@ public function it_should_get_results_from_a_query_with_aliases()
}
}

/**
* @test
*/
public function it_should_get_a_single_result_from_a_query()
{
foreach ($this->usersArrays as $array) {
$qb = QueryBuilder::create($array);

$result = $qb->getResult(3);

$this->assertEquals(3, $result['id']);
$this->assertEquals('Clementine Bauch', $result['name']);
$this->assertEquals($qb->getResult(1), $qb->getFirstResult());
}
}

/**
* @test
*/
Expand Down

0 comments on commit 9ba7782

Please sign in to comment.