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 | | {@see \Google\Cloud\Datastore\Key} | keyValue | | {@see \Google\Cloud\Datastore\GeoPoint} | geoPointValue | | {@see \Google\Cloud\Datastore\Entity} | entityValue | | {@see \Google\Cloud\Datastore\Blob} | blobValue | | {@see \Google\Cloud\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\ServiceBuilder; $cloud = new ServiceBuilder(); $datastore = $cloud->datastore(); $key = $datastore->key('Person', 'Bob'); $entity = $datastore->entity($key, [ 'firstName' => 'Bob', 'lastName' => 'Testguy' ]); echo $entity['firstName']; // 'Bob' $entity['location'] = 'Detroit, MI';
Inheritance: implements ArrayAcces\ArrayAccess, use trait DatastoreTrait
Example #1
0
 public function testCursor()
 {
     $entity = new Entity($this->key, []);
     $this->assertNull($entity->cursor());
     $entity = new Entity($this->key, [], ['cursor' => 'foo']);
     $this->assertEquals('foo', $entity->cursor());
 }
Example #2
0
 /**
  * Translate an Entity to a datastore representation.
  *
  * @param Entity $entity The input entity.
  * @return array A Datastore [Entity](https://cloud.google.com/datastore/reference/rest/v1/Entity)
  */
 public function objectToRequest(Entity $entity)
 {
     $data = $entity->get();
     $properties = [];
     foreach ($data as $key => $value) {
         $exclude = in_array($key, $entity->excludedProperties());
         $properties[$key] = $this->valueObject($value, $exclude);
     }
     return array_filter(['key' => $entity->key(), 'properties' => $properties]);
 }
 /**
  * Enqueue a mutation
  *
  * A mutation is a change to the datastore. Create, Update and Delete are
  * examples of mutations, while Read is not.
  *
  * Google Cloud Datastore supports multiple mutations in a single API call,
  * subject to the limits of the service. Adding mutations separately from
  * committing the changes allows you to create complex operations, both
  * inside a transaction and not.
  *
  * @see https://cloud.google.com/datastore/docs/concepts/limits Limits
  *
  * @param string $operation The operation to execute. "Insert", "Upsert",
  *        "Update" or "Delete".
  * @param Entity|Key $input The entity or key to mutate.
  * @param string $type The type of the input array.
  * @param string $baseVersion [optional] The version of the entity that this mutation
  *        is being applied to. If this does not match the current version on
  *        the server, the mutation conflicts.
  * @return array [Mutation](https://cloud.google.com/datastore/docs/reference/rest/v1/projects/commit#Mutation).
  * @throws InvalidArgumentException
  */
 public function mutation($operation, $input, $type, $baseVersion = null)
 {
     // If the given element is an Entity, it will use that baseVersion.
     if ($input instanceof Entity) {
         $baseVersion = $input->baseVersion();
         $data = $this->entityMapper->objectToRequest($input);
     } elseif ($input instanceof Key) {
         $data = $input->keyObject();
     } else {
         throw new InvalidArgumentException(sprintf('Input must be a Key or Entity, %s given', get_class($input)));
     }
     return array_filter([$operation => $data, 'baseVersion' => $baseVersion]);
 }