🐕

Laravel環境でArangoDB-PHP使うまで

2021/12/07に公開

ArangoDB-PHP

ArangoDB-PHP
ArangoDB PHP Driverを使ってアクセスするまで

Laravel(windows+php8.0)でArangoDB接続までの解説

php環境

php.ini

;extension=bz2
extension=curl
;extension=ffi
;extension=ftp
extension=fileinfo
extension=gd
;extension=gettext
;extension=gmp
;extension=intl
;extension=imap
;extension=ldap
extension=mbstring
;extension=exif      ; Must be after mbstring as it depends on it
;extension=mysqli
;extension=oci8_12c  ; Use with Oracle Database 12c Instant Client
;extension=oci8_19  ; Use with Oracle Database 19 Instant Client
;extension=odbc
extension=openssl
;extension=pdo_firebird
;extension=pdo_mysql
;extension=pdo_oci
;extension=pdo_odbc
;extension=pdo_pgsql
extension=pdo_sqlite
;extension=pgsql
;extension=shmop

composer

Download Composer

プロジェクト作成

composer create-project --prefer-dist laravel/laravel blog

ArangoDBドライバインストール

composer require triagens/arangodb

Autoload

composer.jsonにArangoDBClientを読み込む設定を追加

    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/",
            "ArangoDBClient\\": "triagens/arangodb/"
        }
    },

composer dumpautoloadでAutoload反映

using設定

// ArangoDB用
use ArangoDBClient\Collection as ArangoCollection;
use ArangoDBClient\CollectionHandler as ArangoCollectionHandler;
use ArangoDBClient\Connection as ArangoConnection;
use ArangoDBClient\ConnectionOptions as ArangoConnectionOptions;
use ArangoDBClient\DocumentHandler as ArangoDocumentHandler;
use ArangoDBClient\Document as ArangoDocument;
use ArangoDBClient\Exception as ArangoException;
use ArangoDBClient\Export as ArangoExport;
use ArangoDBClient\ConnectException as ArangoConnectException;
use ArangoDBClient\ClientException as ArangoClientException;
use ArangoDBClient\ServerException as ArangoServerException;
use ArangoDBClient\Statement as ArangoStatement;
use ArangoDBClient\UpdatePolicy as ArangoUpdatePolicy;

使用方法

    $connectionOptions = [
        // database name
        ArangoConnectionOptions::OPTION_DATABASE => '_system',
        // server endpoint to connect to
        ArangoConnectionOptions::OPTION_ENDPOINT => 'tcp://127.0.0.1:8529',
        // authorization type to use (currently supported: 'Basic')
        ArangoConnectionOptions::OPTION_AUTH_TYPE => 'Basic',
        // user for basic authorization
        ArangoConnectionOptions::OPTION_AUTH_USER => 'root',
        // password for basic authorization
        ArangoConnectionOptions::OPTION_AUTH_PASSWD => '',
        // connection persistence on server. can use either 'Close' (one-time connections) or 'Keep-Alive' (re-used connections)
        ArangoConnectionOptions::OPTION_CONNECTION => 'Keep-Alive',
        // connect timeout in seconds
        ArangoConnectionOptions::OPTION_TIMEOUT => 3,
        // whether or not to reconnect when a keep-alive connection has timed out on server
        ArangoConnectionOptions::OPTION_RECONNECT => true,
        // optionally create new collections when inserting documents
        ArangoConnectionOptions::OPTION_CREATE => true,
        // optionally create new collections when inserting documents
        ArangoConnectionOptions::OPTION_UPDATE_POLICY => ArangoUpdatePolicy::LAST,
    ];
    $connection = new ArangoConnection($connectionOptions);

    // now run another query on the data, using bind parameters
    $statement = new ArangoStatement(
        $connection, [
            'query' => 'FOR i IN 1..2 RETURN { key: i, value: i + @value }',
            'bindVars' => [
                'value' => 10,
            ],
        ],
    );

    // executing the statement returns a cursor
    $cursor = $statement->execute();

    // easiest way to get all results returned by the cursor
    $values = $cursor->getAll();

    // var_dump($values);
    foreach($values as $value) {
        var_dump($value->getAll());
    }

結果

array(2) { ["key"]=> int(1) ["value"]=> int(11) } array(2) { ["key"]=> int(2) ["value"]=> int(12) }

Discussion