Cloud Datastore Client - Class Entity (1.24.4)

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 TypeDatastore Value Type
\DateTimeInterfacetimestampValue
Google\Cloud\Datastore\KeykeyValue
Google\Cloud\Datastore\GeoPointgeoPointValue
Google\Cloud\Datastore\EntityentityValue
Google\Cloud\Datastore\BlobblobValue
Google\Cloud\Core\Int64integerValue
Associative ArrayentityValue (No Key)
Non-Associative ArrayarrayValue
floatdoubleValue
intintegerValue
stringstringValue
resourceblobValue
NULLnullValue
boolbooleanValue
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
NameDescription
key string

The value name.

val mixed

The value.

Returns
TypeDescription
void

offsetExists

Parameter
NameDescription
key string

the value to check.

Returns
TypeDescription
bool

offsetUnset

Parameter
NameDescription
key string

the value to remove.

Returns
TypeDescription
void

offsetGet

Parameter
NameDescription
key string

the value to retrieve.

Returns
TypeDescription
mixed

__get

Parameter
NameDescription
property string
Returns
TypeDescription
mixed

__set

Parameters
NameDescription
property string
value mixed
Returns
TypeDescription
void

__unset

Parameter
NameDescription
property string
Returns
TypeDescription
void

__isset

Parameter
NameDescription
property string
Returns
TypeDescription
bool