Cloud Datastore Client - Class Key (1.33.1) Stay organized with collections Save and categorize content based on your preferences.
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 inwhich each element of the Key path specify a Kind and a Name or ID. Anincomplete 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 thatthe identifier is available in Datastore.
Incomplete Keys may be used for inserting records into Datastore. When anincomplete Key is used, Datastore will allocate an ID before inserting.
Incomplete Keys are useful for guaranteeing the availability of an identifierwithout requiring an additional operation to check whether a given name or IDis available.
Key state can be checked by callingKey::state(). The return will be one ofKey::STATE_NAMED orKey::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 \ DatastoreMethods
__construct
Create a Key.
| Parameters | |
|---|---|
| Name | Description |
projectId | stringThe project ID. |
options | arrayConfiguration Options |
↳ namespaceId | stringPartitions data under a namespace. Useful forMultitenant Projects. Applications with no need for multitenancy should not set this value. |
↳ databaseId | stringID of the database to which the entities belong. |
↳ path | arrayThe initial Key path. |
pathElement
See also:
| Parameters | |
|---|---|
| Name | Description |
kind | stringThe kind. |
identifier | string|int[optional] The name or ID of the object. |
options | arrayConfiguration Options |
↳ identifierType | stringIf omitted, the type will be determined internally. Setting this to either |
| Returns | |
|---|---|
| Type | Description |
Key | |
ancestor
See also:
| Parameters | |
|---|---|
| Name | Description |
kind | stringThe kind. |
identifier | string|intThe name or ID of the object. |
options | arrayConfiguration Options |
↳ identifierType | stringIf omitted, the type will be determined internally. Setting this to either |
| Returns | |
|---|---|
| Type | Description |
Key | |
ancestorKey
Use another Key's path as the current Key's ancestor
Given key path will be prepended to any path elements on the current key.
Example:
$parent = $datastore->key('Person', 'Dad');$key->ancestorKey($parent);| Parameter | |
|---|---|
| Name | Description |
key | KeyThe ancestor Key. |
| Returns | |
|---|---|
| Type | Description |
Key | |
state
Check if the Key is considered Named or Incomplete.
UseKey::STATE_NAMED andKey::STATE_INCOMPLETE to check value.
Example:
// 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 instancesof a Key. It should not generally be used externally.
Example:
$key = $datastore->key('Person');$key->setLastElementIdentifier('Bob', Key::TYPE_NAME);| Parameters | |
|---|---|
| Name | Description |
value | stringThe value of the ID or Name. |
type | string[optional] 'id' or 'name'.Defaults to |
| Returns | |
|---|---|
| Type | Description |
void | |
path
Get the key path
Example:
$path = $key->path();| Returns | |
|---|---|
| Type | Description |
array | |
pathEnd
Get the last pathElement in the key
Example:
$lastPathElement = $key->pathEnd();| Returns | |
|---|---|
| Type | Description |
array | |
pathEndIdentifier
Get the last pathElement identifier.
If the key is incomplete, returnsnull.
Example:
$lastPathElementIndentifier = $key->pathEndIdentifier();| Returns | |
|---|---|
| Type | Description |
string|int|null | |
pathEndIdentifierType
Get the last pathElement identifier type.
If the key is incomplete, returnsnull.
Example:
$lastPathElementIdentifierType = $key->pathEndIdentifierType();| Returns | |
|---|---|
| Type | Description |
string|null | |
keyObject
Get the key object formatted for the datastore service.
| Returns | |
|---|---|
| Type | Description |
array | |
jsonSerialize
__toString
Represent the path as a string.
Constants
TYPE_NAME
Value: 'name'TYPE_ID
Value: 'id'STATE_NAMED
Value: 'named'STATE_INCOMPLETE
Value: 'incomplete'STATE_COMPLETE
Value: self::STATE_NAMEDExcept as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-01-24 UTC.