Download the PHP package mongodb/symfony-bundle without Composer
On this page you can find all versions of the php package mongodb/symfony-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mongodb/symfony-bundle
More information about mongodb/symfony-bundle
Files in mongodb/symfony-bundle
Package symfony-bundle
Short Description Bundle to integrate mongodb/mongodb into Symfony
License Apache-2.0
Homepage https://jira.mongodb.org/browse/PHPORM
Informations about the package symfony-bundle
Symfony Bundle for MongoDB (EXPERIMENTAL)
This bundle provides integration of the MongoDB library into Symfony. It is designed as a lightweight alternative to the Doctrine MongoDB ODM, providing only an integration to interact with MongoDB without providing all features of an ODM.
Installation
Install the bundle with composer:
If you have symfony/flex
installed, the bundle is automatically enabled.
Otherwise, you need to enable it manually by adding it to the bundles.php
file:
Configuration
Configuration is done in the config/packages/mongodb.yaml
file. To get started, you need to configure at least one
client:
The id
is used to reference the client in the service container. The uri
is the connection string to connect to. For
security reasons, it is recommended to read this value from your local environment and referencing it through an
environment variable in the container:
You can also specify additional options for the client:
The uri_options
and driver_options
are passed directly to the underlying MongoDB driver.
See the documentation for available options.
If you want to configure multiple clients, you can do so by adding additional clients to the configuration:
[!NOTE] If you add multiple clients, you need to specify the
default_client
option to specify which client should be used as default!
Client Usage
For each client, a service is registered in the container with the mongodb.client.{id}
service id.
[!NOTE] The MongoDB driver only establishes a connection to MongoDB when it is actually needed, so a client can be injected into services without causing network traffic.
With autowiring enabled, you can inject the client into your services like this:
If you register multiple clients, you can autowire them by using the client name with a Client
suffix as parameter
name:
or by using the #[AutowireClient]
attribute:
Database Usage
The client service provides access to databases and collections. You can access a database by calling the
selectDatabase
method, passing the database name and potential options:
An alternative to this is using the #[AutowireDatabase]
attribute, referencing the database name:
If you don't specify a database name in the attribute, the default database name (specified in the default_database
configuration option) will be used. If you did not define a default database, the database name has to be specified in
the attribute.
If you have more than one client defined, you can also reference the client:
Collection Usage
To inject a collection, you can either call the selectCollection
method on a Client
or Database
instance.
For convenience, the #[AutowireCollection]
attribute provides a quicker alternative:
You can also omit the collection
option if the property name matches the collection name.
In the following example the collection name is myCollection
, inferred from the property name:
If you have more than one client defined, you can also reference the client:
By specifiying the default_database
option in the configuration, you can omit the database
option in the
AutowireCollection
attribute:
Specifying options
When using the AutowireDatabase
or AutowireCollection
attributes, you can specify additional options for the
resulting instances. You can pass the following options:
|| Option || Accepted type ||
| codec
| DocumentCodec
instance |
| typeMap
| array
containing type map information |
| readPreference
| MongoDB\Driver\ReadPreference
instance |
| writeConcern
| MongoDB\Driver\writeConcern
instance |
| readConcern
| MongoDB\Driver\ReadConcern
instance |
In addition to passing an instance, you can also pass a service reference by specifying a string for the given option:
All versions of symfony-bundle with dependencies
mongodb/mongodb Version ^1.17
symfony/config Version ^6.3 || ^7.0
symfony/console Version ^6.3 || ^7.0
symfony/dependency-injection Version ^6.3.5 || ^7.0
symfony/http-kernel Version ^6.3.5 || ^7.0
symfony/runtime Version ^6.3 || ^7.0