When you execute a SELECT statement with the query method,
a weeDatabaseResult will be returned. It allows you to retrieve the results of the query.
We can obtain a weeDatabaseResult object by running the following code:
<?php $oResults = weeApp()->db->query('SELECT * FROM articles ORDER BY art_date DESC LIMIT 5');
If you are not using the application framework, you can replace weeApp()->db by your database object.
Let's now see what we can do with those results.
weeDatabaseResults implements the Countable interface.
This interface makes us define a method, count, that returns an integer.
In the case of a database query results, it will be the number of rows returned.
The Countable interface allows us to give objects to the count($var) PHP function.
The function will then call the method count and return the resulting value.
See the count documentation for more details.
This allows us to get the number of rows returned by the query by writing the following code:
<?php
$iNumRows = count($oResults);
Web:Extend provides 3 ways of fetching rows from a weeDatabaseResults object.
The method fetch method is mainly used when you are getting only 1 row back from your query,
which usually apply to all queries that includes an equivalent of the LIMIT 1 statement.
fetch will throw an exception if there is no row to fetch, making it not appropriate for use for iterating through rows.
You can fetch a row using the following code:
<?php
$aRow = $oResults->fetch();
The method fetchAll method returns all the results as an array of associative arrays.
It can be useful if you want to process some data from the results before passing it to another function, like a template for display.
You can fetch all rows using the following code:
<?php
$aRows = $oResults->fetchAll();
The last possibility to retrieve rows is to use the Iterator capabilities of the weeDatabaseResult object.
Iterator allows you to use foreach directly on the weeDatabaseResult object, like this:
<?php foreach ($oResults as $aRow) { // Process the row here, example: var_dump($aRow); }
You might consider reading the PHP5 foreach documentation to learn how it works and be more comfortable before you start using it.
Most of the times you will pass directly the $oResults object to your template for display.
Please read the templates chapter for more details
about the interaction between weeDatabaseResult and the template,
like the automated encoding of rows data, automagically preventing cross-scripting vulnerabilities.
By default weeDatabaseResults returns an associative array
containing the names/values of the fetched row. It is possible to define a class that will store the row and provide additional functionality not available in a PHP array.
You can use the rowClass method to define the class that will store the rows data.
You can use any class, as long as the constructor takes one array parameter.
But Web:Extend also defines a base class for objects which contain data, weeDataHolder.
This class implements ArrayAccess and Mappable to make it act like an array when needed.
You can extend this class to add any method needed.
You could for example add a method that will format the row data to your liking.
When using domain models, you can use this method to automatically create instances of your model.
You only need to do this to change the row class used by the subsequent retrieval operations:
<?php
$oResults->rowClass('myClass');