PHP code example of exs / silex-simplemongo-provider

1. Go to this page and download the library: Download exs/silex-simplemongo-provider 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/ */

    

exs / silex-simplemongo-provider example snippets


//...
$app['mongo.connections'] = array(
    'connection' => 'mongodb://localhost:27017',
    'dbname' => 'DB_NAME'
);
//...

public function __construct(\EXS\SimpleMongoProvider\Services\SimpleMongoService $mongo_service)
{
    $this->mongo_service = $mongo_service;
}
.
.
.

// Insert
$this->mongo_service->persist(YOUR_CLASS_OR_ARRAY);   
$result = $this->mongo_service->flush(COLLECTION_NAME); // the result will store the number of inserted entries or error message
if(!is_int($result) || $result == 0) {
    throwException($result);
}

// Update
$filter = ['product' => 6];
$this->mongo_service->update($filter, YOUR_CLASS_OR_ARRAY);   
$result = $this->mongo_service->flush(COLLECTION_NAME); 
 
// Get data with query
$filter = ['product' => 6];
$option = ['projection' => ['_id' => 0]];

$result = $$this->mongo_service->exeQuery($filter, $option, COLLECTION_NAME);
// $result will contain results in an array
 shell
php composer.phar install
 php
//app.php
//...
$app->register(new \EXS\SimpleMongoProvider\Providers\Services\SimpleMongoProvider());
 php
    public function register(Container $container)
    {
        $container[YOUR_SERVICE_NAME] = ( function ($container) {
            return new YOUR_SERVICE(
                $container['exs.serv.mongo']
                );                
        });
    }