32
32
* Performs a read-write transaction to update two sample records in the
33
33
* database.
34
34
*
35
- * This will transfer 200 ,000 from the `MarketingBudget` field for the second
35
+ * This will transfer 100 ,000 from the `MarketingBudget` field for the second
36
36
* Album to the first Album. If the `MarketingBudget` is too low, it will
37
37
* raise an exception.
38
38
*
@@ -68,7 +68,7 @@ function read_write_transaction($instanceId, $databaseId)
68
68
if ($ secondAlbumBudget < 300000 ) {
69
69
// Throwing an exception will automatically roll back the transaction.
70
70
throw new UnexpectedValueException (
71
- 'The second album doesn \'t have enough funds to transfer '
71
+ 'The second album \' s budget doesn \'t meet the required minimum '
72
72
);
73
73
}
74
74
@@ -86,22 +86,24 @@ function read_write_transaction($instanceId, $databaseId)
86
86
$ firstAlbumBudget = $ firstRow ['MarketingBudget ' ];
87
87
88
88
// Update the budgets.
89
- $ transferAmmount = 200000 ;
90
- $ secondAlbumBudget -= $ transferAmmount ;
91
- $ firstAlbumBudget += $ transferAmmount ;
92
- printf ('Setting first album \'s budget to %s and the second album \'s ' .
93
- 'budget to %s. ' . PHP_EOL , $ firstAlbumBudget , $ secondAlbumBudget );
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 );
94
95
95
- // Update the rows.
96
- $ t ->updateBatch ('Albums ' , [
97
- ['SingerId ' => 1 , 'AlbumId ' => 1 , 'MarketingBudget ' => $ firstAlbumBudget ],
98
- ['SingerId ' => 2 , 'AlbumId ' => 2 , 'MarketingBudget ' => $ secondAlbumBudget ],
99
- ]);
96
+ // Update the rows.
97
+ $ t ->updateBatch ('Albums ' , [
98
+ ['SingerId ' => 1 , 'AlbumId ' => 1 , 'MarketingBudget ' => $ firstAlbumBudget ],
99
+ ['SingerId ' => 2 , 'AlbumId ' => 2 , 'MarketingBudget ' => $ secondAlbumBudget ],
100
+ ]);
100
101
101
- // Commit the transaction!
102
- $ t ->commit ();
103
- });
102
+ // Commit the transaction!
103
+ $ t ->commit ();
104
104
105
- print ('Transaction complete. ' . PHP_EOL );
105
+ print ('Transaction complete. ' . PHP_EOL );
106
+ }
107
+ });
106
108
}
107
109
// [END spanner_read_write_transaction]
0 commit comments