Cloud Datastore Client - Class Entity (1.32.0)

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\Key keyValue
Google\Cloud\Datastore\GeoPoint geoPointValue
Google\Cloud\Datastore\Entity entityValue
Google\Cloud\Datastore\Blob blobValue
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 \ Datastore

Methods

offsetSet

Parameters
Name Description
key string

The value name.

val mixed

The value.

Returns
Type Description
void

offsetExists

Parameter
Name Description
key string

the value to check.

Returns
Type Description
bool

offsetUnset

Parameter
Name Description
key string

the value to remove.

Returns
Type Description
void

offsetGet

Parameter
Name Description
key string

the value to retrieve.

Returns
Type Description
mixed

__get

Parameter
Name Description
property string
Returns
Type Description
mixed

__set

Parameters
Name Description
property string
value mixed
Returns
Type Description
void

__unset

Parameter
Name Description
property string
Returns
Type Description
void

__isset

Parameter
Name Description
property string
Returns
Type Description
bool