Google\Cloud\Datastore\Operation::runQuery PHP Method

runQuery() public method

Run a query and return entities
public runQuery ( Google\Cloud\Datastore\Query\QueryInterface $query, array $options = [] ) : Generator
$query Google\Cloud\Datastore\Query\QueryInterface The query object.
$options array [optional] { Configuration Options @type string $transaction The transaction ID, if the query should be run in a transaction. @type string $className The name of the class to return results as. Must be a subclass of {@see \Google\Cloud\Datastore\Entity}. If not set, {@see \Google\Cloud\Datastore\Entity} will be used. @type string $readConsistency See [ReadConsistency](https://cloud.google.com/datastore/reference/rest/v1/ReadOptions#ReadConsistency). }
return Generator
    public function runQuery(QueryInterface $query, array $options = [])
    {
        $options += ['className' => null, 'namespaceId' => $this->namespaceId];
        $moreResults = true;
        do {
            $request = $options + $this->readOptions($options) + ['projectId' => $this->projectId, 'partitionId' => $this->partitionId($this->projectId, $options['namespaceId']), $query->queryKey() => $query->queryObject()];
            $res = $this->connection->runQuery($request);
            if (isset($res['batch']['entityResults']) && is_array($res['batch']['entityResults'])) {
                $results = $this->mapEntityResult($res['batch']['entityResults'], $options['className']);
                foreach ($results as $result) {
                    (yield $result);
                }
                if ($query->canPaginate() && $res['batch']['moreResults'] === 'NOT_FINISHED') {
                    $query->start($res['batch']['endCursor']);
                } else {
                    $moreResults = false;
                }
            } else {
                $moreResults = false;
            }
        } while ($moreResults);
    }

Usage Example

Beispiel #1
0
 /**
  * Run a query and return entities inside a Transaction
  *
  * Example:
  * ```
  * $result = $transaction->runQuery($query);
  *
  * foreach ($result as $entity) {
  *     echo $entity['firstName'];
  * }
  * ```
  *
  * @param QueryInterface $query The query object.
  * @param array $options [optional] {
  *     Configuration Options
  *
  *     @type string $className The name of the class to return results as.
  *           Must be a subclass of {@see Google\Cloud\Datastore\Entity}.
  *           If not set, {@see Google\Cloud\Datastore\Entity} will be used.
  * }
  * @return \Generator<Google\Cloud\Datastore\Entity>
  */
 public function runQuery(QueryInterface $query, array $options = [])
 {
     return $this->operation->runQuery($query, $options + ['transaction' => $this->transactionId]);
 }
All Usage Examples Of Google\Cloud\Datastore\Operation::runQuery