Using Pageable Recordsets

Pageable recordset is a feature of the Actionscript Recordset Class which allows you to divide the result of a query into different pages and decide how to fetch the returned rows. Most of the Flash UIComponents can work with pageable recordsets, including the DataGrid, List, and ComboBox.

Pageable Recordsets can use different delivery modes:

  • ondemand
  • page
  • fetchall

In order to activate the pageable recordset feature, the methodTable must contain the attribute “pagesize”. It should look like this:

$this->methodTable = array(
	"getRecords" => array(
		"description" => "Returns some records",
		"access"      => "remote",
		"pagesize"    => "25",
	)
);

Your getRecords function and its counting function should look like this:

function getRecords($arg1, $arg2, $offset = 0, $limit = 25)
{
	$this->countQuery = sprintf("SELECT COUNT(*) AS recordCount FROM my_table WHERE col1 = %d OR col2 = %d",
								$arg1, $arg2);
	return mysql_query(sprintf("SELECT * FROM my_table WHERE col1 = %d OR col2 = %d LIMIT %d, %d",
								$arg1, $arg2, $offset, $limit));
}
 
function getRecords_count()
{
	$q = mysql_query($this->countQuery);
	$row = mysql_fetch_assoc($q);
	return $row['recordCount'];
}

What happens is that the time the first time getRecords is called AMFPHP will notice that pagesize is set, and will store the original arguments to the function. It will then call the [functionName]_count function to get the total record count (note that the name of the counting method may be changed by setting “countMethod” in the methodTable).

AMFPHP will then insert special paging information that can be read by Flash’s RecordSet class. Afterwards Flash will call a special function in AMFPHP which will be detected. Then AMFPHP will call your function with the original arguments PLUS two arguments that are to be used with a LIMIT statement.

LIMIT is MySQL-specific, although it is possible to emulate such functionality using stored procs in other database servers. The only other plausible method of achieving pageable recordsets in PHP would have been to store the result in a session, but this proved highly problematic as sessions can take a huge amount of space, possibly filling up the hard drive and causing server crashes, hence we canned the code.

Once a RecordSet has been returned from the server it is under the control of the RsDataFetcher. If you don’t set the delivery mode, Flash can nevertheless automatically return records on demand simply by using RecordSet.getItemAt(index). If the index is out of range, that is, the recordset entry exists but has not been fetched, RsDataFetcher will retrieve the recordset row and dispatch a modelChanged event, with eventType updateItem. Note however that in the default mode, RsDataFetcher will attempt to retrieve entries one at a time. Usually you will want to return items in pages, however, hence you will want to use setDeliveryMode with the ‘page’ option (read more about it in the Remoting manual).

To check that a pageable recordset is completely local, you can either check that RecordSet.getNumberAvailable() == RecordSet.getLength(), or you can listen to the modelChanged event, with eventType ‘allRows’ (again, the details are in the manual).

more info at Macromedia.


© amfphp.org | Disclaimer | Conditions of use