Configuration with MondocConfig
All configuration settings are managed by the MondocConfig class. This class is a singleton that holds the MongoDB client instance and provides the mapping between your models and services.
About MondocConfig
The MondocConfig class is a singleton that holds the MongoDB client instance and provides the mapping between your models and services. This class is responsible for storing the connection(s) to your MongoDB instance(s), and for managing the mapping between your models and services.
Note: After you've set up your MondocConfig object, you won't need to reference it again. The singleton instance is used internally by Mondoc when you interact with your models and services.
Exposed methods
Below is a list of the methods available in the MondocConfig singleton.
-
getInstance(): \District5\Mondoc\MondocConfig
Returns the singleton instance of the MondocConfig class. This is the only entry point for everything inside of
MondocConfig
. -
addDatabase(\MongoDB\Database $database, string $connectionId = 'default'): \District5\Mondoc\MondocConfig
Adds a database to the configuration. This method is used to add a new database connection to the MondocConfig singleton.
The
$connectionId
parameter is optional and defaults todefault
. -
getCollection(string $name, string $connectionId = 'default'): \District5\Mondoc\Db\MondocCollection
Returns a collection from the database. This method is used to retrieve a collection from the database connection.
-
getDatabase(string $connectionId = 'default'): \MongoDB\Database
Returns the database connection. This method is used to retrieve the database connection from the MondocConfig singleton.
-
setServiceMap(array $serviceMap): \District5\Mondoc\MondocConfig
Sets the mapping for the models and services. This method overrides any existing mappings.
-
getServiceMap(): array
Returns the mapping for the models and services.
-
addServiceMap(string $modelFQCN, string $serviceFQCN): \District5\Mondoc\MondocConfig
Add a new to the model to service map. This method adds the new model/service to any existing map present in MondocConfig.
-
getServiceForModel(string $modelFQCN): string
Returns the service class for the given model class.
-
getModelForService(string $serviceFQCN): string
Returns the model class for the given service class.
-
reconstruct(): \District5\Mondoc\Db\Model\MondocAbstractModel
Clears the current MondocConfig instance and reconstructs it.
MondocConfig example
Below is a full example of how you can use the MondocConfig singleton object.
<?php use MongoDB\Client; use District5\Mondoc\MondocConfig; $appDataConnection = new Client('mongodb+srv://user:pass@host:27017'); $appDataDatabase = $appDataConnection->selectDatabase('db_name'); $mondoc = MondocConfig::getInstance(); $mondoc->addDatabase( $appDataDatabase, // This is the MongoDB database object 'default' // This is the connection identifier ); $authDataConnection = new Client('mongodb+srv://user:pass@auth-host:27017'); $authDataDatabase = $authDataConnection->selectDatabase('auth_db_name'); $mondoc->addDatabase( $authDataDatabase, // This is the MongoDB database object 'auth' // This is the connection identifier ); $mondoc->setServiceMap([ MyFirstModel::class => MyFirstService::class, MySecondModel::class => MySecondService::class, ]); // Add a new model to service mapping $mondoc->addServiceMapping( MyThirdModel::class, MyThirdService::class ); $map = $mondoc->getServiceMap(); $serviceClassNameForMyFirstModel = $mondoc->getServiceForModel(MyFirstModel::class); $modelClassNameForMyFirstService = $mondoc->getModelForService(MyFirstService::class);