Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document service repositories #595

Merged
merged 1 commit into from
Nov 6, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions Resources/doc/first_steps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,68 @@ You can use this new method just like the default finder methods of the reposito
When using a custom repository class, you still have access to the default
finder methods such as ``find()`` and ``findAll()``.

Service Repositories
~~~~~~~~~~~~~~~~~~~~

In the previous section, you learnt how to create custom repository classes and how
to get them using ``DocumentManager``. Another way of obtaining a repository instance
is to use the repository as a service and inject it as a dependency into other services.

.. code-block:: php

// src/Acme/StoreBundle/Repository/ProductRepository.php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to document the repositoryClass argument to the Document mapping again to be on the safe side?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, previous chapter is all about that :) On the other hand we could add a comment in the code reminding user to set repositoryClass

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A short comment sounds sensible and should be sufficient if the previous section covers this in more detail 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

namespace Acme\StoreBundle\Repository;
alcaeus marked this conversation as resolved.
Show resolved Hide resolved

use Acme\StoreBundle\Document\Product;
use Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepository;
use Doctrine\Common\Persistence\ManagerRegistry;

/**
* Remember to map this repository in the corresponding document's repositoryClass.
* For more information on this see the previous chapter.
*/
class ProductRepository extends ServiceDocumentRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Product::class);
}
}

The ``ServiceDocumentRepository`` class your custom repository is extending allows you to
leverage Symfony's `autowiring`_ and `autoconfiguration`_. To register all your repositories
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"all of your"

as services you can use the following service configuration:

.. configuration-block::

.. code-block:: yaml

services:
_defaults:
autowire: true
autoconfigure: true

Acme\StoreBundle\Repository\:
resource: '%kernel.root_dir%/../src/Acme/StoreBundle/Repository/*'

.. code-block:: xml

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<defaults autowire="true" autoconfigure="true"/>

<prototype namespace="Acme\StoreBundle\Repository\" resource="%kernel.root_dir%/../src/Acme/StoreBundle/Repository/*"/>
</services>
</container>

.. _`Basic Mapping Documentation`: http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/basic-mapping.html
.. _`Conditional Operators`: http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/query-builder-api.html#conditional-operators
.. _`DoctrineFixturesBundle`: http://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html
.. _`Query Builder`: http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/query-builder-api.html
.. _`autowiring`: https://symfony.com/doc/current/service_container/autowiring.html
.. _`autoconfiguration`: https://symfony.com/doc/current/service_container.html#the-autoconfigure-option