mirzaev / yii2-arangodb
用于连接 ArangoDB 和 Yii2 的库
Requires
- php: ^8.0.0
- triagens/arangodb: ~3.2
- yiisoft/yii2: 2.*
Requires (Dev)
README
Yii 2 的 ArangoDb 扩展
此扩展为 Yii2 框架提供了 ArangoDB 的集成。
安装
此扩展需要 ArangoDB PHP 扩展
安装此扩展的首选方式是通过 composer。
运行以下命令
php composer.phar require --prefer-dist explosivebit/yii2-arangodb "*"
或者
"explosivebit/yii2-arangodb": "*"
将以下内容添加到您的 composer.json 的 require 部分。
通用用法
要使用此扩展,请简单地在您的应用程序配置中添加以下代码
return [
//....
'components' => [
'arangodb' => [
'class' => '\explosivebit\arangodb\Connection',
'connectionOptions' => [
ArangoDBClient\ConnectionOptions::OPTION_DATABASE => "mydatabase",
ArangoDBClient\ConnectionOptions::OPTION_ENDPOINT => 'tcp://127.0.0.1:8529',
ArangoDBClient\ConnectionOptions::OPTION_AUTH_TYPE => 'Basic',
//ArangoDBClient\ConnectionOptions::OPTION_AUTH_USER => '',
//ArangoDBClient\ConnectionOptions::OPTION_AUTH_PASSWD => '',
],
],
],
];
使用连接实例,您可以访问数据库、集合和文档。
要执行 "find" 查询,您应使用 [[\explosivebit\arangodb\Query]]
use explosivebit\arangodb\Query;
$query = new Query;
// compose the query
$query->select(['name', 'status'])
->collection('customer')
->limit(10);
// execute the query
$rows = $query->all();
使用 ArangoDB ActiveRecord
此扩展提供与 [[\yii\db\ActiveRecord]] 类似的 ActiveRecord 解决方案。要声明 ActiveRecord 类,您需要扩展 [[\explosivebit\arangodb\ActiveRecord]] 并实现 collectionName
和 'attributes' 方法
use explosivebit\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]] 与 [[\explosivebit\arangodb\Query]] 和 [[\explosivebit\arangodb\ActiveQuery]] 一起使用
use yii\data\ActiveDataProvider;
use explosivebit\arangodb\Query;
$query = new Query;
$query->collection('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 迁移通过 [[explosivebit\arangodb\console\controllers\MigrateController]] 管理,它是常规 [[\yii\console\controllers\MigrateController]] 的类似物。
为了启用此命令,您应该调整控制台应用程序的配置
return [
// ...
'controllerMap' => [
'arangodb-migrate' => 'explosivebit\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
# Apply 1 migration
yii arangodb-migrate/up 1
# reverts the last applied migration
yii arangodb-migrate/down
# Rollback 1 migration
yii arangodb-migrate/down 1
一旦创建迁移,您就可以配置迁移
当您开始迁移,如下例所示时,您创建一个具有迁移名称的正常文档集合,您需要添加一个额外的参数 Type => 3
以创建 边集合
。此类查询的示例:$this-> createCollection ('services', ['Type' => 3] );
如果您想创建文档集合,则删除 'Type' => 3
或将数字 3 替换为 2。
class m170413_210957_create_services_collection extends \explosivebit\arangodb\Migration
{
public function up()
{
# When you start the migration, a collection of "services" with the edge type is created
$this->createCollection('services',['Type' => 3]);
}
public function down()
{
# When the migration rollback starts, the collection "services"
$this->dropCollection('services');
}
}
使用调试面板
将 ArangoDb 面板添加到您的 yii\debug\Module 配置中
return [
'bootstrap' => ['debug'],
'modules' => [
'debug' => 'yii\debug\Module',
'panels' => [
'arango' => [
'class' => 'explosivebit\arangodb\panels\arangodb\ArangoDbPanel',
],
],
...
],
...
];