You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
2.2 KiB
105 lines
2.2 KiB
11 years ago
|
MongoDb Extension for Yii 2
|
||
|
===========================
|
||
11 years ago
|
|
||
11 years ago
|
This extension provides the [MongoDB](http://www.mongodb.org/) integration for the Yii2 framework.
|
||
11 years ago
|
|
||
|
|
||
|
Installation
|
||
|
------------
|
||
|
|
||
11 years ago
|
This extension requires [MongoDB PHP Extension](http://us1.php.net/manual/en/book.mongo.php) version 1.3.0 or higher.
|
||
|
|
||
11 years ago
|
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
|
||
|
|
||
|
Either run
|
||
11 years ago
|
|
||
11 years ago
|
```
|
||
11 years ago
|
php composer.phar require yiisoft/yii2-mongodb "*"
|
||
11 years ago
|
```
|
||
|
|
||
|
or add
|
||
11 years ago
|
|
||
11 years ago
|
```
|
||
11 years ago
|
"yiisoft/yii2-mongodb": "*"
|
||
11 years ago
|
```
|
||
|
|
||
11 years ago
|
to the require section of your composer.json.
|
||
11 years ago
|
|
||
|
|
||
|
Usage & Documentation
|
||
|
---------------------
|
||
|
|
||
11 years ago
|
To use this extension, simply add the following code in your application configuration:
|
||
|
|
||
|
```php
|
||
|
return [
|
||
|
//....
|
||
|
'components' => [
|
||
11 years ago
|
'mongodb' => [
|
||
|
'class' => '\yii\mongodb\Connection',
|
||
11 years ago
|
'dsn' => 'mongodb://developer:password@localhost:27017/mydatabase',
|
||
|
],
|
||
|
],
|
||
|
];
|
||
|
```
|
||
|
|
||
|
This extension provides ActiveRecord solution similar ot the [[\yii\db\ActiveRecord]].
|
||
11 years ago
|
To declare an ActiveRecord class you need to extend [[\yii\mongodb\ActiveRecord]] and
|
||
11 years ago
|
implement the `collectionName` and 'attributes' methods:
|
||
|
|
||
|
```php
|
||
11 years ago
|
use yii\mongodb\ActiveRecord;
|
||
11 years ago
|
|
||
|
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 ['name', 'email', 'address', 'status'];
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
11 years ago
|
You can use [[\yii\data\ActiveDataProvider]] with [[\yii\mongodb\Query]] and [[\yii\mongodb\ActiveQuery]]:
|
||
11 years ago
|
|
||
|
```php
|
||
|
use yii\data\ActiveDataProvider;
|
||
11 years ago
|
use yii\mongodb\Query;
|
||
11 years ago
|
|
||
|
$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
|
||
11 years ago
|
classes under namespace "\yii\mongodb\file".
|