This method does not execute any service requests.
Entities are created with a Datastore Key, or by specifying a Kind. Kinds
are only allowed for insert operations. For any other case, you must
specify a named key. If a kind is given, an ID will be automatically
allocated for the entity upon insert. Additionally, if your entity
requires a complex key elementPath, you must create the key separately.
In complex applications you may want to create your own entity types.
Google Cloud PHP supports subclassing of {@see \Google\Cloud\Datastore\Entity}.
If the name of a subclass of Entity is given in the options array, an
entity will be created with that class rather than the default class.
Example:
$key = $datastore->key('Person', 'Bob');
$entity = $datastore->entity($key, [
'firstName' => 'Bob',
'lastName' => 'Testguy'
]);
[snippet=array]
Entity values can be assigned and accessed via the array syntax.
$entity = $datastore->entity($key);
$entity['firstName'] = 'Bob';
$entity['lastName'] = 'Testguy';
[snippet=object_accessor]
Entity values can also be assigned and accessed via an object syntax.
$entity = $datastore->entity($key);
$entity->firstName = 'Bob';
$entity->lastName = 'Testguy';
[snippet=incomplete]
Entities can be created with a Kind only, for inserting into datastore
$entity = $datastore->entity('Person');
[snippet=custom_class]
Entities can be custom classes extending the built-in Entity class.
class Person extends Google\Cloud\Datastore\Entity
}
$person = $datastore->entity('Person', [ 'firstName' => 'Bob'], [
'className' => Person::class
]);
echo get_class($person); // Person
[snippet=exclude_indexes]
If you wish to exclude certain properties from datastore indexes,
property names may be supplied in the method $options:
$entity = $datastore->entity('Person', [
'firstName' => 'Bob',
'dateOfBirth' => new DateTime('January 31, 1969')
], [
'excludeFromIndexes' => [
'dateOfBirth'
]
]);