Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 91a9525

Browse filesBrowse files
NanoSectorjaviereguiluz
authored andcommitted
[Doctrine] entityvalueresolver target entities
1 parent 0d01dbc commit 91a9525
Copy full SHA for 91a9525

File tree

Expand file treeCollapse file tree

2 files changed

+44
-17
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+44
-17
lines changed

‎doctrine.rst

Copy file name to clipboardExpand all lines: doctrine.rst
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,28 @@ variable. Let's say you want the first or the last comment of a product dependin
780780
Comment $comment
781781
): Response {
782782
}
783+
784+
.. _doctrine-entity-value-resolver-resolve-target-entities:
785+
786+
Fetch via Interfaces
787+
~~~~~~~~~~~~~~~~~~~~
788+
789+
Suppose your ``Product`` object implements an interface called ``ProductInterface``.
790+
If you want to decouple your controllers from your entity implementations, you can instead reference entities via an interface.
791+
To do this, first you need to configure the :doc:`resolve_target_entities option </doctrine/resolve_target_entity>`.
792+
793+
Your controller can then reference the Product entity by its interface instead::
794+
795+
public function show(
796+
#[MapEntity]
797+
ProductInterface $product
798+
): Response {
799+
// ...
800+
}
801+
802+
.. versionadded:: 7.3
803+
804+
Support for ``resolve_target_entites`` in the ``EntityValueResolver`` was introduced in Symfony 7.3.
783805

784806
MapEntity Options
785807
~~~~~~~~~~~~~~~~~

‎doctrine/resolve_target_entity.rst

Copy file name to clipboardExpand all lines: doctrine/resolve_target_entity.rst
+22-17Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
1-
How to Define Relationships with Abstract Classes and Interfaces
2-
================================================================
1+
Referencing Entities with Abstract Classes and Interfaces
2+
=========================================================
33

4-
One of the goals of bundles is to create discrete bundles of functionality
5-
that do not have many (if any) dependencies, allowing you to use that
6-
functionality in other applications without including unnecessary items.
4+
In applications where functionality is segregated with minimal concrete dependencies
5+
between the various layers, such as monoliths which are split into multiple modules,
6+
it might be hard to prevent hard dependencies on entities between modules.
77

88
Doctrine 2.2 includes a new utility called the ``ResolveTargetEntityListener``,
99
that functions by intercepting certain calls inside Doctrine and rewriting
1010
``targetEntity`` parameters in your metadata mapping at runtime. It means that
11-
in your bundle you are able to use an interface or abstract class in your
12-
mappings and expect correct mapping to a concrete entity at runtime.
11+
you are able to use an interface or abstract class in your mappings and expect
12+
correct mapping to a concrete entity at runtime.
1313

1414
This functionality allows you to define relationships between different entities
1515
without making them hard dependencies.
1616

17+
.. tip::
18+
19+
Starting with Symfony 7.3, this functionality also works with the ``EntityValueResolver``.
20+
See :ref:`doctrine-entity-value-resolver-resolve-target-entities` for more details.
21+
1722
Background
1823
----------
1924

20-
Suppose you have an InvoiceBundle which provides invoicing functionality
21-
and a CustomerBundle that contains customer management tools. You want
22-
to keep these separated, because they can be used in other systems without
23-
each other, but for your application you want to use them together.
25+
Suppose you have an application which provides two modules; an Invoice module which
26+
provides invoicing functionality, and a Customer module that contains customer management
27+
tools. You want to keep dependencies between these modules separated, because they should
28+
not be aware of the other module's implementation details.
2429

25-
In this case, you have an ``Invoice`` entity with a relationship to a
26-
non-existent object, an ``InvoiceSubjectInterface``. The goal is to get
27-
the ``ResolveTargetEntityListener`` to replace any mention of the interface
30+
In this case, you have an ``Invoice`` entity with a relationship to the interface
31+
``InvoiceSubjectInterface``. This is not recognized as a valid entity by Doctrine.
32+
The goal is to get the ``ResolveTargetEntityListener`` to replace any mention of the interface
2833
with a real object that implements that interface.
2934

3035
Set up
@@ -89,7 +94,7 @@ An InvoiceSubjectInterface::
8994
public function getName(): string;
9095
}
9196

92-
Next, you need to configure the listener, which tells the DoctrineBundle
97+
Next, you need to configure the ``resolve_target_entities`` option, which tells the DoctrineBundle
9398
about the replacement:
9499

95100
.. configuration-block::
@@ -141,6 +146,6 @@ Final Thoughts
141146
--------------
142147

143148
With the ``ResolveTargetEntityListener``, you are able to decouple your
144-
bundles, keeping them usable by themselves, but still being able to
149+
modules, keeping them usable by themselves, but still being able to
145150
define relationships between different objects. By using this method,
146-
your bundles will end up being easier to maintain independently.
151+
your modules will end up being easier to maintain independently.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.