jsantos / arango-silex
用于轻松处理 ArangoDB 的模型
dev-master
2017-01-23 12:40 UTC
Requires
- triagens/arangodb: ^3.1
Requires (Dev)
- phpunit/phpunit: ^5.7
- silex/silex: ^2.0
This package is auto-updated.
Last update: 2024-09-22 15:07:48 UTC
README
我开发了一个 模型 和 ServiceProvider,它使用 ArangoDB 的官方库来处理 PHP,从而简化了在应用程序中处理数据的操作。
将 Arango-Silex 添加到我的应用程序
composer require jsantos/arango-silex dev-master
目录结构
在 src 中存在两个目录,ArangoModel 和 Provider。
- 在 Provider 中有数据库连接服务(ConnectionArangoServiceProvider.php)。
- 在 ArangoModel 中有 Model(ArangoModel.php),其中包含帮助操作应用程序数据的特性。
配置与 ArangoDB 的连接
在您的应用程序中,选择一个您喜欢的目录来创建与 ArangoDB 的连接文件,选择目录后,按照以下步骤操作。示例:创建连接文件。
我们将创建一个名为 ConnectionArango 的 class,它扩展 ConnectionArangoServiceprovider 并初始化 ArangoDB 数据库的一些配置。我们只需要 3 个属性,$database、$authUser 和 $authPassword。
所有
- protected $database
- protected $endpoint
- protected $authType
- protected $authUser
- protected $authPassword
- protected $connection
- protected $timeout
- protected $reconnect
- protected $create
- protected $updatePolicy
<?php namespace Exemplo\Provider; use JSantos\Provider\ConnectionArangoServiceProvider; class ConnectionArango extends ConnectionArangoServiceProvider { /** * Nome de seu Banco de Dados ArangoDB * @var string */ protected $database = "meu_banco"; /** * Usuario de seu Banco de Dados ArangoDB * @var string */ protected $authUser = "meu_usuario"; /** * Senha de seu Banco de Dados ArangoDB * @var string */ protected $authPassword = "minha_senha"; }
目录/文件:exemplo/Provider/ConnectionArango.php
在 Silex 中注册连接
注册我们刚刚创建的 ConnectionArango。
<?php chdir(dirname(__DIR__)); require "vendor/autoload.php"; use Silex\Application; $app = new Application; $app['debug'] = true; //Registrando Service Controller $app->register(new \Silex\Provider\ServiceControllerServiceProvider()); //Registrando Conexao com ArangoDB $app->register(new \Exemplo\Provider\ConnectionArango()); $app->run();
如何使用 ArangoModel
首先,我们需要注册 ArangoModel ServiceProvider(ArangoModelServiceProvider)。
<?php chdir(dirname(__DIR__)); require "vendor/autoload.php"; use Silex\Application; $app = new Application; $app['debug'] = true; //Registrando Service Controller $app->register(new \Silex\Provider\ServiceControllerServiceProvider()); //Registrando Conexao com ArangoDB $app->register(new \Exemplo\Provider\ConnectionArango()); //Registrando Service Provider de ArangoModel $app->register(new \JSantos\Provider\ArangoModelServiceProvider()); $app->run();
ArangoModel 方法
- createCollection(string $newCollection)
- hasCollection(string $nameCollection)
- deleteCollection(string $nameCollection, array $data = [])
- createDocument(string $nameCollection, array $data)
- lastInsertId()
- hasDocument(string $nameCollection,$idDocument)
- getDocument(string $nameCollection, $idDocument)
- updateDocument(string $nameCollection, $idDocument, array $data)
- removeAttributeDocument(Document|array $document, string $attribute)
- replaceDocument(string $nameCollection, Document $currentDocument, Document $newDocument)
- removeDocument(Document|array $document)
- prepare(string $queryAQL)
- bindCollection(array $bindCollection)
- bindValue(array $bindValue)
- execute()
- query(string $queryAQL)
- searchInDocument(string $nameCollection, array $document)
创建 Collection
<?php $arango = $this->app['arango.model']; $newCollection = "users"; if (!$arango->createCollection($newCollection)) { echo "error"; } echo "success!";
检查 Collection 是否已存在
<?php $arango = $this->app['arango.model']; $myCollection = "users"; if ($arango->hasCollection($myCollection)) { echo "exist"; } echo "no exist";
删除 Collection
<?php $arango = $this->app['arango.model']; $myCollection = "users"; if ($arango->hasCollection($myCollection)) { $arango->deleteCollection($myCollection); }
在 Collection 中创建文档
<?php $arango = $this->app['arango.model']; $collection = "users"; if (!$arango->hasCollection($collection)) { $arango->createCollection($collection); } $data = [ "email" => "jacsonk47@gmail.com", "password" => password_hash("jacson", PASSWORD_BCRYPT, ["cost"=>12]) ]; $idDocument = $arango->createDocument($collection,$data);