public function listBooks($limit = 10, $cursor = null) { $query = new \Google_Service_Datastore_Query(); $query->setKinds(['Book']); $query->setOrder('title'); $query->setLimit($limit); $query->setStartCursor($cursor); $request = new \Google_Service_Datastore_RunQueryRequest(); $request->setQuery($query); $response = $this->datastore->datasets->runQuery($this->datasetId, $request); /** @var \Google_Service_Datastore_QueryResultBatch $batch */ $batch = $response->getBatch(); return array('books' => $response, 'next_page_token' => $batch->getEndCursor()); }
public function listBooks($limit = 10, $cursor = null) { $query = new \Google_Service_Datastore_Query(['kinds' => [['name' => 'Book']], 'order' => 'title', 'limit' => $limit, 'startCursor' => $cursor]); $request = new \Google_Service_Datastore_RunQueryRequest(); $request->setQuery($query); $response = $this->datastore->datasets->runQuery($this->datasetId, $request); /** @var \Google_Service_Datastore_QueryResultBatch $batch */ $batch = $response->getBatch(); $endCursor = $batch->getEndCursor(); $books = []; foreach ($batch->getEntityResults() as $entityResult) { $entity = $entityResult->getEntity(); $books[] = $this->propertiesToBook($entity->getProperties()); } return array('books' => $books, 'cursor' => $endCursor === $cursor ? null : $endCursor); }
/** * Creates a query object for pulling the last $limit number of * "PubSubMessage" items, equivalent to the following GQL: * * SELECT * from DatastoreComment ORDER BY created DESC LIMIT 20 * * @see https://cloud.google.com/datastore/docs/concepts/gql */ public function createSimpleQuery($limit = 20) { $request = new \Google_Service_Datastore_RunQueryRequest(); $query = new \Google_Service_Datastore_Query(); $order = new \Google_Service_Datastore_PropertyOrder(); $order->setDirection('descending'); $property = new \Google_Service_Datastore_PropertyReference(); $property->setName('created'); $order->setProperty($property); $query->setOrder([$order]); $kind = new \Google_Service_Datastore_KindExpression(); $kind->setName(self::KIND); $query->setKinds([$kind]); $query->setLimit($limit); $request->setQuery($query); return $request; }
/** * Execute the given query and return the results. */ protected static function executeQuery($query) { $req = new Google_Service_Datastore_RunQueryRequest(); $req->setQuery($query); $response = DatastoreService::getInstance()->runQuery($req); if (isset($response['batch']['entityResults'])) { return $response['batch']['entityResults']; } else { return []; } }
/** * @param \Google_Service_Datastore_Query|Query\Builder $query * @param array $options * @return Query\Results */ public function runQuery($query, $options = []) { if ($query instanceof Query\Builder) { $query = $query->build(); } // wrap the query in a run query request $request = new \Google_Service_Datastore_RunQueryRequest(); $request->setQuery($query); // run the actual query $response = $this->datastoreService->datasets->runQuery($this->datasetId, $request, $options); // wrap the response in a helper class return new Query\Results($response); }