webCOMAND

query::get_first()

The get_first method executes an assembled query like get(), except only the first object is returned, instead of the entire collection.

Prototype

get_first(array $options = []): object

Parameters

  • options - An associative array with zero or more of the following key/value pairs.
    • checksum - If TRUE, a checksum unique to the resulting collection will be made available for retrieval with collection::get_result_checksum().  Otherwise do not produce a checksum (default).
    • checksum_only - If TRUE, only produce the checksum, and do not return the results.  This is the most efficient way to produce a checksum if you do not need the results.
    • bind - An array of values to bind to ? placeholders, or an  associative array of keys and values to bind to :key placeholders.  As an alternative, call bind().

Return

The first object from the query results, NULL if not results, or FALSE if there was an error.

Example (Chained)

// create, assemble and execute the query as one chained statement
$contact = $repo->query()
	->select('Name', 'Phone');
	->from('Contact');
	->where('Name', 'John');
	->get_first();

print($contact->Name . ": " . $contact->Phone . "\n");

Example (Long-hand)

// create and assemble the query, one clause at a time
$query = $repo->query();
$query->select('Name', 'Phone');
$query->from('Contact');
$query->where('Name', 'John');

// execute the query assembled above
$contact = $query->get_first();
print($contact->Name . ": " . $contact->Phone . "\n");

Related

get(), repo::get_first()