facile-it / mongodb-bundle
Bundle service integration of official [mongodb/mongo-php-library](https://github.com/mongodb/mongo-php-library) driver library
Installs: 44 944
Dependents: 1
Suggesters: 0
Security: 0
Stars: 30
Watchers: 35
Forks: 15
Open Issues: 3
Type:symfony-bundle
Requires
- php: ^7.2
- ext-mongodb: ^1.1.5
- mongodb/mongodb: ^1.0
- symfony/framework-bundle: ^3.4 || ^4.3 || ^5.0
Requires (Dev)
- facile-it/facile-coding-standard: ^0.3.1
- matthiasnoback/symfony-dependency-injection-test: ^4
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^8.5.2
- symfony/console: ^3.4 || ^4.3 || ^5.0
- symfony/phpunit-bridge: ^5.0
- symfony/web-profiler-bundle: ^3.4 || ^4.3 || ^5.0
Suggests
- symfony/console: Enables console commands
- symfony/web-profiler-bundle: Enables profiling of mongodb queries
- dev-master / 1.x-dev
- 1.0.0
- 1.0.0-BETA
- 1.0.0-ALPHA2
- 1.0.0-ALPHA1
- 0.7.4
- 0.7.3
- 0.7.2
- 0.7.1
- 0.7.0
- 0.6.7
- 0.6.6
- 0.6.5
- 0.6.4
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.6.0-BETA3
- 0.6.0-BETA2
- 0.6.0-BETA1
- 0.5.0
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1
- dev-psalm_try
- dev-change_config_behaviour
- dev-fix_authsource
- dev-fix_#96_add_version_to_test
- dev-fix-dispatcher-proxy-deprecation
- dev-improve-testing
- dev-buildtry
- dev-0.7.x-dev
- dev-1812_38_webdev_env
This package is auto-updated.
Last update: 2021-01-14 10:05:49 UTC
README
Bundle service integration of official mongodb/mongo-php-library driver library, (mongodb/mongodb on packagist)
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() { return [ // ... new Facile\MongoDbBundle\FacileMongoDbBundle(), ]; }
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 uri: 'mongodb://host1:3062,host2' # default null (will use hosts to build connection URI) hosts: # required if uri is not set - will compose your connection URI (mongodb://host1:3062,host2:27017) - { host: host1, port: 3062 } # this - { host: host2 } username: '' password: '' authSource: '' # the database name associated with the user’s credentials, defaults to connection # 'database_name' if not specified 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: ~ # Service reference to manage driver options context - see example below driverOptions: "app.mongodb.context_service" # default null
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 add a php class in your bundle's /DataFixtures/Mongo
directory.
For Example src/AppBundle/DataFixtures/Mongo/LoadTaskDataFixture.php
.
Note: This class must implement at least the MongoFixtureInterface
, if you need container support you can
extend AbstractContainerAwareFixture
to use the getContainer
method.
Ordering
Since 0.6.6 it's possibile to specify the order by
which the fixtures are loaded.
Make sure it implements OrderedFixtureInterface
.
Fixtures with lower values of getOrder
will be loaded first.
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.
Driver options
Example service to manage driver options. This class must implements Facile\MongoDbBundle\Services\DriverOptions\DriverOptionsInterface.
# config/packages/facile_it_mongodb.yaml
mongo_db_bundle:
...
driverOptions: "app.mongodb.context_service"
# config/services.yaml
app.mongodb.context_service:
class: 'App\Services\DriverOptionsService'
arguments:
- context: { cafile: 'example/route/file.crt' }
Contributing
Feel free to contribute by opening a pull request. Bug fixes or feature suggestions are always welcome.
Development environment
If you like docker there are some scripts to setup an insulated development environment.
Please be sure to have docker
and docker-compose
installed on your system.
To setup the project:
make setup
To start the container and log into the php:
make start
To execute the test suite:
make test
Note: All the above are meant to be used outside the containers.
Port binding setup
During the setup a docker-compose.override.yml
file is created from the docker-compose.override.yml.dist
template.
Change the port binding configuration inside it to fit you needs.