Skip to content

Commit fa7cb77

Browse files
committed
feature #4700 add informations how to create a custom doctrine mapping (timglabisch)
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #4700). Discussion ---------- add informations how to create a custom doctrine mapping i tryed to explain a bit how doctrine tries to find the correct mapping, the driver and how you can configure your own mapping. Commits ------- 8a98fc1 Update doctrine.rst bec3c98 Doctrine Custom Mapping
2 parents 0494129 + 8a98fc1 commit fa7cb77

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

reference/configuration/doctrine.rst

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,4 +521,151 @@ can be placed directly under ``doctrine.orm`` config level.
521521
This shortened version is commonly used in other documentation sections.
522522
Keep in mind that you can't use both syntaxes at the same time.
523523

524+
Custom Mapping Entities in a Bundle
525+
-----------------------------------
526+
527+
Doctrine's ``auto_mapping`` feature loads annotation configuration from the
528+
``Entity/`` directory of each bundle *and* looks for other formats (e.g. YAML, XML)
529+
in the ``Resources/config/doctrine`` directory.
530+
531+
If you store metadata somewhere else in your bundle, you can define your own mappings,
532+
where you tell Doctrine exactly *where* to look, along with some other configurations.
533+
534+
If you're using the ``auto_mapping`` configuration, you just need to overwrite the
535+
configurations you want. In this case it's important that the key of the mapping
536+
configurations corresponds to the name of the bundle.
537+
538+
For example, suppose you decide to store your ``XML`` configuration for ``AppBundle`` entities
539+
in the ``@AppBundle/SomeResources/config/doctrine`` directory instead:
540+
541+
.. configuration-block::
542+
543+
.. code-block:: yaml
544+
545+
doctrine:
546+
# ...
547+
orm:
548+
# ...
549+
auto_mapping: true
550+
mappings:
551+
# ...
552+
AppBundle:
553+
type: xml
554+
dir: SomeResources/config/doctrine
555+
556+
.. code-block:: xml
557+
558+
<?xml version="1.0" charset="UTF-8" ?>
559+
<container xmlns="http://symfony.com/schema/dic/services"
560+
xmlns:doctrine="http://symfony.com/schema/dic/doctrine">
561+
562+
<doctrine:config>
563+
<doctrine:orm auto-mapping="true">
564+
<mapping name="AppBundle" dir="SomeResources/config/doctrine" type="xml" />
565+
</doctrine:orm>
566+
</doctrine:config>
567+
</container>
568+
569+
.. code-block:: php
570+
571+
$container->loadFromExtension('doctrine', array(
572+
'orm' => array(
573+
'auto_mapping' => true,
574+
'mappings' => array(
575+
'AppBundle' => array('dir' => 'SomeResources/config/doctrine', 'type' => 'xml'),
576+
),
577+
),
578+
));
579+
580+
Mapping Entities Outside of a Bundle
581+
------------------------------------
582+
583+
You can also create new mappings, for example outside of the Symfony folder.
584+
585+
For example, the following looks for entity classes in the ``App\Entity`` namespace in the
586+
``src/Entity`` directory and gives them an ``App`` alias (so you can say things like ``App:Post``):
587+
588+
.. configuration-block::
589+
590+
.. code-block:: yaml
591+
592+
doctrine:
593+
# ...
594+
orm:
595+
# ...
596+
mappings:
597+
# ...
598+
SomeEntityNamespace:
599+
type: annotation
600+
dir: %kernel.root_dir%/../src/Entity
601+
is_bundle: false
602+
prefix: App\Entity
603+
alias: App
604+
605+
.. code-block:: xml
606+
607+
<?xml version="1.0" charset="UTF-8" ?>
608+
<container xmlns="http://symfony.com/schema/dic/services"
609+
xmlns:doctrine="http://symfony.com/schema/dic/doctrine">
610+
611+
<doctrine:config>
612+
<doctrine:orm>
613+
<mapping name="SomeEntityNamespace"
614+
type="annotation"
615+
dir="%kernel.root_dir%/../src/Entity"
616+
is-bundle="false"
617+
prefix="App\Entity"
618+
alias="App"
619+
/>
620+
</doctrine:orm>
621+
</doctrine:config>
622+
</container>
623+
624+
.. code-block:: php
625+
626+
$container->loadFromExtension('doctrine', array(
627+
'orm' => array(
628+
'auto_mapping' => true,
629+
'mappings' => array(
630+
'SomeEntityNamespace' => array(
631+
'type' => 'annotation',
632+
'dir' => '%kernel.root_dir%/../src/Entity',
633+
'is_bundle' => false,
634+
'prefix' => 'App\Entity',
635+
'alias' => 'App',
636+
),
637+
),
638+
),
639+
));
640+
641+
Detecting a Mapping Configuration Format
642+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
643+
644+
If the ``type`` on the bundle configuration isn't set,
645+
the DoctrineBundle will try to detect the correct mapping configuration format for
646+
the bundle.
647+
648+
DoctrineBundle will look for files matching ``*.orm.[FORMAT]`` (e.g. ``Post.orm.yml``)
649+
in the configured ``dir`` of your mapping (if you're mapping a bundle, then ``dir`` is
650+
relative to the bundle's directory).
651+
652+
The bundle looks for (in this order) XML, YAML and PHP files.
653+
Using the ``auto_mapping`` feature, every bundle can have only one configuration format.
654+
The bundle will stop as soon as it locates one.
655+
656+
If it wasn't possible to determine a configuration format for a bundle,
657+
the DoctrineBundle will check if there is an ``Entity`` folder in the bundle's root directory.
658+
If the folder exist, Doctrine will fall back to using an annotation driver.
659+
660+
Default Value of dir
661+
~~~~~~~~~~~~~~~~~~~~
662+
663+
If ``dir`` is not specified, then its default value depends on which configuration driver is being used.
664+
For drivers that rely on the PHP files (annotation, staticphp) it will
665+
be ``[Bundle]/Entity``. For drivers that are using configuration
666+
files (XML, YAML, ...) it will be ``[Bundle]/Resources/config/doctrine``.
667+
668+
If the ``dir`` configuration is set and the ``is_bundle`` configuration is ``true``,
669+
the DoctrineBundle will prefix the ``dir`` configuration with the path of the bundle.
670+
524671
.. _`DQL User Defined Functions`: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/dql-user-defined-functions.html

0 commit comments

Comments
 (0)