Skip to content

Bundle service integration of official [mongodb/mongo-php-library](https://github.com/mongodb/mongo-php-library) driver library

License

Notifications You must be signed in to change notification settings

guidopili/mongodb-bundle

 
 

Repository files navigation

facile-it/mongodb-bundle

Bundle service integration of official mongodb/mongo-php-library driver library, (mongodb/mongodb on packagist)

PHP Version Latest Stable Version Latest Unstable Version License

Build Status Scrutinizer Code Quality Code Coverage Dependency Status

Installation

First of all, you need to require this library through composer:

composer require facile-it/mongodb-bundle

Then, enable the bundle on the AppKernel class:

// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Facile\MongoDbBundle\FacileMongoDbBundle(),
    );

    // ...

    return $bundles
}

Configuration

Here is the configuration reference:

mongo_db_bundle:
    
    data_collection: true # set to false to disable data collection
    
    # clients section, here you can define connection to different servers or with different credentials
    clients:
    
      foo_client_name:    #choose your client name
        hosts:            # Required - will compose your mongo connection url (mongodb://host1:3062,host2:27017)
            - { host: host1, port: 3062 } # this 
            - { host: host2 }
        username:         ''
        password:         ''
        replicaSet:       '' # default null (no replica) (experimental)
        ssl:              false
        connectTimeoutMS: 3000 # default null (no timeout)
        readPreference:   primaryPreferred # see https://docs.mongodb.com/manual/reference/read-preference/#primary for info
        
      other_client:   ~ # same as upper configuration
      
    # connections section, theese represents your Database object reference
    connections:
    
      foo_db:
        client_name:    foo_client_name # Required - your client name in clients section
        database_name:  'foo_db' # Required
        
      other_db:
        client_name:    ~
        database_name:  ~
        
      foo_db_2:
        client_name:    ~
        database_name:  ~

Authentication

Actually only target database autentication is supported, this means that user registered on admin database can't be authenticated correctly.

Services

You can directly access to the MongoDB\Database with those services:

$this->get('mongo.connection'); // Default connection (first declared)
$this->get('mongo.connection.{connectionName}'); // [test_db, other_db, test_db_2] for example

To manipulate the database, please read the official documentation

Fixtures

This bundle supports doctrine style fixtures, to create one you will need to create a php class in your bundle '/DataFixtures/Mongo' directory for Example 'src/AppBundle/DataFixtures/Mongo/LoadTaskDataFixture.php'

NB: This class must implement at least the MongoFixtureInterface, if you need container support you can extend AbstractContainerAwareFixture to use the getContainer method.

Commands

Some commands are provided:

  • mongodb:database:drop: To drop a database.
  • mongodb:collection:drop: To drop a collection.
  • mongodb:fixtures:load: To load the fixtures.

Query Profiling

On dev environment all queries executed by the library MongoDB\Collection class are profiled and showed inside the symfony web profiler.

Profiler Toolbar

Profiler Panel

About

Bundle service integration of official [mongodb/mongo-php-library](https://github.com/mongodb/mongo-php-library) driver library

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 87.2%
  • HTML 12.8%