Paul Klimov
5a046230fd
|
11 years ago | |
---|---|---|
.. | ||
file | 11 years ago | |
ActiveQuery.php | 11 years ago | |
ActiveRecord.php | 11 years ago | |
ActiveRelation.php | 11 years ago | |
Collection.php | 11 years ago | |
Connection.php | 11 years ago | |
Database.php | 11 years ago | |
Exception.php | 11 years ago | |
LICENSE.md | 11 years ago | |
Query.php | 11 years ago | |
README.md | 11 years ago | |
composer.json | 11 years ago |
README.md
MongoDb Extension for Yii 2
This extension provides the MongoDB integration for the Yii2 framework.
Installation
This extension requires MongoDB PHP Extension version 1.3.0 or higher.
The preferred way to install this extension is through composer.
Either run
php composer.phar require yiisoft/yii2-mongodb "*"
or add
"yiisoft/yii2-mongodb": "*"
to the require section of your composer.json.
Usage & Documentation
To use this extension, simply add the following code in your application configuration:
return [
//....
'components' => [
'mongodb' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://developer:password@localhost:27017/mydatabase',
],
],
];
This extension provides ActiveRecord solution similar ot the \yii\db\ActiveRecord.
To declare an ActiveRecord class you need to extend \yii\mongodb\ActiveRecord and
implement the collectionName
and 'attributes' methods:
use yii\mongodb\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 ['name', 'email', 'address', 'status'];
}
}
You can use \yii\data\ActiveDataProvider with \yii\mongodb\Query and \yii\mongodb\ActiveQuery:
use yii\data\ActiveDataProvider;
use yii\mongodb\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();
This extension supports MongoGridFS via classes under namespace "\yii\mongodb\file".