Framework  3.9
DataItemIterator Class Reference

DataItemIterator is a memory-efficient iterator class that can be used when rendering large data sets. More...

Inherits Iterator, Countable, and ArrayAccess.

Public Member Functions

 __construct ($query)
 
 count ()
 
 rewind ()
 
 current ()
 
 valid ()
 
 next ()
 
 key ()
 
 offsetExists ($offset)
 
 offsetGet ($offset)
 
 offsetSet ($offset, $value)
 
 offsetUnset ($offset)
 

Public Attributes

 $query
 
 $result
 
 $current = null
 
 $position = -1
 
 $item
 

Detailed Description

DataItemIterator is a memory-efficient iterator class that can be used when rendering large data sets.

With this iterator DataItems are retrieved from the database one at a time, rather than retrieving the full result set into memory all at once. It is a forward-only iterator, meaning it can be used in foreach(...) loops, but not for random access to the results. This class is not intended to be used directly by the programmer.

Author
andy

Definition at line 49 of file iterated_query.inc.

Constructor & Destructor Documentation

◆ __construct()

DataItemIterator::__construct (   $query)

Definition at line 57 of file iterated_query.inc.

58  {
59  $this->query = $query;
60  $this->item = new $this->query->class;
61  }
query($class)
Performs a query against the database, returning an array of DataItem objects of the specified class.
Definition: query.inc:373

Member Function Documentation

◆ count()

DataItemIterator::count ( )

Definition at line 63 of file iterated_query.inc.

64  {
65  $this->rewind();
66  trace("Row Count: {$this->query->rowCount}", 4);
67  return $this->query->rowCount;
68  }
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010

◆ current()

DataItemIterator::current ( )

Definition at line 88 of file iterated_query.inc.

89  {
90  $this->item->populate($this->current);
91  return $this->item;
92  }

◆ key()

DataItemIterator::key ( )

Definition at line 105 of file iterated_query.inc.

106  {
107  return $this->position;
108  }

◆ next()

DataItemIterator::next ( )

Definition at line 99 of file iterated_query.inc.

100  {
101  $this->current = $this->result->fetch();
102  ++$this->position;
103  }

◆ offsetExists()

DataItemIterator::offsetExists (   $offset)

Definition at line 110 of file iterated_query.inc.

111  {
112  if (!is_numeric($offset)) return false;
113  return ($offset >= 0 && $offset < $this->query->rowCount);
114  }

◆ offsetGet()

DataItemIterator::offsetGet (   $offset)

Definition at line 116 of file iterated_query.inc.

117  {
118  if ($offset == $this->position) return $this->item;
119 
120  if ($offset < $this->position)
121  {
122  $this->rewind();
123  }
124  else
125  {
126  $offset -= $this->position;
127  }
128 
129  while($offset--)
130  {
131  $this->next();
132  }
133  return $this->item;
134  }

◆ offsetSet()

DataItemIterator::offsetSet (   $offset,
  $value 
)

Definition at line 136 of file iterated_query.inc.

137  {
138  throw new FakoliException("Attempt to write to iterated data query");
139  }

◆ offsetUnset()

DataItemIterator::offsetUnset (   $offset)

Definition at line 141 of file iterated_query.inc.

142  {
143  throw new FakoliException("Attempt to write to iterated data query");
144  }

◆ rewind()

DataItemIterator::rewind ( )

Definition at line 70 of file iterated_query.inc.

71  {
72  if ($this->position == 0) return;
73 
74  $this->current = null;
75  $this->position = 0;
76 
77  try
78  {
79  $this->result = $this->query->_runQuery();
80  $this->current = $this->result->fetch();
81  }
82  catch(PDOException $e)
83  {
84  throw new FakoliException($e->getMessage());
85  }
86  }

◆ valid()

DataItemIterator::valid ( )

Definition at line 94 of file iterated_query.inc.

95  {
96  return ($this->current != null);
97  }

Member Data Documentation

◆ $current

DataItemIterator::$current = null

Definition at line 53 of file iterated_query.inc.

◆ $item

DataItemIterator::$item

Definition at line 55 of file iterated_query.inc.

◆ $position

DataItemIterator::$position = -1

Definition at line 54 of file iterated_query.inc.

◆ $query

DataItemIterator::$query

Definition at line 51 of file iterated_query.inc.

◆ $result

DataItemIterator::$result

Definition at line 52 of file iterated_query.inc.


The documentation for this class was generated from the following file: