Reference documentation and code samples for the Cloud Datastore Client class Entity.
A Datastore Entity
Entity implements PHP's ArrayAccess , allowing access via the array syntax (example below).
Properties are mapped automatically to their corresponding Datastore value types. Refer to the table below for a guide to how types are stored.
PHP Type | Datastore Value Type |
---|---|
\DateTimeInterface
|
timestampValue
|
Google\Cloud\Datastore\Google\Cloud\Datastore\Key | keyValue
|
Google\Cloud\Datastore\Google\Cloud\Datastore\GeoPoint | geoPointValue
|
Google\Cloud\Datastore\Google\Cloud\Datastore\Entity | entityValue
|
Google\Cloud\Datastore\Google\Cloud\Datastore\Blob | blobValue
|
Google\Cloud\Datastore\Google\Cloud\Core\Int64 | integerValue
|
Associative Array | entityValue
(No Key) |
Non-Associative Array | arrayValue
|
float
|
doubleValue
|
int
|
integerValue
|
string
|
stringValue
|
resource
|
blobValue
|
NULL
|
nullValue
|
bool
|
booleanValue
|
object
(Outside types specified above) |
ERROR InvalidArgumentException
|
Example:
use Google\Cloud\Datastore\DatastoreClient;
$datastore = new DatastoreClient();
$key = $datastore->key('Person', 'Bob');
$entity = $datastore->entity($key, [
'firstName' => 'Bob',
'lastName' => 'Testguy'
]);
$firstName = $entity['firstName']; // 'Bob'
$entity['location'] = 'Detroit, MI';
// Custom entity types can be created by implementing the datastore entity interface.
// You can also define mappings to correctly fetch embedded entities.
use Google\Cloud\Datastore\EntityTrait;
use Google\Cloud\Datastore\EntityInterface;
class Business implements EntityInterface
{
use EntityTrait;
public static function mappings()
{
return [
'parent' => Business::class
];
}
}
$alphabet = new Business;
$alphabet->set([
'companyName' => 'Alphabet'
]);
$key = $datastore->key('Business', 'Google');
$google = $datastore->entity($key, [
'companyName' => 'Google',
'parent' => $alphabet
], [
'className' => Business::class
]);
$datastore->insert($google);
$google = $datastore->lookup($key, ['className' => Business::class]);
echo get_class($google); // `Business`
echo get_class($google->get()['parent']); // `Business`
Namespace
Google \ Cloud \ DatastoreMethods
offsetSet
key
string
The value name.
val
mixed
The value.
void
offsetExists
key
string
the value to check.
bool
offsetUnset
key
string
the value to remove.
void
offsetGet
key
string
the value to retrieve.
mixed
__get
property
string
mixed
__set
property
string
value
mixed
void
__unset
property
string
void
__isset
property
string
bool