Created
September 13, 2012 19:00
-
-
Save Swader/3716757 to your computer and use it in GitHub Desktop.
Usage example of the Result class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The Result class is the default return format of the APIs | |
* | |
* When you get a Result object (e.g. $r), the recommended way of checking | |
* whether or not it contains any values is like so: | |
*/ | |
if ($r->count()) { | |
// Do something | |
} | |
/** | |
* The Result object also implements the Iterator interface, | |
* which means it can be iterated, like so: | |
*/ | |
foreach ($r as $k => $v) { | |
echo 'Key is '.$k.' <br />'; | |
echo 'Values is '.$v.' <br />'; | |
echo '<hr />'; | |
} | |
/** | |
* You can also extract the array Result contains by doing | |
* any of the following: | |
*/ | |
$aExtractedValues = $r->data; | |
// or | |
$aExtractedValues = $r->getData(); | |
/** | |
* When you are returning anything from the APIs, use the following | |
* format: | |
*/ | |
$myResult = array( /* ... some result here ... */ ); | |
return new Result($myResult); | |
/** | |
* If you are returning a subset of a large set of data, | |
* set the counter as well so the data can be paginated, | |
* like so: | |
*/ | |
$mySubset = array( /* ... some result here ... */ ); | |
$iCount = $this->fetchOne('SELECT FOUND_ROWS()'); | |
return new Result($mySubset, $iCount); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment