3232 * Performs a read-write transaction to update two sample records in the
3333 * database.
3434 *
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
3636 * Album to the first Album. If the `MarketingBudget` is too low, it will
3737 * raise an exception.
3838 *
@@ -68,7 +68,7 @@ function read_write_transaction($instanceId, $databaseId)
6868 if ($ secondAlbumBudget < 300000 ) {
6969 // Throwing an exception will automatically roll back the transaction.
7070 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 '
7272 );
7373 }
7474
@@ -86,22 +86,24 @@ function read_write_transaction($instanceId, $databaseId)
8686 $ firstAlbumBudget = $ firstRow ['MarketingBudget ' ];
8787
8888 // 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 );
9495
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+ ]);
100101
101- // Commit the transaction!
102- $ t ->commit ();
103- });
102+ // Commit the transaction!
103+ $ t ->commit ();
104104
105- print ('Transaction complete. ' . PHP_EOL );
105+ print ('Transaction complete. ' . PHP_EOL );
106+ }
107+ });
106108}
107109// [END spanner_read_write_transaction]
0 commit comments