README.md 2.61 KB
Newer Older
Qiang Xue committed
1 2
MongoDb Extension for Yii 2
===========================
3

Qiang Xue committed
4
This extension provides the [MongoDB](http://www.mongodb.org/) integration for the Yii2 framework.
5 6 7 8 9


Installation
------------

Qiang Xue committed
10 11
This extension requires [MongoDB PHP Extension](http://us1.php.net/manual/en/book.mongo.php) version 1.3.0 or higher.

12 13 14
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run
Qiang Xue committed
15

16
```
17
php composer.phar require yiisoft/yii2-mongodb "*"
18 19 20
```

or add
Qiang Xue committed
21

22
```
23
"yiisoft/yii2-mongodb": "*"
24 25
```

Qiang Xue committed
26
to the require section of your composer.json.
27 28 29 30 31


Usage & Documentation
---------------------

32 33 34 35 36 37
To use this extension, simply add the following code in your application configuration:

```php
return [
	//....
	'components' => [
38 39
		'mongodb' => [
			'class' => '\yii\mongodb\Connection',
40 41 42 43 44 45 46
			'dsn' => 'mongodb://developer:password@localhost:27017/mydatabase',
		],
	],
];
```

This extension provides ActiveRecord solution similar ot the [[\yii\db\ActiveRecord]].
47
To declare an ActiveRecord class you need to extend [[\yii\mongodb\ActiveRecord]] and
48 49 50
implement the `collectionName` and 'attributes' methods:

```php
51
use yii\mongodb\ActiveRecord;
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

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()
	{
Klimov Paul committed
68
		return ['_id', 'name', 'email', 'address', 'status'];
69 70 71 72
	}
}
```

Klimov Paul committed
73 74
Note: collection primary key name ('_id') should be always explicitly setup as an attribute.

Qiang Xue committed
75
You can use [[\yii\data\ActiveDataProvider]] with [[\yii\mongodb\Query]] and [[\yii\mongodb\ActiveQuery]]:
76 77 78

```php
use yii\data\ActiveDataProvider;
79
use yii\mongodb\Query;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

$query = new Query;
$query->from('customer')->where(['status' => 2]);
$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();
```

This extension supports [MongoGridFS](http://docs.mongodb.org/manual/core/gridfs/) via
Qiang Xue committed
106
classes under namespace "\yii\mongodb\file".
Klimov Paul committed
107 108 109 110 111

This extension supports logging and profiling, however log messages does not contain
actual text of the performed queries, they contains only a “close approximation” of it
composed on the values which can be extracted from PHP Mongo extension classes.
If you need to see actual query text, you should use specific tools for that.