Key Iterator Methods - Mura Docs v7.1

Key Iterator Methods

The key Mura Iterator methods are described below.

Method Returns Description
hasNext boolean Returns true if iterator has objects in the array, and hasn't finished looping. Returns false, if iterator is empty, or has finished looping. This method "looks right" or forward, in the array of objects.
next object Moves the "index" to the next object, or next record in the underlying query's recordset. Returns the next object in the iterator (array of objects). Usually assigned to a variable, in order to have a way to reference the current object being looped over in the array of objects.
peek object Returns the object after the current object being looped over (or next) in the iterator. It's a way to "peek" at what's up next.
reset n/a Resets the "index" back to the beginning. Useful for when you need to loop over an iterator more than once.
end n/a Moves the "index" to the last object, or last record in the underlying query's recordset.
hasPrevious boolean Similar to hasNext, except it "looks left" or backward, in the array of objects. Returns true if iterator has an object that it has previously looped over, or false, if the index is at the beginning.
previous object Moves the "index" to the previous object, or previous record in the underlying query's recordset. Usually assigned to a variable, in order to have a way to reference the current object being looped over in the array of objects.
setQuery n/a Expects a query parameter, and an optional maxRecordsPerPage (defaults to 1000) parameter, to set the underlying query used for the iterator.
getQuery query Returns the underlying query used for the iterator.
getRecordCount numeric Returns the total number of records in the underlying query used for the iterator.
setPage n/a Expects a pageIndex parameter, to set the page of records/objects the iterator should be working with.
getPage numeric Returns the page index of records/objects the iterator is currently working with.
pageCount numeric Returns the total number of pages the iterator has, based on the total number of items/records and number returned from getItemsPerPage().
setItemsPerPage n/a Expects a itemsPerPage parameter, to set how many items per page the iterator should be working with. Formerly known as setNextN().
getItemsPerPage numeric Returns the number of items per page the iterator is currently working with. Formerly known as getNextN().
setStartRow n/a Expects a startRow parameter, to set which row of the underlying query the iterator should be working with.
getStartRow numeric Returns the row of the underlying query the iterator is currently working with.
getCurrentIndex numeric Returns the index of the iterator is currently working with.

Examples

In addition to the examples listed below, be sure to view the examples in the Common Beans section, where many of Mura's objects include their own iterator methods.

Feed Bean Iterator

The example below demonstrates how to use a Feed Bean, and use its getIterator() method.

<cfset feedBean = m.getFeed('content').where().prop('credits').containsValue('Steve')>
<cfset it = feedBean.getIterator()>

<cfif it.hasNext()>
  <cfloop condition="it.hasNext()">
    <cfset item = it.next()>
    <div>
      <a href="#item.get('url')#">
        #esapiEncode('html', item.get('title'))#
      </a>
    </div>
  </cfloop>
<cfelse>
  <p class="alert alert-info">
    This iterator doesn't have any items.
  </p>
</cfif>

Content Iterator

You can use any query to populate and use Mura's contentIterator bean, as long as the query contains a siteid and contentid column.

rs = QueryExecute("
  SELECT contentid, siteid
  FROM tcontent
  WHERE active = 1
");

Now, use the contentIterator bean's setQuery method, and use the iterator.

it = m.getBean('contentIterator').setQuery(rs);

if ( it.hasNext() ) {
  item = it.next();
  // Do something with the item
  WriteDump( item.getAllValues() );
} else {
  // No items/records exist
}

Query Iterator

The example below demonstrates how to use a regular query with Mura's queryIterator bean.

rs = QueryExecute("
  SELECT *
  FROM someTable
");

Now, use the queryIterator bean's setQuery method, and use the iterator.

it = m.getBean('queryIterator').setQuery(rs);

if ( it.hasNext() ) {
  item = it.next();
  // Do something with the item
  WriteDump( item );
} else {
  // No items/records exist
}