Reference documentation and code samples for the Cloud Datastore Client class Key.
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 callingKey::state(). The return will be one ofKey::STATE_NAMEDorKey::STATE_INCOMPLETE.
Example:
use Google\Cloud\Datastore\DatastoreClient;
$datastore = new DatastoreClient();
$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.
Namespace
Google \ Cloud \ Datastore
Methods
__construct
Create a Key.
Parameters
Name
Description
projectId
string
The project ID.
options
array
Configuration Options
↳ namespaceId
string
Partitions data under a namespace. Useful forMultitenant Projects. Applications with no need for multitenancy should not set this value.
// An incomplete key does not have an ID on its last path element.
$key = $datastore->key('parent', 1234)
->pathElement('child');
if ($key->state() === Key::STATE_INCOMPLETE) {
echo 'Key is incomplete!';
}
// A named key has a kind and an identifier on each path element.
$key = $datastore->key('parent', 1234)
->pathElement('child', 4321);
if ($key->state() === Key::STATE_NAMED) {
echo 'Key is named!';
}
Returns
Type
Description
string
setLastElementIdentifier
Set the value of the last path element in a Key
This method is used internally when IDs are allocated to existing instances
of a Key. It should not generally be used externally.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Cloud Datastore Client - Class Key (1.33.1)\n\nVersion latestkeyboard_arrow_down\n\n- [1.33.1 (latest)](/php/docs/reference/cloud-datastore/latest/Key)\n- [1.33.0](/php/docs/reference/cloud-datastore/1.33.0/Key)\n- [1.32.3](/php/docs/reference/cloud-datastore/1.32.3/Key)\n- [1.31.0](/php/docs/reference/cloud-datastore/1.31.0/Key)\n- [1.30.0](/php/docs/reference/cloud-datastore/1.30.0/Key)\n- [1.29.2](/php/docs/reference/cloud-datastore/1.29.2/Key)\n- [1.28.2](/php/docs/reference/cloud-datastore/1.28.2/Key)\n- [1.26.0](/php/docs/reference/cloud-datastore/1.26.0/Key)\n- [1.25.0](/php/docs/reference/cloud-datastore/1.25.0/Key)\n- [1.24.4](/php/docs/reference/cloud-datastore/1.24.4/Key)\n- [1.23.0](/php/docs/reference/cloud-datastore/1.23.0/Key)\n- [1.22.1](/php/docs/reference/cloud-datastore/1.22.1/Key)\n- [1.21.2](/php/docs/reference/cloud-datastore/1.21.2/Key)\n- [1.19.0](/php/docs/reference/cloud-datastore/1.19.0/Key)\n- [1.18.1](/php/docs/reference/cloud-datastore/1.18.1/Key)\n- [1.17.1](/php/docs/reference/cloud-datastore/1.17.1/Key) \nReference documentation and code samples for the Cloud Datastore Client class Key.\n\nKeys are unique identifiers for entities.\n\nKeys may be considered either \"named\" or \"incomplete\". A named Key is one in\nwhich each element of the Key path specify a Kind and a Name or ID. An\nincomplete Key omits the Name or ID from the final path element.\n\nNamed Keys are required for any lookup, update, upsert or delete operations.\nThey may also be used for inserting records, so long as you are certain that\nthe identifier is available in Datastore.\n\nIncomplete Keys may be used for inserting records into Datastore. When an\nincomplete Key is used, Datastore will allocate an ID before inserting.\n\nIncomplete Keys are useful for guaranteeing the availability of an identifier\nwithout requiring an additional operation to check whether a given name or ID\nis available.\n\nKey state can be checked by calling `Key::state()`. The return will be one of\n`Key::STATE_NAMED` or `Key::STATE_INCOMPLETE`.\n\nExample: \n\n use Google\\Cloud\\Datastore\\DatastoreClient;\n\n $datastore = new DatastoreClient();\n\n $key = $datastore-\u003ekey('Person', 'Bob');\n\n // Keys with complex paths can be constructed with additional method calls.\n\n $key = $datastore-\u003ekey('Person', 'Bob');\n $key-\u003eancestor('Parents', 'Joe');\n $key-\u003eancestor('Grandparents', 'Barb');\n\n // Path elements can also be appended, so long as the current last path\n // element contains a kind and identifier.\n\n $key = $datastore-\u003ekey('Grandparents', 'Barb');\n $key-\u003epathElement('Parents', 'Joe');\n $key-\u003epathElement('Person');\n $key-\u003epathElement('Child', 'Dave'); // Error here.\n\nNamespace\n---------\n\nGoogle \\\\ Cloud \\\\ Datastore\n\nMethods\n-------\n\n### __construct\n\nCreate a Key.\n\n### pathElement\n\nSee also:\n\n- [PathElement](https://cloud.google.com/datastore/reference/rest/v1/Key#PathElement)\n\n### ancestor\n\nSee also:\n\n- [PathElement](https://cloud.google.com/datastore/reference/rest/v1/Key#PathElement)\n\n### ancestorKey\n\nUse another Key's path as the current Key's ancestor\n\nGiven key path will be prepended to any path elements on the current key.\n\nExample: \n\n $parent = $datastore-\u003ekey('Person', 'Dad');\n $key-\u003eancestorKey($parent);\n\n### state\n\nCheck if the Key is considered Named or Incomplete.\n\nUse `Key::STATE_NAMED` and `Key::STATE_INCOMPLETE` to check value.\n\nExample: \n\n // An incomplete key does not have an ID on its last path element.\n $key = $datastore-\u003ekey('parent', 1234)\n -\u003epathElement('child');\n\n if ($key-\u003estate() === Key::STATE_INCOMPLETE) {\n echo 'Key is incomplete!';\n }\n\n // A named key has a kind and an identifier on each path element.\n $key = $datastore-\u003ekey('parent', 1234)\n -\u003epathElement('child', 4321);\n\n if ($key-\u003estate() === Key::STATE_NAMED) {\n echo 'Key is named!';\n }\n\n### setLastElementIdentifier\n\nSet the value of the last path element in a Key\n\nThis method is used internally when IDs are allocated to existing instances\nof a Key. It should not generally be used externally.\n\nExample: \n\n $key = $datastore-\u003ekey('Person');\n $key-\u003esetLastElementIdentifier('Bob', Key::TYPE_NAME);\n\n### path\n\nGet the key path\n\nExample: \n\n $path = $key-\u003epath();\n\n### pathEnd\n\nGet the last pathElement in the key\n\nExample: \n\n $lastPathElement = $key-\u003epathEnd();\n\n### pathEndIdentifier\n\nGet the last pathElement identifier.\n\nIf the key is incomplete, returns `null`.\n\nExample: \n\n $lastPathElementIndentifier = $key-\u003epathEndIdentifier();\n\n### pathEndIdentifierType\n\nGet the last pathElement identifier type.\n\nIf the key is incomplete, returns `null`.\n\nExample: \n\n $lastPathElementIdentifierType = $key-\u003epathEndIdentifierType();\n\n### keyObject\n\nGet the key object formatted for the datastore service.\n\n### jsonSerialize\n\n### __toString\n\nRepresent the path as a string.\n\nConstants\n---------\n\n### TYPE_NAME\n\n Value: 'name'\n\n### TYPE_ID\n\n Value: 'id'\n\n### STATE_NAMED\n\n Value: 'named'\n\n### STATE_INCOMPLETE\n\n Value: 'incomplete'\n\n### STATE_COMPLETE\n\n Value: self::STATE_NAMED"]]