32
32
* Performs a read-write transaction to update two sample records in the
33
33
* database.
34
34
*
35
- * This will transfer 100 ,000 from the `MarketingBudget` field for the second
36
- * Album to the first Album. If the `MarketingBudget` is too low, it will
37
- * raise an exception.
35
+ * This will transfer 200 ,000 from the `MarketingBudget` field for the second
36
+ * Album to the first Album. If the `MarketingBudget` for the second Album is
37
+ * too low, it will raise an exception.
38
38
*
39
39
* Before running this sample, you will need to run the `update_data` sample
40
40
* to populate the fields.
@@ -53,6 +53,8 @@ function read_write_transaction($instanceId, $databaseId)
53
53
$ database = $ instance ->database ($ databaseId );
54
54
55
55
$ database ->runTransaction (function (Transaction $ t ) use ($ spanner ) {
56
+ $ transferAmount = 200000 ;
57
+
56
58
// Read the second album's budget.
57
59
$ secondAlbumKey = [2 ,2 ];
58
60
$ secondAlbumKeySet = $ spanner ->keySet (['keys ' => [$ secondAlbumKey ]]);
@@ -65,10 +67,10 @@ function read_write_transaction($instanceId, $databaseId)
65
67
66
68
$ firstRow = $ secondAlbumResult ->rows ()->current ();
67
69
$ secondAlbumBudget = $ firstRow ['MarketingBudget ' ];
68
- if ($ secondAlbumBudget < 300000 ) {
70
+ if ($ secondAlbumBudget < $ transferAmount ) {
69
71
// Throwing an exception will automatically roll back the transaction.
70
72
throw new UnexpectedValueException (
71
- 'The second album \'s budget doesn \' t meet the required minimum '
73
+ 'The second album \'s budget is lower than the transfer amount: ' . $ transferAmount
72
74
);
73
75
}
74
76
@@ -86,24 +88,21 @@ function read_write_transaction($instanceId, $databaseId)
86
88
$ firstAlbumBudget = $ firstRow ['MarketingBudget ' ];
87
89
88
90
// Update the budgets.
89
- $ transferAmount = 100000 ;
90
- if ($ firstAlbumBudget >= $ transferAmount ) {
91
- $ secondAlbumBudget += $ transferAmount ;
92
- $ firstAlbumBudget -= $ transferAmount ;
93
- printf ('Setting first album \'s budget to %s and the second album \'s ' .
94
- 'budget to %s. ' . PHP_EOL , $ firstAlbumBudget , $ secondAlbumBudget );
91
+ $ secondAlbumBudget -= $ transferAmount ;
92
+ $ firstAlbumBudget += $ transferAmount ;
93
+ printf ('Setting first album \'s budget to %s and the second album \'s ' .
94
+ 'budget to %s. ' . PHP_EOL , $ firstAlbumBudget , $ secondAlbumBudget );
95
95
96
- // Update the rows.
97
- $ t ->updateBatch ('Albums ' , [
98
- ['SingerId ' => 1 , 'AlbumId ' => 1 , 'MarketingBudget ' => $ firstAlbumBudget ],
99
- ['SingerId ' => 2 , 'AlbumId ' => 2 , 'MarketingBudget ' => $ secondAlbumBudget ],
100
- ]);
96
+ // Update the rows.
97
+ $ t ->updateBatch ('Albums ' , [
98
+ ['SingerId ' => 1 , 'AlbumId ' => 1 , 'MarketingBudget ' => $ firstAlbumBudget ],
99
+ ['SingerId ' => 2 , 'AlbumId ' => 2 , 'MarketingBudget ' => $ secondAlbumBudget ],
100
+ ]);
101
101
102
- // Commit the transaction!
103
- $ t ->commit ();
102
+ // Commit the transaction!
103
+ $ t ->commit ();
104
104
105
- print ('Transaction complete. ' . PHP_EOL );
106
- }
105
+ print ('Transaction complete. ' . PHP_EOL );
107
106
});
108
107
}
109
108
// [END spanner_read_write_transaction]
0 commit comments