Membuat Koleksi dan Database ArangoDB di PHP

Ditulis oleh Muhammad Arslan, dipublikasi pada 28 Oct 2017 dalam kategori Tutorial
Membuat Koleksi dan Database ArangoDB di PHP - CodePolitan.com

Setelah berhasil mendapatkan modul triagens/arangodb, kamu dapat mulai menggunakan library tersebut di dalam kode PHP atau web framework PHP kamu. Kamu juga dapat menggunakan Collection dan CollectionHandler untuk membuat collection di ArangoDB.

Mempelajari Fitur Generator dan Implementasinya dalam PHP

Berikut adalah contoh script PHP untuk membuat koleksi di ArangoDB:

<?php

require dirname(__FILE__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';

use ArangoDBClient\Connection as ArangoConnection;
use ArangoDBClient\ConnectionOptions as ArangoConnectionOptions;
use ArangoDBClient\UpdatePolicy as ArangoUpdatePolicy;
use ArangoDBClient\Collection as ArangoCollection;
use ArangoDBClient\CollectionHandler as ArangoCollectionHandler;
use ArangoDBClient\Document as ArangoDocument;
use ArangoDBClient\DocumentHandler as ArangoDocumentHandler;

$connectionOptions =array(
    // 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 => 'root',
    // connection persistence on server. can use either 'Close' (one-time connections) or 'Keep-Alive' (re-used connections)
    ArangoConnectionOptions::OPTION_CONNECTION => 'Close',
    // 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,

    "database" => "example"
);

// open connection
$connection = new ArangoConnection($connectionOptions);

// create a new collection
$colName = "Cities";
$col = new ArangoCollection($colName);
$colHandler = new ArangoCollectionHandler($connection);

if (!$colHandler->has($colName)) {
    $colHandler->create($col);
}

Silahkan jalankan script diatas, dan periksalah ke ArangoDB Web Admin:

$ php create-collection.php


Berlangganan Informasi CodePolitan Melalui Email

Jangan sampai kamu melewatkan informasi penting tentang pemrograman dan teknologi! Kamu bisa berlangganan Newsletter CodePolitan dengan cara mengisi formulir di samping.