Keys are unique identifiers for entities.
Keys may be considered either "named" or "incomplete". A named Key is one in
which each element of the Key path specify a Kind and a Name or ID. An
incomplete Key omits the Name or ID from the final path element.
Named Keys are required for any lookup, update, upsert or delete operations.
They may also be used for inserting records, so long as you are certain that
the identifier is available in Datastore.
Incomplete Keys may be used for inserting records into Datastore. When an
incomplete Key is used, Datastore will allocate an ID before inserting.
Incomplete Keys are useful for guaranteeing the availability of an identifier
without requiring an additional operation to check whether a given name or ID
is available.
Key state can be checked by calling Key::state(). The return will be one of
Key::STATE_NAMED or Key::STATE_INCOMPLETE.
Example:
use Google\Cloud\ServiceBuilder;
$cloud = new ServiceBuilder();
$datastore = $cloud->datastore();
$key = $datastore->key('Person', 'Bob');
Keys with complex paths can be constructed with additional method calls.
$key = $datastore->key('Person', 'Bob');
$key->ancestor('Parents', 'Joe');
$key->ancestor('Grandparents', 'Barb');
Path elements can also be appended, so long as the current last path
element contains a kind and identifier.
$key = $datastore->key('Grandparents', 'Barb');
$key->pathElement('Parents', 'Joe');
$key->pathElement('Person');
$key->pathElement('Child', 'Dave'); // Error here.