yii2-arangodb/README.md

218 lines
5.5 KiB
Markdown
Raw Permalink Normal View History

2017-12-22 17:52:34 +07:00
[Описание на Русском](https://github.com/explosivebit/yii2-arangodb/blob/master/README.RU.md)
2014-07-29 21:11:33 +08:00
ArangoDb Extension for Yii 2
===========================
2014-06-16 12:29:28 +08:00
2014-07-29 21:11:33 +08:00
This extension provides the [ArangoDB](http://www.arangodb.org/) integration for the Yii2 framework.
Installation
------------
This extension requires [ArangoDB PHP Extension](https://github.com/triAGENS/ArangoDB-PHP)
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
Either run
```
2017-04-14 04:48:32 +07:00
php composer.phar require --prefer-dist explosivebit/yii2-arangodb "*"
2014-07-29 21:11:33 +08:00
```
or add
```
2017-04-14 04:48:32 +07:00
"explosivebit/yii2-arangodb": "*"
2014-07-29 21:11:33 +08:00
```
to the require section of your composer.json.
General Usage
-------------
To use this extension, simply add the following code in your application configuration:
```php
return [
//....
'components' => [
'arangodb' => [
2017-04-14 04:48:32 +07:00
'class' => '\explosivebit\arangodb\Connection',
2014-07-29 21:11:33 +08:00
'connectionOptions' => [
2017-10-09 14:16:41 +07:00
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 => '',
2014-07-29 21:11:33 +08:00
],
],
],
];
```
Using the connection instance you may access databases, collections and documents.
2017-04-14 04:48:32 +07:00
To perform "find" queries, you should use [[\explosivebit\arangodb\Query]]:
2014-07-29 21:11:33 +08:00
```php
2017-04-14 04:48:32 +07:00
use explosivebit\arangodb\Query;
2014-07-29 21:11:33 +08:00
$query = new Query;
// compose the query
$query->select(['name', 'status'])
->collection('customer')
2014-07-29 21:11:33 +08:00
->limit(10);
// execute the query
$rows = $query->all();
```
Using the ArangoDB ActiveRecord
------------------------------
This extension provides ActiveRecord solution similar ot the [[\yii\db\ActiveRecord]].
2017-04-14 04:48:32 +07:00
To declare an ActiveRecord class you need to extend [[\explosivebit\arangodb\ActiveRecord]] and
2014-07-29 21:11:33 +08:00
implement the `collectionName` and 'attributes' methods:
```php
2017-04-14 04:48:32 +07:00
use explosivebit\arangodb\ActiveRecord;
2014-07-29 21:11:33 +08:00
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'];
}
}
```
Note: collection primary key name ('_key') should be always explicitly setup as an attribute.
2017-04-14 04:48:32 +07:00
You can use [[\yii\data\ActiveDataProvider]] with [[\explosivebit\arangodb\Query]] and [[\explosivebit\arangodb\ActiveQuery]]:
2014-07-29 21:11:33 +08:00
```php
use yii\data\ActiveDataProvider;
2017-04-14 04:48:32 +07:00
use explosivebit\arangodb\Query;
2014-07-29 21:11:33 +08:00
$query = new Query;
$query->collection('customer')->where(['status' => 2]);
2014-07-29 21:11:33 +08:00
$provider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 10,
]
]);
$models = $provider->getModels();
```
```php
use yii\data\ActiveDataProvider;
use app\models\Customer;
$provider = new ActiveDataProvider([
'query' => Customer::find(),
'pagination' => [
'pageSize' => 10,
]
]);
$models = $provider->getModels();
```
Using Migrations
----------------
2017-04-14 04:48:32 +07:00
ArangoDB migrations are managed via [[explosivebit\arangodb\console\controllers\MigrateController]], which is an analog of regular
2014-07-29 21:11:33 +08:00
[[\yii\console\controllers\MigrateController]].
In order to enable this command you should adjust the configuration of your console application:
```php
return [
// ...
'controllerMap' => [
2017-04-14 04:48:32 +07:00
'arangodb-migrate' => 'explosivebit\arangodb\console\controllers\MigrateController'
2014-07-29 21:11:33 +08:00
],
];
```
Below are some common usages of this command:
```
# 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
2014-07-29 21:11:33 +08:00
# reverts the last applied migration
yii arangodb-migrate/down
# Rollback 1 migration
yii arangodb-migrate/down 1
```
Once migration created, you can configure the migration:
2017-06-21 09:19:14 +07:00
When you start the migration as in the example below, you create a normal document collection with the migration name, you need to add an additional parameter `Type => 3` to create the `edge collection`. Example of such a query: `$this-> createCollection ('services', ['Type' => 3] );` If you want to create a document collection, then delete the `'Type' => 3` or replace the number 3 with 2.
```php
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');
}
}
2014-08-05 13:40:17 +08:00
```
Using Debug Panel
-----------------
Add ArangoDb panel to your yii\debug\Module configuration
```php
return [
'bootstrap' => ['debug'],
'modules' => [
'debug' => 'yii\debug\Module',
'panels' => [
'arango' => [
2017-04-14 04:48:32 +07:00
'class' => 'explosivebit\arangodb\panels\arangodb\ArangoDbPanel',
2014-08-05 13:40:17 +08:00
],
],
...
],
...
];
```
2014-11-23 19:18:02 +07:00
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/DevGroup-ru/yii2-arangodb/trend.png)](https://bitdeli.com/free "Bitdeli Badge")