devgroup / yii2-arangodb
Yii2 arangodb 组件
Requires
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-10 02:05:46 UTC
README
本扩展为 Yii2 框架提供对 ArangoDB 的集成。
安装
此扩展需要 ArangoDB PHP 扩展
安装此扩展的首选方式是通过 composer。
运行以下命令
php composer.phar require --prefer-dist devgroup/yii2-arangodb "*"
或将以下代码添加到您的 composer.json 文件的 require 部分:
"devgroup/yii2-arangodb": "*"
...
通用用法
要使用此扩展,请将以下代码添加到您的应用程序配置中:
return [ //.... 'components' => [ 'arangodb' => [ 'class' => '\devgroup\arangodb\Connection', 'connectionOptions' => [ triagens\ArangoDb\ConnectionOptions::OPTION_DATABASE => "mydatabase", triagens\ArangoDb\ConnectionOptions::OPTION_ENDPOINT => 'tcp://127.0.0.1:8529', //triagens\ArangoDb\ConnectionOptions::OPTION_AUTH_USER => '', //triagens\ArangoDb\ConnectionOptions::OPTION_AUTH_PASSWD => '', ], ], ], ];
使用连接实例,您可以访问数据库、集合和文档。
要执行 "find" 查询,您应使用 [[\devgroup\arangodb\Query]]。
use devgroup\arangodb\Query; $query = new Query; // compose the query $query->select(['name', 'status']) ->from('customer') ->limit(10); // execute the query $rows = $query->all();
使用 ArangoDB ActiveRecord
本扩展提供与 [[\yii\db\ActiveRecord]] 类似的 ActiveRecord 解决方案。要声明 ActiveRecord 类,您需要扩展 [[\devgroup\arangodb\ActiveRecord]] 并实现 collectionName
和 'attributes' 方法。
use devgroup\arangodb\ActiveRecord; class Customer extends ActiveRecord { /** * @return string the name of the index associated with this ActiveRecord class. */ public static function collectionName() { return 'customer'; } /** * @return array list of attribute names. */ public function attributes() { return ['_key', 'name', 'email', 'address', 'status']; } }
注意:集合主键名称 ('_key') 应始终显式设置为属性。
您可以使用 [[\yii\data\ActiveDataProvider]] 与 [[\devgroup\arangodb\Query]] 和 [[\devgroup\arangodb\ActiveQuery]]。
use yii\data\ActiveDataProvider; use devgroup\arangodb\Query; $query = new Query; $query->from('customer')->where(['status' => 2]); $provider = new ActiveDataProvider([ 'query' => $query, 'pagination' => [ 'pageSize' => 10, ] ]); $models = $provider->getModels();
use yii\data\ActiveDataProvider; use app\models\Customer; $provider = new ActiveDataProvider([ 'query' => Customer::find(), 'pagination' => [ 'pageSize' => 10, ] ]); $models = $provider->getModels();
使用迁移
ArangoDB 迁移通过 [[devgroup\arangodb\console\controllers\MigrateController]] 管理,它是常规 [[\yii\console\controllers\MigrateController]] 的类似物。
为了启用此命令,您应调整控制台应用程序的配置。
return [ // ... 'controllerMap' => [ 'arangodb-migrate' => 'devgroup\arangodb\console\controllers\MigrateController' ], ];
以下是此命令的一些常见用法:
# creates a new migration named 'create_user_collection'
yii arangodb-migrate/create create_user_collection
# applies ALL new migrations
yii arangodb-migrate
# reverts the last applied migration
yii arangodb-migrate/down
使用调试面板
将 ArangoDb 面板添加到您的 yii\debug\Module 配置中。
return [ 'bootstrap' => ['debug'], 'modules' => [ 'debug' => 'yii\debug\Module', 'panels' => [ 'arango' => [ 'class' => 'devgroup\arangodb\panels\arangodb\ArangoDbPanel', ], ], ... ], ... ];