Hey all, hopefully a quick question…
Is there a way to access specific objects in the collection? Specifically, the first one. I can do a “foreach” on $thing, but I can’t do $thing[0].
Greg
Accessing the first item is easy, because the object itself takes on those field values, e.g. $thing->name is set (assuming a record was found, of course—see $thing->exists())
Accessing an arbitrary item in the collection can be done using $thing->all, which is an array. The index depends on your configuration. If your configuration has
$config['all_array_uses_ids'] = TRUE;then objects are indexed in that array by their ID. If FALSE, $thing->all is a standard array indexed starting with 0.
The foreach works as you’ve described because DataMapper objects are iterators, but you can’t directly index them ($thing[0], as in your example) because they are not arrays.