Skip to content

Commit f4d043d

Browse files
committed
Merge pull request #350 from doctrine/mongo-as-a-service
Add section on reusing connection
2 parents 75bf8a2 + 488fd79 commit f4d043d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Resources/doc/index.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,44 @@ be fetched from the Symfony2 service container. A similar example like the tutor
769769
// find everything in the collection
770770
$cursor = $collection->find();
771771
772+
Reusing Mongo connection
773+
~~~~~~~~~~~~~~~~~~~~~~~~
774+
775+
If you find yourself in need of ``\Mongo`` class instance i.e. to `store sessions`_
776+
in your Mongo database you may fetch it by defining following services:
777+
778+
.. configuration-block::
779+
780+
.. code-block:: yaml
781+
782+
services:
783+
mongo.connection:
784+
class: Doctrine\MongoDB\Connection
785+
factory: ["@doctrine.odm.mongodb.document_manager", getConnection]
786+
calls:
787+
- [initialize, []]
788+
mongo:
789+
class: Mongo
790+
factory: ["@mongo.connection", getMongo]
791+
792+
.. code-block:: xml
793+
794+
<?xml version="1.0" encoding="UTF-8" ?>
795+
<container xmlns="http://symfony.com/schema/dic/services"
796+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
797+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
798+
799+
<services>
800+
<service id="mongo.connection" class="Doctrine\MongoDB\Connection">
801+
<factory service="doctrine.odm.mongodb.document_manager" method="getConnection" />
802+
<call method="initialize" />
803+
</service>
804+
<service id="mongo" class="Doctrine\MongoDB\Connection">
805+
<factory service="mongo.connection" method="getMongo" />
806+
</service>
807+
</services>
808+
</container>
809+
772810
Summary
773811
-------
774812

@@ -807,3 +845,4 @@ Learn more from the Cookbook
807845
.. _`PHP Documentation`: http://www.php.net/manual/en/mongo.tutorial.php
808846
.. _`the cookbook`: http://symfony.com/doc/current/cookbook/security/entity_provider.html
809847
.. _`UniqueEntity`: http://symfony.com/doc/current/reference/constraints/UniqueEntity.html
848+
.. _`store sessions`: http://symfony.com/doc/current/cookbook/doctrine/mongodb_session_storage.html

0 commit comments

Comments
 (0)