triagens / arangodb
ArangoDB PHP 客户端
Requires
- php: >=5.6.0
- dev-devel
- 3.10.x-dev
- 3.9.x-dev
- 3.8.x-dev
- v3.8.0
- 3.7.x-dev
- 3.6.x-dev
- v3.6.0
- 3.5.x-dev
- v3.5.1
- v3.5.0
- 3.4.x-dev
- v3.4.0
- 3.3.x-dev
- v3.3.9
- v3.3.2
- v3.3.1
- v3.3.0
- 3.2.x-dev
- v3.2.4
- v3.2.0
- 3.1.x-dev
- v3.1.0
- 3.0.x-dev
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.0
- 2.8.x-dev
- v2.8.1
- v2.8.0
- 2.7.x-dev
- v2.7.1
- v2.7.0
- 2.6.x-dev
- v2.6.9
- v2.6.6
- v2.6.1
- v2.6.0
- 2.5.x-dev
- v2.5.2
- v2.5.1
- v2.5.0
- 2.4.x-dev
- v2.4.1
- v2.4.0
- 2.3.x-dev
- v2.3.6
- v2.3.5
- v2.3.4
- 2.2.x-dev
- v2.2.6
- v2.2
- 2.1.x-dev
- 2.0.x-dev
- v2.0.0
- 1.4.x-dev
- v1.4.0
- 1.3.x-dev
- v1.3.1
- v1.3.0
- 1.2.x-dev
- v1.2.1
- v1.2.0
- v1.2.0-BETA2
- v1.2.0-BETA1
- 1.1.x-dev
- v1.1.1
- v1.1.0
- 1.0.x-dev
- v1.0.1
- v1.0.0
- v0.3
- v0.2.1
- v0.2
- v0.1.3
- v0.1.2
- v0.1.1
- v0.0.1
- dev-master
- dev-feature/DE-104
- dev-feature/php-8
- dev-feature/test-more-php-versions
- dev-feature-3.7/test-more-php-versions
- dev-feature/disable-mmfiles-only-tests
- dev-bug-fix/adjust-error-codes-in-tests
- dev-bug-fix-3.7/adjust-error-code-in-tests
- dev-feature/issue-243
- dev-documentation/add-doc-link
- dev-documentation/fix-links
- dev-documentation/autogenerate-phpdoc-master
- dev-documentation/autogenerate-phpdoc
- dev-documentation/php-refactor
- dev-feature/add-automatic-failover
- dev-improvements_of_tests
- dev-connection_tests_close_and_keep-alive
- dev-feature/issue-235
This package is not auto-updated.
Last update: 2024-09-14 12:42:51 UTC
README
这个 ArangoDB 驱动器,称为 ArangoDB-PHP 客户端,允许通过 REST 访问服务器上的文档。应使用 DocumentHandler 类来实现这些目的。在 examples/document.php
文件中有基于 REST 的文档访问示例。
此外,PHP 客户端还允许使用 Statement 类发出更复杂的 AQL 查询。在 examples/aql-query.php
文件中有这种语句的示例。
要使用 PHP 客户端,您必须包含主目录中的 autoloader.php
文件。自动加载器会动态加载所需的其他类。自动加载器可以嵌套在其他自动加载器中。
ArangoDB PHP 客户端是一个 API,允许您从 PHP 应用程序外部发送和检索 ArangoDB 中的文档。客户端库本身是用 PHP 编写的,没有其他依赖项,只需纯 PHP 5.6(或更高版本)。
客户端库提供文档和集合类,您可以使用它们以面向对象的方式处理文档和集合。在服务器交换文档数据时,库内部使用 ArangoDB 的 HTTP REST 接口。库用户无需关心这一点,因为客户端库抽象了所有 REST 接口的细节。
需求
- PHP 版本 5.6 或更高
关于 PHP 版本支持的说明
一旦旧 PHP 版本达到生命周期的结束状态,此驱动程序将停止支持这些版本。支持将在下一次发布的下一个次要版本或补丁版本中删除。
通常,建议始终使用最新版本的 PHP,以利用所有改进(特别是在性能方面)。
ArangoDB-PHP 重要版本信息
ArangoDB-PHP 驱动程序版本必须与 ArangoDB 版本匹配
- ArangoDB-PHP v3.8.x 与 ArangoDB v3.8.x 的功能相当
- ArangoDB-PHP v3.9.x 与 ArangoDB v3.9.x 的功能相当
等等。
使用 Composer 或 Git 进行安装
要开始,您需要 PHP 5.6 或更高版本,以及在任何您能访问的主机上运行的 ArangoDB 服务器。
有两种获取 ArangoDB PHP 客户端的方法
- 使用 Composer
- 克隆 Git 存储库
替代方案 1:使用 Composer
如果您使用 Composer,可以在命令行中运行以下命令来安装 PHP 客户端
composer require triagens/arangodb
替代方案 2:克隆 Git 存储库
如果您选择此替代方案,则需要安装 Git 客户端。要从 GitHub 克隆 ArangoDB-PHP 客户端存储库,请在项目目录中执行以下命令
git clone "https://github.com/arangodb/arangodb-php.git"
这将在您的当前目录中创建一个名为 arangodb-php
的子目录。它包含客户端库的所有文件。它还包括一个专用的自动加载器,您可以使用它来自动加载客户端库的类文件。要调用此自动加载器,请将以下行添加到需要库的PHP文件中
require 'arangodb-php/autoload.php';
ArangoDB-PHP客户端的自动加载器仅关注其自己的类文件,不处理任何其他文件。这意味着它可以完全与其他自动加载器嵌套。
如果您不想包含autoload.php来加载和设置自动加载器,可以直接调用自动加载器
require 'arangodb-php/lib/ArangoDBClient/autoloader.php'; \ArangoDBClient\Autoloader::init();
设置连接
为了使用ArangoDB,您需要指定连接选项。您可以通过创建一个名为$connectionOptions
的PHP数组来实现。将此代码放入当前目录中名为test.php
的文件中
// use the following line when using Composer // require __DIR__ . '/vendor/composer/autoload.php'; // use the following line when using git require __DIR__ . '/arangodb-php/autoload.php'; // set up some aliases for less typing later 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\ConnectException as ArangoConnectException; use ArangoDBClient\ClientException as ArangoClientException; use ArangoDBClient\ServerException as ArangoServerException; use ArangoDBClient\Statement as ArangoStatement; use ArangoDBClient\UpdatePolicy as ArangoUpdatePolicy; // set up some basic connection options $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, ]; // turn on exception logging (logs to whatever PHP is configured) ArangoException::enableLogging(); $connection = new ArangoConnection($connectionOptions);
这使得客户端连接到运行在本地的ArangoDB
- (
OPTION_HOST
) - 默认端口8529(
OPTION_PORT
) - 连接超时为3秒(
OPTION_TIMEOUT
)
当在尚不存在的集合中创建新文档时,您有以下选择
-
自动生成新集合:如果您希望这样做,请将
OPTION_CREATE
设置为true
-
出错失败:如果您希望这种行为,请将
OPTION_CREATE
设置为false
当更新一个之前/同时被其他用户更新的文档时,您可以选择以下行为之一
-
最后更新获胜:如果您喜欢这种行为,请将
OPTION_UPDATE_POLICY
设置为last -
冲突错误失败:如果您喜欢这种行为,请将
OPTION_UPDATE_POLICY
设置为conflict
设置主动故障转移
默认情况下,PHP客户端仅连接到单个端点,通过在连接选项中指定端点的字符串值来实现,例如
$connectionOptions = [ ArangoConnectionOptions::OPTION_ENDPOINT => 'tcp://127.0.0.1:8529' ];
要设置多个服务器以连接,也可以指定服务器数组
$connectionOptions = [ ArangoConnectionOptions::OPTION_ENDPOINT => [ 'tcp://localhost:8531', 'tcp://localhost:8532', 'tcp://localhost:8530' ] ];
使用此选项需要ArangoDB 3.3或更高版本,并且数据库在主动故障转移模式下运行。
驱动程序默认尝试连接到端点数组中的第一个服务器端点,只有在无法建立连接时才会尝试以下服务器。如果无法连接到任何服务器,驱动程序会抛出异常。
由于驱动程序不知道数组中的哪个服务器是当前领导者,因此驱动程序默认按数组顺序连接到指定的服务器。然而,为了节省几次不必要的连接尝试到失败的服务器,可以设置服务器列表的缓存(使用Memcached)。缓存的值包含最后一个工作的服务器,这样就需要尽可能少的连接尝试。
为了使用此缓存,需要安装PHP的Memcached模块,并在连接选项中设置以下相关选项
$connectionOptions = [ // memcached persistent id (will be passed to Memcached::__construct) ArangoConnectionOptions::OPTION_MEMCACHED_PERSISTENT_ID => 'arangodb-php-pool', // memcached servers to connect to (will be passed to Memcached::addServers) ArangoConnectionOptions::OPTION_MEMCACHED_SERVERS => [ [ '127.0.0.1', 11211 ] ], // memcached options (will be passed to Memcached::setOptions) ArangoConnectionOptions::OPTION_MEMCACHED_OPTIONS => [ ], // key to store the current endpoints array under ArangoConnectionOptions::OPTION_MEMCACHED_ENDPOINTS_KEY => 'arangodb-php-endpoints' // time-to-live for the endpoints array stored in memcached ArangoConnectionOptions::OPTION_MEMCACHED_TTL => 600 ];
创建集合
这只是为了展示如何创建集合。对于这些示例,在插入文档之前不需要创建集合,因为我们已经将
ArangoConnectionOptions::OPTION_CREATE
设置为true
。
因此,在获取设置后,我们可以开始创建集合。我们创建一个名为users的集合。
下面的代码首先在变量名$user
中设置集合,然后将其推送到服务器,并返回服务器创建的集合ID
$collectionHandler = new ArangoCollectionHandler($connection); // clean up first if ($collectionHandler->has('users')) { $collectionHandler->drop('users'); } if ($collectionHandler->has('example')) { $collectionHandler->drop('example'); } // create a new collection $userCollection = new ArangoCollection(); $userCollection->setName('users'); $id = $collectionHandler->create($userCollection); // print the collection id created by the server var_dump($id); // check if the collection exists $result = $collectionHandler->has('users'); var_dump($result);
创建文档
在创建集合后,我们可以开始创建初始文档。我们在名为users的集合中创建一个用户文档。此集合尚不需要存在。我们在此集合中插入的第一个文档会动态创建集合。这是因为我们在$connectionOptions
中已将OPTION_CREATE
设置为true
。
以下代码首先在本地变量$user
中设置文档,然后将其推送到服务器并返回由服务器创建的文档ID。
$handler = new ArangoDocumentHandler($connection); // create a new document $user = new ArangoDocument(); // use set method to set document properties $user->set('name', 'John'); $user->set('age', 25); $user->set('thisIsNull', null); $user->set('obj', ['nested' => True]); // use magic methods to set document properties $user->likes = ['fishing', 'hiking', 'swimming']; // send the document to the server $id = $handler->save('users', $user); // check if a document exists $result = $handler->has('users', $id); var_dump($result); // print the document id created by the server var_dump($id); var_dump($user->getId());
可以通过使用set()
方法或直接操作文档属性来设置文档属性。
如您所见,通过在客户端库的DocumentHandler类的save()
方法上调用,实现了向服务器发送文档。它需要集合名称(本例中为users)以及要保存的文档对象。 save()
返回由服务器创建的文档ID。该ID是一个可能或不可能适合PHP整数的数值。
添加异常处理
上面的代码可以工作,但它没有检查任何错误。为了使其在出现错误时也能工作,我们将其包装在一些基本的异常处理程序中。
try { $handler = new ArangoDocumentHandler($connection); // create a new document $user = new ArangoDocument(); // use set method to set document properties $user->set('name', 'John'); $user->set('age', 25); // use magic methods to set document properties $user->likes = ['fishing', 'hiking', 'swimming']; // send the document to the server $id = $handler->save('users', $user); // check if a document exists $result = $handler->has('users', $id); var_dump($result); // print the document id created by the server var_dump($id); var_dump($user->getId()); } catch (ArangoConnectException $e) { print 'Connection error: ' . $e->getMessage() . PHP_EOL; } catch (ArangoClientException $e) { print 'Client error: ' . $e->getMessage() . PHP_EOL; } catch (ArangoServerException $e) { print 'Server error: ' . $e->getServerCode() . ':' . $e->getServerMessage() . ' ' . $e->getMessage() . PHP_EOL; }
检索文档
要从一个服务器检索文档,可以使用DocumentHandler类的get()
方法。它需要集合名称(本例中为)加上文档ID。还有一个getById()
方法,它是get()
的一个别名。
// get the document back from the server $userFromServer = $handler->get('users', $id); var_dump($userFromServer); /* The result of the get() method is a Document object that you can use in an OO fashion: object(ArangoDBClient\Document)##6 (4) { ["_id":"ArangoDBClient\Document":private]=> string(15) "2377907/4818344" ["_rev":"ArangoDBClient\Document":private]=> int(4818344) ["_values":"ArangoDBClient\Document":private]=> array(3) { ["age"]=> int(25) ["name"]=> string(4) "John" ["likes"]=> array(3) { [0]=> string(7) "fishing" [1]=> string(6) "hiking" [2]=> string(8) "swimming" } } ["_changed":"ArangoDBClient\Document":private]=> bool(false) } */
每当文档ID尚不明确,但您想通过任何其他属性从服务器获取文档时,可以使用CollectionHandler->byExample()
方法。它允许您提供要查找的文档的示例。示例可以是具有相关属性的Document对象,或者是一个包含您要查找的属性的PHP数组。
// get a document list back from the server, using a document example $cursor = $collectionHandler->byExample('users', ['name' => 'John']); var_dump($cursor->getAll());
这返回了具有示例中提供的属性(本例中具有属性name
的值"John"
)的指定集合(此处为users)中的所有文档。结果是可迭代的游标,可以顺序迭代或完全迭代。我们选择通过调用游标的getAll()
方法来获取上面的完整结果集。
如果示例不明确,CollectionHandler->byExample()
会返回多个文档。
更新文档
要更新现有文档,可以使用DocumentHandler类的update()
方法。在本例中,我们想
- 将状态设置为
'CA'
- 更改
likes
数组
// update a document $userFromServer->likes = ['fishing', 'swimming']; $userFromServer->state = 'CA'; $result = $handler->update($userFromServer); var_dump($result); $userFromServer = $handler->get('users', $id); var_dump($userFromServer);
要使用update()
方法删除属性,必须传递一个选项,告知它不要保留具有null值的属性。在本例中,我们想删除age
。
// update a document removing an attribute, // The 'keepNull'=>false option will cause ArangoDB to // remove all attributes in the document, // that have null as their value - not only the ones defined here $userFromServer->likes = ['fishing', 'swimming']; $userFromServer->state = 'CA'; $userFromServer->age = null; $result = $handler->update($userFromServer, ['keepNull' => false]); var_dump($result); $userFromServer = $handler->get('users', $id); var_dump($userFromServer);
使用前面的示例更新的文档必须在之前从服务器获取。如果您想要更新一个尚未从服务器获取的文档,请使用updateById()
。
// update a document, identified by collection and document id $user = new ArangoDocument(); $user->name = 'John'; $user->likes = ['Running', 'Rowing']; // Notice that for the example we're getting the existing // document id via a method call. Normally we would use the known id $result = $handler->updateById('users', $userFromServer->getId(), $user); var_dump($result); $userFromServer = $handler->get('users', $id); var_dump($userFromServer);
替换文档
要完全替换现有文档,可以使用DocumentHandler类的replace()
方法。在本例中,我们想删除state
属性。
// replace a document (notice that we are using the previously fetched document) // In this example we are removing the state attribute unset($userFromServer->state); $result = $handler->replace($userFromServer); var_dump($result); $userFromServer = $handler->get('users', $id); var_dump($userFromServer);
使用前面的示例替换的文档必须在之前从服务器获取。如果您想要替换一个尚未从服务器获取的文档,请使用replaceById()
。
// replace a document, identified by collection and document id $user = new ArangoDocument(); $user->name = 'John'; $user->likes = ['Running', 'Rowing']; // Notice that for the example we're getting the existing // document id via a method call. Normally we would use the known id $result = $handler->replaceById('users', $userFromServer->getId(), $user); var_dump($result); $userFromServer = $handler->get('users', $id); var_dump($userFromServer);
删除文档
要删除服务器上的现有文档,可以使用DocumentHandler类的remove()
方法。remove()
只需要要删除的文档作为参数。
// remove a document on the server, using a document object $result = $handler->remove($userFromServer); var_dump($result);
注意,文档必须在之前从服务器获取。如果您之前没有获取文档,请使用removeById()
方法。这只需要集合名称(此处为users)和文档ID。
// remove a document on the server, using a collection id and document id // In this example, we are using the id of the document we deleted in the previous example, // so it will throw an exception here. (we are catching it though, in order to continue) try { $result = $handler->removeById('users', $userFromServer->getId()); } catch (\ArangoDBClient\ServerException $e) { $e->getMessage(); }
删除集合
要删除服务器上的现有集合,请使用CollectionHandler类的drop()
方法。drop()
只需要要删除的集合名称。
// drop a collection on the server, using its name, $result = $collectionHandler->drop('users'); var_dump($result); // drop the other one we created, too $collectionHandler->drop('example');
运行AQL查询
要运行AQL查询,请使用Statement类。
方法 Statement::execute
创建一个游标对象,可以用来遍历查询的结果集。
// create a statement to insert 1000 test users $statement = new ArangoStatement( $connection, [ 'query' => 'FOR i IN 1..1000 INSERT { _key: CONCAT("test", i) } IN users' ] ); // execute the statement $cursor = $statement->execute(); // now run another query on the data, using bind parameters $statement = new ArangoStatement( $connection, [ 'query' => 'FOR u IN @@collection FILTER u.name == @name RETURN u', 'bindVars' => [ '@collection' => 'users', 'name' => 'John' ] ] ); // executing the statement returns a cursor $cursor = $statement->execute(); // easiest way to get all results returned by the cursor var_dump($cursor->getAll()); // to get statistics for the query, use Cursor::getExtra(); var_dump($cursor->getExtra());
注意:默认情况下,Statement 对象将创建一个将每个值转换为 Document 对象的游标。这对于返回整个文档的 AQL 查询通常是期望的行为。然而,AQL 查询也可以返回投影或其他无法转换为 Document 对象的数据。
为了阻止转换为 Document 对象,Statement 必须提供 _flat
属性。这允许处理任意 AQL 查询的结果。
// run an AQL query that does not return documents but scalars // we need to set the _flat attribute of the Statement in order for this to work $statement = new ArangoStatement( $connection, [ 'query' => 'FOR i IN 1..1000 RETURN i', '_flat' => true ] ); // executing the statement returns a cursor $cursor = $statement->execute(); // easiest way to get all results returned by the cursor // note that now the results won't be converted into Document objects var_dump($cursor->getAll());
大批量文档处理
ArangoDB-PHP 驱动提供了一种机制,可以轻松地通过单个请求从同一集合中获取多个文档。需要提供的只是一个文档键数组。
$exampleCollection = new ArangoCollection(); $exampleCollection->setName('example'); $id = $collectionHandler->create($exampleCollection); // create a statement to insert 100 example documents $statement = new ArangoStatement( $connection, [ 'query' => 'FOR i IN 1..100 INSERT { _key: CONCAT("example", i), value: i } IN example' ] ); $statement->execute(); // later on, we can assemble a list of document keys $keys = []; for ($i = 1; $i <= 100; ++$i) { $keys[] = 'example' . $i; } // and fetch all the documents at once $documents = $collectionHandler->lookupByKeys('example', $keys); var_dump($documents); // we can also bulk-remove them: $result = $collectionHandler->removeByKeys('example', $keys); var_dump($result);
自定义文档类
如果您想使用自定义文档类,可以通过方法 setDocumentClass
将其名称传递给 DocumentHandler 或 CollectionHandler。请记住,您的类必须扩展 \ArangoDBClient\Document
。
$ch = new CollectionHandler($connection); $ch->setDocumentClass('\AppBundle\Entity\Product'); $cursor = $ch->all('product'); // All returned documents will be \AppBundle\Entity\Product instances $dh = new DocumentHandler($connection); $dh->setDocumentClass('\AppBundle\Entity\Product'); $product = $dh->get('products', 11231234); // Product will be \AppBundle\Entity\Product instance
有关更多详细信息,请参阅 examples/customDocumentClass.php
文件。
记录异常
驱动程序提供了一个简单的日志记录机制,默认情况下是关闭的。如果开启,驱动程序将使用 PHP 的标准 error_log
机制记录所有异常。它调用 PHP 的 error_log()
函数来完成。是否记录异常以及记录位置取决于 PHP 配置。请咨询您的 php.ini
设置以获取更多详细信息。
要在驱动程序中开启异常日志记录,在驱动程序的 Exception 基类上设置一个标志,所有驱动程序异常都从这个基类派生。
use ArangoDBClient\Exception as ArangoException; ArangoException::enableLogging();
要关闭日志记录,调用其 disableLogging
方法。
use ArangoDBClient\Exception as ArangoException; ArangoException::disableLogging();
整合所有内容
以下是结合上述所有部分的完整代码。
// use the following line when using Composer // require __DIR__ . '/vendor/composer/autoload.php'; // use the following line when using git require __DIR__ . '/autoload.php'; // set up some aliases for less typing later 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\ConnectException as ArangoConnectException; use ArangoDBClient\ClientException as ArangoClientException; use ArangoDBClient\ServerException as ArangoServerException; use ArangoDBClient\Statement as ArangoStatement; use ArangoDBClient\UpdatePolicy as ArangoUpdatePolicy; // set up some basic connection options $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, ]; // turn on exception logging (logs to whatever PHP is configured) ArangoException::enableLogging(); try { $connection = new ArangoConnection($connectionOptions); $collectionHandler = new ArangoCollectionHandler($connection); // clean up first if ($collectionHandler->has('users')) { $collectionHandler->drop('users'); } if ($collectionHandler->has('example')) { $collectionHandler->drop('example'); } // create a new collection $userCollection = new ArangoCollection(); $userCollection->setName('users'); $id = $collectionHandler->create($userCollection); // print the collection id created by the server var_dump($id); // check if the collection exists $result = $collectionHandler->has('users'); var_dump($result); $handler = new ArangoDocumentHandler($connection); // create a new document $user = new ArangoDocument(); // use set method to set document properties $user->set('name', 'John'); $user->set('age', 25); $user->set('thisIsNull', null); // use magic methods to set document properties $user->likes = ['fishing', 'hiking', 'swimming']; // send the document to the server $id = $handler->save('users', $user); // check if a document exists $result = $handler->has('users', $id); var_dump($result); // print the document id created by the server var_dump($id); var_dump($user->getId()); // get the document back from the server $userFromServer = $handler->get('users', $id); var_dump($userFromServer); // get a document list back from the server, using a document example $cursor = $collectionHandler->byExample('users', ['name' => 'John']); var_dump($cursor->getAll()); // update a document $userFromServer->likes = ['fishing', 'swimming']; $userFromServer->state = 'CA'; $result = $handler->update($userFromServer); var_dump($result); $userFromServer = $handler->get('users', $id); var_dump($userFromServer); // update a document removing an attribute, // The 'keepNull'=>false option will cause ArangoDB to // remove all attributes in the document, // that have null as their value - not only the ones defined here $userFromServer->likes = ['fishing', 'swimming']; $userFromServer->state = 'CA'; $userFromServer->age = null; $result = $handler->update($userFromServer, ['keepNull' => false]); var_dump($result); $userFromServer = $handler->get('users', $id); var_dump($userFromServer); // replace a document (notice that we are using the previously fetched document) // In this example we are removing the state attribute unset($userFromServer->state); $result = $handler->replace($userFromServer); var_dump($result); $userFromServer = $handler->get('users', $id); var_dump($userFromServer); // replace a document, identified by collection and document id $user = new ArangoDocument(); $user->name = 'John'; $user->likes = ['Running', 'Rowing']; $userFromServer->state = 'CA'; // Notice that for the example we're getting the existing // document id via a method call. Normally we would use the known id $result = $handler->replaceById('users', $userFromServer->getId(), $user); var_dump($result); $userFromServer = $handler->get('users', $id); var_dump($userFromServer); // remove a document on the server $result = $handler->remove($userFromServer); var_dump($result); // remove a document on the server, using a collection id and document id // In this example, we are using the id of the document we deleted in the previous example, // so it will throw an exception here. (we are catching it though, in order to continue) try { $result = $handler->removeById('users', $userFromServer->getId()); } catch (\ArangoDBClient\ServerException $e) { $e->getMessage(); } // create a statement to insert 1000 test users $statement = new ArangoStatement( $connection, [ 'query' => 'FOR i IN 1..1000 INSERT { _key: CONCAT("test", i) } IN users' ] ); // execute the statement $cursor = $statement->execute(); // now run another query on the data, using bind parameters $statement = new ArangoStatement( $connection, [ 'query' => 'FOR u IN @@collection FILTER u.name == @name RETURN u', 'bindVars' => [ '@collection' => 'users', 'name' => 'John' ] ] ); // executing the statement returns a cursor $cursor = $statement->execute(); // easiest way to get all results returned by the cursor var_dump($cursor->getAll()); // to get statistics for the query, use Cursor::getExtra(); var_dump($cursor->getExtra()); $exampleCollection = new ArangoCollection(); $exampleCollection->setName('example'); $id = $collectionHandler->create($exampleCollection); // create a statement to insert 100 example documents $statement = new ArangoStatement( $connection, [ 'query' => 'FOR i IN 1..100 INSERT { _key: CONCAT("example", i), value: i } IN example' ] ); $statement->execute(); // later on, we can assemble a list of document keys $keys = []; for ($i = 1; $i <= 100; ++$i) { $keys[] = 'example' . $i; } // and fetch all the documents at once $documents = $collectionHandler->lookupByKeys('example', $keys); var_dump($documents); // we can also bulk-remove them: $result = $collectionHandler->removeByKeys('example', $keys); var_dump($result); // drop a collection on the server, using its name, $result = $collectionHandler->drop('users'); var_dump($result); // drop the other one we created, too $collectionHandler->drop('example'); } catch (ArangoConnectException $e) { print 'Connection error: ' . $e->getMessage() . PHP_EOL; } catch (ArangoClientException $e) { print 'Client error: ' . $e->getMessage() . PHP_EOL; } catch (ArangoServerException $e) { print 'Server error: ' . $e->getServerCode() . ': ' . $e->getServerMessage() . ' - ' . $e->getMessage() . PHP_EOL; }