Skip to content

Instantly share code, notes, and snippets.

@ikwattro
Created July 25, 2015 19:13
Show Gist options
  • Save ikwattro/65681bfeeb690e344e9c to your computer and use it in GitHub Desktop.
Save ikwattro/65681bfeeb690e344e9c to your computer and use it in GitHub Desktop.
Live Transaction with Neo4j's PHP NeoClient

Using live transactions can have some benefits :

  • Having a result of the transaction for each statement
  • Replicating batch process and benefit of the Cypher Transactional endpoint
  • Using the same transaction between multiple http requests

How-to:

  1. Open the transaction, you don't have to care about transaction ids etc.., all is handled by the transaction object you receive back
$transaction = $client->createTransaction();
// Returns a \Neoxygen\NeoClient\Transaction\Transaction object
  1. Add statements to the transaction :
$query = 'CREATE (user:User {id: {user_id}, name: {user_name} }) RETURN user';
$params = ['user_id' => 3479, 'user_name' => 'Angus Young'];
$result = $transaction->pushQuery($query, $params);
// You receive a result object back, meaning you can reuse the properties of this object in further statements. Note that the 
// transaction is not committed yet
  1. Commit the transaction :
try {
    $transaction->commit();
    $results = $transaction->getResults(); // results of all the statements of this transaction
} catch (\Neoxygen\NeoClient\Exception\Neo4jException $e) {
  // do what you want
  throw new $e;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment