Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sokil/php-mongo-yii

Yii 1 PHPMongo Adapter. Part of @PHPMongoKit
https://github.com/sokil/php-mongo-yii

mongo mongodb odm php phpmongo phpmongokit yii yii-adapter

Last synced: 17 days ago
JSON representation

Yii 1 PHPMongo Adapter. Part of @PHPMongoKit

Awesome Lists containing this project

README

        

php-mongo-yii
=============

Yii 1 Adapter for [PHPMongo ORM](https://github.com/sokil/php-mongo)

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

You can install library through Composer:
```javascript
{
"require": {
"sokil/php-mongo-yii": "dev-master"
}
}
```

Configuration of Client
-----------------------

```php
array(
// configure mongo service
'mongo' => array(
'class' => '\Sokil\Mongo\Yii\ClientAdapter',
'dsn' => 'mongodb://127.0.0.1',
'options' => array(
'connect' => true,
'readPreference' => \MongoClient::RP_SECONDARY_PREFERRED,
),
'defaultDatabase' => 'database_name',
'map' => array(
'database_name' => array(
'collectionName1' => '\Collection\Class1',
'collectionName2' => '\Collection\Class2',
)
),
'logger' => 'somePsrCompartibleLogService',
),
),

// define log
'somePsrCompartibleLogService' => array(
'class' => '\SomePSRLogger',
),
);
```

For PSR compartible logger you can use adapter to Yii's log https://gist.github.com/sokil/56654a5abdfbcce411ea or [Monolog](https://github.com/Seldaek/monolog)

Using mongo in code:

```php
mongo->getClient();
// get database
$database = \Yii::app()->mongo->getDatabase('database_name');
// get collection of default database
$collection = \Yii::app()->mongo->getCollection('collectionName1');
```

Routing Yii logs to mongo
-------------------------

Configure logger to use mongo:

```php
array(
// define connection service
'mongo' => array(
'class' => '\Sokil\Mongo\Yii\ClientAdapter',
// ...
),
// configure log service
'log' => array(
'class' => 'CLogRouter',
'routes' => array(
array(
'class' => '\Sokil\Mongo\Yii\LogRoute',
'levels' => 'error, warning',
// define collection name where to store log records
'collectionName' => 'log',
// define mongo connection service used to record logs
'serviceName' => 'mongo',
),
),
),
),
);

```

Data Provider
-------------

```php
mongo->getCollection('collName')->find()->where('type', 10);

// get data provider
$dataProvider = new \Sokil\Mongo\Yii\DataProvider($cursor, array(
'attributes' => array('name', 'type'),
'pagination' => array('pageSize' => 30)
));
```