Skip to content

Commit 98b6015

Browse files
author
Takashi Matsuo
authored
Upgraded to google/cloud to 0.11.1 (GoogleCloudPlatform#195)
* Upgraded to google/cloud to 0.11.1 * Use `hasAncestor` and `keysOnly` for simplicity. * Simplified the transaction example.
1 parent d9eeea9 commit 98b6015

File tree

5 files changed

+253
-56
lines changed

5 files changed

+253
-56
lines changed

datastore/api/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"require": {
3-
"google/cloud": "~1.0|~0.10"
3+
"google/cloud": "~0.11"
44
},
55
"require-dev": {
66
"phpunit/phpunit": "~4.8",
7-
"google/cloud-tools": "~1.0|~0.5"
7+
"google/cloud-tools": "~0.5"
88
},
99
"autoload": {
1010
"psr-4": { "Google\\Cloud\\Samples\\Datastore\\": "src" },

datastore/api/composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

datastore/api/index.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,15 @@ indexes:
2929
properties:
3030
- name: priority
3131
- name: created
32+
33+
- kind: Task
34+
properties:
35+
- name: done
36+
- name: priority
37+
direction: desc
38+
39+
- kind: Task
40+
properties:
41+
- name: priority
42+
direction: desc
43+
- name: created

datastore/api/src/functions/concepts.php

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ function descending_sort(DatastoreClient $datastore)
408408
// [START descending_sort]
409409
$query = $datastore->query()
410410
->kind('Task')
411-
->order('create', Query::ORDER_DESCENDING);
411+
->order('created', Query::ORDER_DESCENDING);
412412
// [END descending_sort]
413413
return $query;
414414
}
@@ -438,13 +438,11 @@ function multi_sort(DatastoreClient $datastore)
438438
*/
439439
function ancestor_query(DatastoreClient $datastore)
440440
{
441-
// TODO: Move to `hasAncestor` once it's available.
442-
// https://github.com/GoogleCloudPlatform/google-cloud-php/issues/186
443441
// [START ancestor_query]
444442
$ancestorKey = $datastore->key('TaskList', 'default');
445443
$query = $datastore->query()
446444
->kind('Task')
447-
->filter('__key__', Query::OP_HAS_ANCESTOR, $ancestorKey);
445+
->hasAncestor($ancestorKey);
448446
// [END ancestor_query]
449447
return $query;
450448
}
@@ -473,11 +471,9 @@ function kindless_query(DatastoreClient $datastore, Key $lastSeenKey)
473471
*/
474472
function keys_only_query(DatastoreClient $datastore)
475473
{
476-
// TODO: Move to `keysOnly()` once it's available.
477-
// https://github.com/GoogleCloudPlatform/google-cloud-php/issues/187
478474
// [START keys_only_query]
479475
$query = $datastore->query()
480-
->projection('__key__')
476+
->keysOnly()
481477
->limit(1);
482478
// [END keys_only_query]
483479
return $query;
@@ -780,21 +776,14 @@ function transfer_funds(
780776
$amount
781777
) {
782778
$transaction = $datastore->transaction();
783-
$result = $transaction->lookupBatch([$fromKey, $toKey]);
779+
// The option 'sort' is important here, otherwise the order of the result
780+
// might be different from the order of the keys.
781+
$result = $transaction->lookupBatch([$fromKey, $toKey], ['sort' => true]);
784782
if (count($result['found']) != 2) {
785783
$transaction->rollback();
786784
}
787-
// Currently, the result from lookupBatch doesn't guarantee the same order
788-
// as the given keys with the client library.
789-
// TODO: remove this hack once the issue below is fixed.
790-
// https://github.com/GoogleCloudPlatform/google-cloud-php/issues/175
791-
if ($result['found'][0]->key()->path() == $fromKey->path()) {
792-
$fromAccount = $result['found'][0];
793-
$toAccount = $result['found'][1];
794-
} else {
795-
$fromAccount = $result['found'][1];
796-
$toAccount = $result['found'][0];
797-
}
785+
$fromAccount = $result['found'][0];
786+
$toAccount = $result['found'][1];
798787
$fromAccount['balance'] -= $amount;
799788
$toAccount['balance'] += $amount;
800789
$transaction->updateBatch([$fromAccount, $toAccount]);

0 commit comments

Comments
 (0)