-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtransaction-query.php
61 lines (49 loc) · 2.07 KB
/
transaction-query.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace ArangoDBClient;
require __DIR__ . '/init.php';
try {
$connection = new Connection($connectionOptions);
$collectionHandler = new CollectionHandler($connection);
$documentHandler = new DocumentHandler($connection);
$transactionHandler = new StreamingTransactionHandler($connection);
// set up a document collection "users"
$collection = new Collection('users');
try {
$collectionHandler->create($collection);
} catch (\Exception $e) {
// collection may already exist - ignore this error for now
}
// clear everything, so we can start with a clean state
$collectionHandler->truncate($collection);
// creates a transaction object
$trx = new StreamingTransaction($connection, [
TransactionBase::ENTRY_COLLECTIONS => [
TransactionBase::ENTRY_WRITE => 'users'
]
]);
// starts the transaction
$trx = $transactionHandler->create($trx);
// get a StreamingTransactionCollection object. this is used to execute operations
// in a transaction context
$trxCollection = $trx->getCollection('users');
// calling query() directly on the transaction makes the AQL query execute in the
// context of the running transaction
$result = $trx->query([
'query' => 'FOR i IN 1..10 INSERT { _key: CONCAT("test", i), value: i } INTO @@collection',
'bindVars' => [ '@collection' => $trxCollection->getName() ]
]);
echo "BEFORE COMMIT" . PHP_EOL;
echo "COLLECTION COUNT OUTSIDE OF TRANSACTION IS: ", $collectionHandler->count($collection) . PHP_EOL;
echo "COLLECTION COUNT INSIDE OF TRANSACTION IS: ", $collectionHandler->count($trxCollection) . PHP_EOL;
// commits the transaction
$transactionHandler->commit($trx);
echo PHP_EOL;
echo "AFTER COMMIT" . PHP_EOL;
echo "COLLECTION COUNT IS: ", $collectionHandler->count($collection) . PHP_EOL;
} catch (ConnectException $e) {
print $e . PHP_EOL;
} catch (ServerException $e) {
print $e . PHP_EOL;
} catch (ClientException $e) {
print $e . PHP_EOL;
}