lithium\data\collection\MultiKeyRecordSet::offsetGet()
Overrides
lithium\util\Collection::offsetGet()
Gets a record from the record set using PHP's array syntax, i.e. $records[5]
. Using loose
typing, integer keys can be accessed using strings and vice-versa. For record sets with
composite keys, records may be accessed using arrays as array keys. Note that the order of
the keys in the array does not matter.
Because record data in RecordSet
is lazy-loaded from the database, new records are fetched
until one with a matching key is found.
Parameters
-
mixed
$offset
The offset, or ID (index) of the record you wish to load. If
$offset
isnull
, all records are loaded into the record set, andoffsetGet
returnsnull
.
Returns
objectReturns a Record
object if a record is found with a key that matches the
value of $offset
, otheriwse returns null
.
Source
public function offsetGet($offset) {
$offset = (!$offset || $offset === true) ? 0 : $offset;
if (in_array($offset, $this->_index)) {
return $this->_data[array_search($offset, $this->_index)];
}
if ($this->closed()) {
return null;
}
if ($model = $this->_model) {
$offsetKey = $model::key($offset);
while ($record = $this->_populate()) {
$curKey = $model::key($record);
$keySet = $offsetKey == $curKey;
if (!is_null($offset) && $keySet) {
return $record;
}
}
}
$this->close();
}