PHP code example of sokil / php-mongo-yii

1. Go to this page and download the library: Download sokil/php-mongo-yii library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sokil / php-mongo-yii example snippets




return array(
    'components' => 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',
    ),
);


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



return array(
    'components' => 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',
                ),
            ),
        ),
    ),
);
        



// get cursor
$cursor = Yii::app()->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)
));
javascript
{
    "hp-mongo-yii": "dev-master"
    }
}