GraphAware\Neo4j\Client\HttpDriver\Session::pushToTransaction PHP Method

pushToTransaction() public method

public pushToTransaction ( integer $transactionId, array $statementsStack ) : GraphAware\Common\Result\ResultCollection
$transactionId integer
$statementsStack array
return GraphAware\Common\Result\ResultCollection
    public function pushToTransaction($transactionId, array $statementsStack)
    {
        $statements = [];
        foreach ($statementsStack as $statement) {
            $st = ['statement' => $statement->text(), 'resultDataContents' => ['REST', 'GRAPH'], 'includeStats' => true];
            if (!empty($statement->parameters())) {
                $st['parameters'] = $this->formatParams($statement->parameters());
            }
            $statements[] = $st;
        }
        $headers = [['X-Stream' => true, 'Content-Type' => 'application/json']];
        $body = json_encode(['statements' => $statements]);
        $request = new Request('POST', sprintf('%s/db/data/transaction/%d', $this->uri, $transactionId), $headers, $body);
        try {
            $response = $this->httpClient->send($request);
            $data = json_decode((string) $response->getBody(), true);
            if (!empty($data['errors'])) {
                $msg = sprintf('Neo4j Exception with code "%s" and message "%s"', $data['errors'][0]['code'], $data['errors'][0]['message']);
                $exception = new Neo4jException($msg);
                $exception->setNeo4jStatusCode($data['errors'][0]['code']);
                throw $exception;
            }
            return $this->responseFormatter->format(json_decode($response->getBody(), true), $statementsStack);
        } catch (RequestException $e) {
            if ($e->hasResponse()) {
                $body = json_decode($e->getResponse()->getBody(), true);
                if (!isset($body['code'])) {
                    throw $e;
                }
                $msg = sprintf('Neo4j Exception with code "%s" and message "%s"', $body['errors'][0]['code'], $body['errors'][0]['message']);
                $exception = new Neo4jException($msg);
                $exception->setNeo4jStatusCode($body['errors'][0]['code']);
                throw $exception;
            }
            throw $e;
        }
    }

Usage Example

 /**
  * @param array $statements
  *
  * @return \GraphAware\Common\Result\ResultCollection
  *
  * @throws Neo4jException
  */
 public function runMultiple(array $statements)
 {
     try {
         return $this->session->pushToTransaction($this->transactionId, $statements);
     } catch (Neo4jException $e) {
         if ($e->effect() === Neo4jException::EFFECT_ROLLBACK) {
             $this->closed = true;
             $this->state = self::ROLLED_BACK;
         }
         throw $e;
     }
 }