Skip to content

Commit 8874460

Browse files
committed
Doctrine Custom Mapping
1 parent 907ee0d commit 8874460

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
@@ -499,4 +499,151 @@ can be placed directly under ``doctrine.orm`` config level.
499499
This shortened version is commonly used in other documentation sections.
500500
Keep in mind that you can't use both syntaxes at the same time.
501501

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