explosivebit / yii2-arangodb
Yii2 arangodb组件
Requires
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-20 20:26:26 UTC
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 => '', ], ], ], ];
使用连接实例,您可以访问数据库、集合和文档。
要执行"查找"查询,应使用[[\explosivebit\arangodb\Query]]
use explosivebit\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类,您需要扩展[[\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->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迁移通过[[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', ], ], ... ], ... ];