PHP Class Google\Cloud\Datastore\Query\Query

Queries can be created either by using the builder pattern, or by providing a Query when creating this object. Example: use Google\Cloud\ServiceBuilder; $cloud = new ServiceBuilder(); $datastore = $cloud->datastore(); $query = $datastore->query(); $query->kind('Person'); $query->filter('firstName', 'Bob'); $result = $datastore->runQuery($query); Queries can also be constructed using a Query Object: $query = $datastore->query([ 'query' => [ 'kind' => [ [ 'name' => 'People' ] ], 'filter' => [ 'propertyFilter' => [ 'op' => 'EQUAL', 'property' => [ 'name' => 'firstName' ], 'value' => [ 'stringValue': 'Bob' ] ] ] ] ]); $result = $datastore->runQuery($query);
See also: https://cloud.google.com/datastore/reference/rest/v1/projects/runQuery#query Query Object Reference
See also: https://cloud.google.com/datastore/docs/concepts/queries Datastore Queries
Inheritance: implements Google\Cloud\Datastore\Query\QueryInterface, use trait Google\Cloud\Datastore\DatastoreTrait
Show file Open project: GoogleCloudPlatform/gcloud-php Class Usage Examples

Public Methods

Method Description
__construct ( EntityMapper $entityMapper, array $query = [] )
canPaginate ( ) : boolean Indicate that this type does support automatic pagination.
distinctOn ( array | string $property ) : Query The properties to make distinct.
end ( string $cursor ) : Query The ending point for the query results.
filter ( string $property, string $operator, mixed $value ) : Query Add a filter to the query.
hasAncestor ( Key $key ) : Query Query for entities by their ancestors.
jsonSerialize ( )
keysOnly ( ) : Query Set the query to return only keys (no properties).
kind ( array | string $kinds ) : Query Set the Kind to query.
limit ( integer $num ) : Query The number of results to return.
offset ( integer $num ) : Query The number of results to skip.
order ( string $property, string $direction = self::ORDER_DEFAULT ) : Query Specify an order for the query.
projection ( array | string $properties ) : Query Set the Query Projection.
queryKey ( ) : string Return the query_type union field name.
queryObject ( ) : array Return a service-compliant array.
start ( string $cursor ) : Query The starting point for the query results.

Private Methods

Method Description
initializeFilter ( ) : void Setup the filter object when the first filter is created.
mapOperator ( string $operator ) : string Convert given operator to API-compatible operator.
propertyName ( string $property ) : array Format a property name.

Method Details

__construct() public method

public __construct ( EntityMapper $entityMapper, array $query = [] )
$entityMapper Google\Cloud\Datastore\EntityMapper An instance of EntityMapper
$query array [optional] [Query](https://cloud.google.com/datastore/reference/rest/v1/projects/runQuery#query)

canPaginate() public method

Indicate that this type does support automatic pagination.
public canPaginate ( ) : boolean
return boolean

distinctOn() public method

The query results will contain the first result for each distinct combination of values for the given properties (if empty, all results are returned). Example: $query->distinctOn('lastName');
public distinctOn ( array | string $property ) : Query
$property array | string The property or properties to make distinct.
return Query

end() public method

Example: $query->end($lastResultCursor);
See also: https://cloud.google.com/datastore/docs/concepts/queries#cursors_limits_and_offsets Cursors, Limits and Offsets
public end ( string $cursor ) : Query
$cursor string The cursor on which to end the result.
return Query

filter() public method

If the top-level filter is specified as a propertyFilter, it will be replaced. Any composite filters will be preserved and the new filter will be added. Example: $query->filter('firstName', '=', 'Bob') ->filter('lastName', '=', 'Testguy');
See also: https://cloud.google.com/datastore/reference/rest/v1/projects/runQuery#operator_1 Allowed Operators
public filter ( string $property, string $operator, mixed $value ) : Query
$property string The property to filter.
$operator string The operator to use in the filter. A list of allowed operators may be found [here](https://cloud.google.com/datastore/reference/rest/v1/projects/runQuery#operator_1). Short comparison operators are provided for convenience and are mapped to their datastore-compatible equivalents. Available short operators are `=`, `<`, `<=`, `>`, and `>=`.
$value mixed The value to check.
return Query

hasAncestor() public method

Keys can be provided either via a {@see \Google\Cloud\Datastore\Key} object, or by providing a kind, identifier and (optionally) an identifier type. Example: $key = $datastore->key('Person', 'Bob'); $query->hasAncestor($key); Specifying an identifier type $key = $datastore->key('Robots', '1337', [ 'identifierType' => Key::TYPE_NAME ]); $query->hasAncestor($key);
public hasAncestor ( Key $key ) : Query
$key Google\Cloud\Datastore\Key The ancestor Key instance.
return Query

jsonSerialize() public method

public jsonSerialize ( )

keysOnly() public method

Example: $query->keysOnly();
public keysOnly ( ) : Query
return Query

kind() public method

If empty, returns entities of all kinds. Must be set in order to filter results. While you may supply as many kinds as you wish, datastore currently only accepts one at a time. Example: $query->kind('Person');
public kind ( array | string $kinds ) : Query
$kinds array | string The kind or kinds to return. Only a single kind is currently supported.
return Query

limit() public method

Example: $query->limit(50);
See also: https://cloud.google.com/datastore/docs/concepts/queries#cursors_limits_and_offsets Cursors, Limits and Offsets
public limit ( integer $num ) : Query
$num integer The number of results to return.
return Query

offset() public method

Example: $query->offset(2);
See also: https://cloud.google.com/datastore/docs/concepts/queries#cursors_limits_and_offsets Cursors, Limits and Offsets
public offset ( integer $num ) : Query
$num integer The number of results to skip.
return Query

order() public method

Example: $query->order('birthDate', Query::ORDER_DESCENDING);
See also: https://cloud.google.com/datastore/reference/rest/v1/projects/runQuery#Direction Allowed Directions
public order ( string $property, string $direction = self::ORDER_DEFAULT ) : Query
$property string The property to order by.
$direction string The direction to order in.
return Query

projection() public method

Accepts an array of properties. If set, only these properties will be returned. Example: $query->projection(['firstName', 'lastName']);
public projection ( array | string $properties ) : Query
$properties array | string The property or properties to include in the result.
return Query

queryKey() public method

Return the query_type union field name.
public queryKey ( ) : string
return string

queryObject() public method

This method is intended for use internally by the PHP client.
public queryObject ( ) : array
return array

start() public method

Example: $query->start($lastResultCursor);
See also: https://cloud.google.com/datastore/docs/concepts/queries#cursors_limits_and_offsets Cursors, Limits and Offsets
public start ( string $cursor ) : Query
$cursor string The cursor on which to start the result.
return Query