This file is indexed.

/usr/share/doc/php-doctrine-orm/html/reference/xml-mapping.html is in doctrine-orm-doc 2.4.6-1+deb8u1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>20. XML Mapping &mdash; Doctrine 2 ORM 2 documentation</title>
    <link rel="stylesheet" href="../_static/bootstrap/css/bootstrap.min.css" type="text/css" />
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../_static/layout.css" type="text/css" />
    <link rel="stylesheet" href="../_static/configurationblock.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>

    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/configurationblock.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/configurationblock.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/configurationblock.js"></script>
    <script src="../_static/bootstrap/js/bootstrap.min.js"></script>

    <script type="text/javascript">
    <!--
        $(document).ready(function() {
            $("#versions").change(function() {
                var docsUrl = $(this).val();
                window.location.href = docsUrl;
            });
        });
    -->
    </script>
    <link rel="shortcut icon" href="../_static/doctrine.ico"/>
    <link rel="top" title="Doctrine 2 ORM 2 documentation" href="../index.html" />
    <link rel="next" title="21. YAML Mapping" href="yaml-mapping.html" />
    <link rel="prev" title="19. Partial Objects" href="partial-objects.html" /> 
  </head>
  <body>
    <div id="wrapper">
      <div id="header">
        <h1 id="h1title"></h1>
        <div id="logo">
          <a href="http://www.doctrine-project.org/">Doctrine - PHP Database Libraries</a>
        </div>
      </div>
      <div id="nav" class="cls">
        <div class="tl cls">
          <ul>
            <li><a target="_top" href="/">home</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/about">about</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/projects.html">projects</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/projects/orm">orm</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/projects/dbal">dbal</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/blog">blog</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/jira">development</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/contribute">contribute</a></li>
            <li><a class="" target="_top" href="http://www.doctrine-project.org/community">community</a></li>
          </ul>
        </div>
      </div>
      <div id="content" class="cls">
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="yaml-mapping.html" title="21. YAML Mapping"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="partial-objects.html" title="19. Partial Objects"
             accesskey="P">previous</a> |</li>
        <li><a href="/">Doctrine Homepage</a> &raquo;</li>
        <li><a href="../index.html">Doctrine 2 ORM 2 documentation</a> &raquo;</li> 
      </ul>
    </div>  

        <div class="document">
            <div class="documentwrapper">
                <div class="bodywrapper">

              <div class="body" >
                
  <div class="section" id="xml-mapping">
<h1>20. XML Mapping<a class="headerlink" href="#xml-mapping" title="Permalink to this headline"></a></h1>
<p>The XML mapping driver enables you to provide the ORM metadata in
form of XML documents.</p>
<p>The XML driver is backed by an XML Schema document that describes
the structure of a mapping document. The most recent version of the
XML Schema document is available online at
<a class="reference external" href="http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd</a>.
In order to point to the latest version of the document of a
particular stable release branch, just append the release number,
i.e.: doctrine-mapping-2.0.xsd The most convenient way to work with
XML mapping files is to use an IDE/editor that can provide
code-completion based on such an XML Schema document. The following
is an outline of a XML mapping document with the proper xmlns/xsi
setup for the latest code in trunk.</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;doctrine-mapping</span> <span class="na">xmlns=</span><span class="s">&quot;http://doctrine-project.org/schemas/orm/doctrine-mapping&quot;</span>
      <span class="na">xmlns:xsi=</span><span class="s">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
      <span class="na">xsi:schemaLocation=</span><span class="s">&quot;http://doctrine-project.org/schemas/orm/doctrine-mapping</span>
<span class="s">                   https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd&quot;</span><span class="nt">&gt;</span>

    ...

<span class="nt">&lt;/doctrine-mapping&gt;</span>
</pre></div>
</div>
<p>The XML mapping document of a class is loaded on-demand the first
time it is requested and subsequently stored in the metadata cache.
In order to work, this requires certain conventions:</p>
<ul class="simple">
<li>Each entity/mapped superclass must get its own dedicated XML
mapping document.</li>
<li>The name of the mapping document must consist of the fully
qualified name of the class, where namespace separators are
replaced by dots (.). For example an Entity with the fully
qualified class-name &#8220;MyProject&#8221; would require a mapping file
&#8220;MyProject.Entities.User.dcm.xml&#8221; unless the extension is changed.</li>
<li>All mapping documents should get the extension &#8221;.dcm.xml&#8221; to
identify it as a Doctrine mapping file. This is more of a
convention and you are not forced to do this. You can change the
file extension easily enough.</li>
<li></li>
</ul>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$driver</span><span class="o">-&gt;</span><span class="na">setFileExtension</span><span class="p">(</span><span class="s1">&#39;.xml&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p>It is recommended to put all XML mapping documents in a single
folder but you can spread the documents over several folders if you
want to. In order to tell the XmlDriver where to look for your
mapping documents, supply an array of paths as the first argument
of the constructor, like this:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$config</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\Doctrine\ORM\Configuration</span><span class="p">();</span>
<span class="nv">$driver</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\Doctrine\ORM\Mapping\Driver\XmlDriver</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">&#39;/path/to/files1&#39;</span><span class="p">,</span> <span class="s1">&#39;/path/to/files2&#39;</span><span class="p">));</span>
<span class="nv">$config</span><span class="o">-&gt;</span><span class="na">setMetadataDriverImpl</span><span class="p">(</span><span class="nv">$driver</span><span class="p">);</span>
</pre></div>
</div>
<div class="section" id="simplified-xml-driver">
<h2>20.1. Simplified XML Driver<a class="headerlink" href="#simplified-xml-driver" title="Permalink to this headline"></a></h2>
<p>The Symfony project sponsored a driver that simplifies usage of the XML Driver.
The changes between the original driver are:</p>
<ol class="arabic simple">
<li>File Extension is .orm.xml</li>
<li>Filenames are shortened, &#8220;MyProjectEntitiesUser&#8221; will become User.orm.xml</li>
<li>You can add a global file and add multiple entities in this file.</li>
</ol>
<p>Configuration of this client works a little bit different:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$namespaces</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span>
    <span class="s1">&#39;MyProject\Entities&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;/path/to/files1&#39;</span><span class="p">,</span>
    <span class="s1">&#39;OtherProject\Entities&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;/path/to/files2&#39;</span>
<span class="p">);</span>
<span class="nv">$driver</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver</span><span class="p">(</span><span class="nv">$namespaces</span><span class="p">);</span>
<span class="nv">$driver</span><span class="o">-&gt;</span><span class="na">setGlobalBasename</span><span class="p">(</span><span class="s1">&#39;global&#39;</span><span class="p">);</span> <span class="c1">// global.orm.xml</span>
</pre></div>
</div>
<div class="section" id="example">
<h3>20.1.1. Example<a class="headerlink" href="#example" title="Permalink to this headline"></a></h3>
<p>As a quick start, here is a small example document that makes use
of several common elements:</p>
<div class="highlight-xml"><div class="highlight"><pre>// Doctrine.Tests.ORM.Mapping.User.dcm.xml
<span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<span class="nt">&lt;doctrine-mapping</span> <span class="na">xmlns=</span><span class="s">&quot;http://doctrine-project.org/schemas/orm/doctrine-mapping&quot;</span>
      <span class="na">xmlns:xsi=</span><span class="s">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
      <span class="na">xsi:schemaLocation=</span><span class="s">&quot;http://doctrine-project.org/schemas/orm/doctrine-mapping</span>
<span class="s">                          http://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd&quot;</span><span class="nt">&gt;</span>

    <span class="nt">&lt;entity</span> <span class="na">name=</span><span class="s">&quot;Doctrine\Tests\ORM\Mapping\User&quot;</span> <span class="na">table=</span><span class="s">&quot;cms_users&quot;</span><span class="nt">&gt;</span>

        <span class="nt">&lt;indexes&gt;</span>
            <span class="nt">&lt;index</span> <span class="na">name=</span><span class="s">&quot;name_idx&quot;</span> <span class="na">columns=</span><span class="s">&quot;name&quot;</span><span class="nt">/&gt;</span>
            <span class="nt">&lt;index</span> <span class="na">columns=</span><span class="s">&quot;user_email&quot;</span><span class="nt">/&gt;</span>
        <span class="nt">&lt;/indexes&gt;</span>

        <span class="nt">&lt;unique-constraints&gt;</span>
            <span class="nt">&lt;unique-constraint</span> <span class="na">columns=</span><span class="s">&quot;name,user_email&quot;</span> <span class="na">name=</span><span class="s">&quot;search_idx&quot;</span> <span class="nt">/&gt;</span>
        <span class="nt">&lt;/unique-constraints&gt;</span>

        <span class="nt">&lt;lifecycle-callbacks&gt;</span>
            <span class="nt">&lt;lifecycle-callback</span> <span class="na">type=</span><span class="s">&quot;prePersist&quot;</span> <span class="na">method=</span><span class="s">&quot;doStuffOnPrePersist&quot;</span><span class="nt">/&gt;</span>
            <span class="nt">&lt;lifecycle-callback</span> <span class="na">type=</span><span class="s">&quot;prePersist&quot;</span> <span class="na">method=</span><span class="s">&quot;doOtherStuffOnPrePersistToo&quot;</span><span class="nt">/&gt;</span>
            <span class="nt">&lt;lifecycle-callback</span> <span class="na">type=</span><span class="s">&quot;postPersist&quot;</span> <span class="na">method=</span><span class="s">&quot;doStuffOnPostPersist&quot;</span><span class="nt">/&gt;</span>
        <span class="nt">&lt;/lifecycle-callbacks&gt;</span>

        <span class="nt">&lt;id</span> <span class="na">name=</span><span class="s">&quot;id&quot;</span> <span class="na">type=</span><span class="s">&quot;integer&quot;</span> <span class="na">column=</span><span class="s">&quot;id&quot;</span><span class="nt">&gt;</span>
            <span class="nt">&lt;generator</span> <span class="na">strategy=</span><span class="s">&quot;AUTO&quot;</span><span class="nt">/&gt;</span>
            <span class="nt">&lt;sequence-generator</span> <span class="na">sequence-name=</span><span class="s">&quot;tablename_seq&quot;</span> <span class="na">allocation-size=</span><span class="s">&quot;100&quot;</span> <span class="na">initial-value=</span><span class="s">&quot;1&quot;</span> <span class="nt">/&gt;</span>
        <span class="nt">&lt;/id&gt;</span>

        <span class="nt">&lt;field</span> <span class="na">name=</span><span class="s">&quot;name&quot;</span> <span class="na">column=</span><span class="s">&quot;name&quot;</span> <span class="na">type=</span><span class="s">&quot;string&quot;</span> <span class="na">length=</span><span class="s">&quot;50&quot;</span> <span class="na">nullable=</span><span class="s">&quot;true&quot;</span> <span class="na">unique=</span><span class="s">&quot;true&quot;</span> <span class="nt">/&gt;</span>
        <span class="nt">&lt;field</span> <span class="na">name=</span><span class="s">&quot;email&quot;</span> <span class="na">column=</span><span class="s">&quot;user_email&quot;</span> <span class="na">type=</span><span class="s">&quot;string&quot;</span> <span class="na">column-definition=</span><span class="s">&quot;CHAR(32) NOT NULL&quot;</span> <span class="nt">/&gt;</span>

        <span class="nt">&lt;one-to-one</span> <span class="na">field=</span><span class="s">&quot;address&quot;</span> <span class="na">target-entity=</span><span class="s">&quot;Address&quot;</span> <span class="na">inversed-by=</span><span class="s">&quot;user&quot;</span><span class="nt">&gt;</span>
            <span class="nt">&lt;cascade&gt;&lt;cascade-remove</span> <span class="nt">/&gt;&lt;/cascade&gt;</span>
            <span class="nt">&lt;join-column</span> <span class="na">name=</span><span class="s">&quot;address_id&quot;</span> <span class="na">referenced-column-name=</span><span class="s">&quot;id&quot;</span> <span class="na">on-delete=</span><span class="s">&quot;CASCADE&quot;</span> <span class="na">on-update=</span><span class="s">&quot;CASCADE&quot;</span><span class="nt">/&gt;</span>
        <span class="nt">&lt;/one-to-one&gt;</span>

        <span class="nt">&lt;one-to-many</span> <span class="na">field=</span><span class="s">&quot;phonenumbers&quot;</span> <span class="na">target-entity=</span><span class="s">&quot;Phonenumber&quot;</span> <span class="na">mapped-by=</span><span class="s">&quot;user&quot;</span><span class="nt">&gt;</span>
            <span class="nt">&lt;cascade&gt;</span>
                <span class="nt">&lt;cascade-persist/&gt;</span>
            <span class="nt">&lt;/cascade&gt;</span>
            <span class="nt">&lt;order-by&gt;</span>
                <span class="nt">&lt;order-by-field</span> <span class="na">name=</span><span class="s">&quot;number&quot;</span> <span class="na">direction=</span><span class="s">&quot;ASC&quot;</span> <span class="nt">/&gt;</span>
            <span class="nt">&lt;/order-by&gt;</span>
        <span class="nt">&lt;/one-to-many&gt;</span>

        <span class="nt">&lt;many-to-many</span> <span class="na">field=</span><span class="s">&quot;groups&quot;</span> <span class="na">target-entity=</span><span class="s">&quot;Group&quot;</span><span class="nt">&gt;</span>
            <span class="nt">&lt;cascade&gt;</span>
                <span class="nt">&lt;cascade-all/&gt;</span>
            <span class="nt">&lt;/cascade&gt;</span>
            <span class="nt">&lt;join-table</span> <span class="na">name=</span><span class="s">&quot;cms_users_groups&quot;</span><span class="nt">&gt;</span>
                <span class="nt">&lt;join-columns&gt;</span>
                    <span class="nt">&lt;join-column</span> <span class="na">name=</span><span class="s">&quot;user_id&quot;</span> <span class="na">referenced-column-name=</span><span class="s">&quot;id&quot;</span> <span class="na">nullable=</span><span class="s">&quot;false&quot;</span> <span class="na">unique=</span><span class="s">&quot;false&quot;</span> <span class="nt">/&gt;</span>
                <span class="nt">&lt;/join-columns&gt;</span>
                <span class="nt">&lt;inverse-join-columns&gt;</span>
                    <span class="nt">&lt;join-column</span> <span class="na">name=</span><span class="s">&quot;group_id&quot;</span> <span class="na">referenced-column-name=</span><span class="s">&quot;id&quot;</span> <span class="na">column-definition=</span><span class="s">&quot;INT NULL&quot;</span> <span class="nt">/&gt;</span>
                <span class="nt">&lt;/inverse-join-columns&gt;</span>
            <span class="nt">&lt;/join-table&gt;</span>
        <span class="nt">&lt;/many-to-many&gt;</span>

    <span class="nt">&lt;/entity&gt;</span>

<span class="nt">&lt;/doctrine-mapping&gt;</span>
</pre></div>
</div>
<p>Be aware that class-names specified in the XML files should be
fully qualified.</p>
</div>
<div class="section" id="xml-element-reference">
<h3>20.1.2. XML-Element Reference<a class="headerlink" href="#xml-element-reference" title="Permalink to this headline"></a></h3>
<p>The XML-Element reference explains all the tags and attributes that
the Doctrine Mapping XSD Schema defines. You should read the
Basic-, Association- and Inheritance Mapping chapters to understand
what each of this definitions means in detail.</p>
</div>
</div>
<div class="section" id="defining-an-entity">
<h2>20.2. Defining an Entity<a class="headerlink" href="#defining-an-entity" title="Permalink to this headline"></a></h2>
<p>Each XML Mapping File contains the definition of one entity,
specified as the <tt class="docutils literal"><span class="pre">&lt;entity</span> <span class="pre">/&gt;</span></tt> element as a direct child of the
<tt class="docutils literal"><span class="pre">&lt;doctrine-mapping</span> <span class="pre">/&gt;</span></tt> element:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;doctrine-mapping&gt;</span>
    <span class="nt">&lt;entity</span> <span class="na">name=</span><span class="s">&quot;MyProject\User&quot;</span> <span class="na">table=</span><span class="s">&quot;cms_users&quot;</span> <span class="na">repository-class=</span><span class="s">&quot;MyProject\UserRepository&quot;</span><span class="nt">&gt;</span>
        <span class="c">&lt;!-- definition here --&gt;</span>
    <span class="nt">&lt;/entity&gt;</span>
<span class="nt">&lt;/doctrine-mapping&gt;</span>
</pre></div>
</div>
<p>Required attributes:</p>
<ul class="simple">
<li>name - The fully qualified class-name of the entity.</li>
</ul>
<p>Optional attributes:</p>
<ul class="simple">
<li><strong>table</strong> - The Table-Name to be used for this entity. Otherwise the
Unqualified Class-Name is used by default.</li>
<li><strong>repository-class</strong> - The fully qualified class-name of an
alternative <tt class="docutils literal"><span class="pre">Doctrine\ORM\EntityRepository</span></tt> implementation to be
used with this entity.</li>
<li><strong>inheritance-type</strong> - The type of inheritance, defaults to none. A
more detailed description follows in the
<em>Defining Inheritance Mappings</em> section.</li>
<li><strong>read-only</strong> - (&gt;= 2.1) Specifies that this entity is marked as read only and not
considered for change-tracking. Entities of this type can be persisted
and removed though.</li>
</ul>
</div>
<div class="section" id="defining-fields">
<h2>20.3. Defining Fields<a class="headerlink" href="#defining-fields" title="Permalink to this headline"></a></h2>
<p>Each entity class can contain zero to infinite fields that are
managed by Doctrine. You can define them using the <tt class="docutils literal"><span class="pre">&lt;field</span> <span class="pre">/&gt;</span></tt>
element as a children to the <tt class="docutils literal"><span class="pre">&lt;entity</span> <span class="pre">/&gt;</span></tt> element. The field
element is only used for primitive types that are not the ID of the
entity. For the ID mapping you have to use the <tt class="docutils literal"><span class="pre">&lt;id</span> <span class="pre">/&gt;</span></tt> element.</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;entity</span> <span class="na">name=</span><span class="s">&quot;MyProject\User&quot;</span><span class="nt">&gt;</span>

    <span class="nt">&lt;field</span> <span class="na">name=</span><span class="s">&quot;name&quot;</span> <span class="na">type=</span><span class="s">&quot;string&quot;</span> <span class="na">length=</span><span class="s">&quot;50&quot;</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;field</span> <span class="na">name=</span><span class="s">&quot;username&quot;</span> <span class="na">type=</span><span class="s">&quot;string&quot;</span> <span class="na">unique=</span><span class="s">&quot;true&quot;</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;field</span> <span class="na">name=</span><span class="s">&quot;age&quot;</span> <span class="na">type=</span><span class="s">&quot;integer&quot;</span> <span class="na">nullable=</span><span class="s">&quot;true&quot;</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;field</span> <span class="na">name=</span><span class="s">&quot;isActive&quot;</span> <span class="na">column=</span><span class="s">&quot;is_active&quot;</span> <span class="na">type=</span><span class="s">&quot;boolean&quot;</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;field</span> <span class="na">name=</span><span class="s">&quot;weight&quot;</span> <span class="na">type=</span><span class="s">&quot;decimal&quot;</span> <span class="na">scale=</span><span class="s">&quot;5&quot;</span> <span class="na">precision=</span><span class="s">&quot;2&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/entity&gt;</span>
</pre></div>
</div>
<p>Required attributes:</p>
<ul class="simple">
<li>name - The name of the Property/Field on the given Entity PHP
class.</li>
</ul>
<p>Optional attributes:</p>
<ul class="simple">
<li>type - The <tt class="docutils literal"><span class="pre">Doctrine\DBAL\Types\Type</span></tt> name, defaults to
&#8220;string&#8221;</li>
<li>column - Name of the column in the database, defaults to the
field name.</li>
<li>length - The length of the given type, for use with strings
only.</li>
<li>unique - Should this field contain a unique value across the
table? Defaults to false.</li>
<li>nullable - Should this field allow NULL as a value? Defaults to
false.</li>
<li>version - Should this field be used for optimistic locking? Only
works on fields with type integer or datetime.</li>
<li>scale - Scale of a decimal type.</li>
<li>precision - Precision of a decimal type.</li>
<li>column-definition - Optional alternative SQL representation for
this column. This definition begin after the field-name and has to
specify the complete column definition. Using this feature will
turn this field dirty for Schema-Tool update commands at all
times.</li>
</ul>
</div>
<div class="section" id="defining-identity-and-generator-strategies">
<h2>20.4. Defining Identity and Generator Strategies<a class="headerlink" href="#defining-identity-and-generator-strategies" title="Permalink to this headline"></a></h2>
<p>An entity has to have at least one <tt class="docutils literal"><span class="pre">&lt;id</span> <span class="pre">/&gt;</span></tt> element. For
composite keys you can specify more than one id-element, however
surrogate keys are recommended for use with Doctrine 2. The Id
field allows to define properties of the identifier and allows a
subset of the <tt class="docutils literal"><span class="pre">&lt;field</span> <span class="pre">/&gt;</span></tt> element attributes:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;entity</span> <span class="na">name=</span><span class="s">&quot;MyProject\User&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;id</span> <span class="na">name=</span><span class="s">&quot;id&quot;</span> <span class="na">type=</span><span class="s">&quot;integer&quot;</span> <span class="na">column=</span><span class="s">&quot;user_id&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/entity&gt;</span>
</pre></div>
</div>
<p>Required attributes:</p>
<ul class="simple">
<li>name - The name of the Property/Field on the given Entity PHP
class.</li>
<li>type - The <tt class="docutils literal"><span class="pre">Doctrine\DBAL\Types\Type</span></tt> name, preferably
&#8220;string&#8221; or &#8220;integer&#8221;.</li>
</ul>
<p>Optional attributes:</p>
<ul class="simple">
<li>column - Name of the column in the database, defaults to the
field name.</li>
</ul>
<p>Using the simplified definition above Doctrine will use no
identifier strategy for this entity. That means you have to
manually set the identifier before calling
<tt class="docutils literal"><span class="pre">EntityManager#persist($entity)</span></tt>. This is the so called
<tt class="docutils literal"><span class="pre">ASSIGNED</span></tt> strategy.</p>
<p>If you want to switch the identifier generation strategy you have
to nest a <tt class="docutils literal"><span class="pre">&lt;generator</span> <span class="pre">/&gt;</span></tt> element inside the id-element. This of
course only works for surrogate keys. For composite keys you always
have to use the <tt class="docutils literal"><span class="pre">ASSIGNED</span></tt> strategy.</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;entity</span> <span class="na">name=</span><span class="s">&quot;MyProject\User&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;id</span> <span class="na">name=</span><span class="s">&quot;id&quot;</span> <span class="na">type=</span><span class="s">&quot;integer&quot;</span> <span class="na">column=</span><span class="s">&quot;user_id&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;generator</span> <span class="na">strategy=</span><span class="s">&quot;AUTO&quot;</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;/id&gt;</span>
<span class="nt">&lt;/entity&gt;</span>
</pre></div>
</div>
<p>The following values are allowed for the <tt class="docutils literal"><span class="pre">&lt;generator</span> <span class="pre">/&gt;</span></tt> strategy
attribute:</p>
<ul class="simple">
<li>AUTO - Automatic detection of the identifier strategy based on
the preferred solution of the database vendor.</li>
<li>IDENTITY - Use of a IDENTIFY strategy such as Auto-Increment IDs
available to Doctrine AFTER the INSERT statement has been executed.</li>
<li>SEQUENCE - Use of a database sequence to retrieve the
entity-ids. This is possible before the INSERT statement is
executed.</li>
</ul>
<p>If you are using the SEQUENCE strategy you can define an additional
element to describe the sequence:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;entity</span> <span class="na">name=</span><span class="s">&quot;MyProject\User&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;id</span> <span class="na">name=</span><span class="s">&quot;id&quot;</span> <span class="na">type=</span><span class="s">&quot;integer&quot;</span> <span class="na">column=</span><span class="s">&quot;user_id&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;generator</span> <span class="na">strategy=</span><span class="s">&quot;SEQUENCE&quot;</span> <span class="nt">/&gt;</span>
        <span class="nt">&lt;sequence-generator</span> <span class="na">sequence-name=</span><span class="s">&quot;user_seq&quot;</span> <span class="na">allocation-size=</span><span class="s">&quot;5&quot;</span> <span class="na">initial-value=</span><span class="s">&quot;1&quot;</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;/id&gt;</span>
<span class="nt">&lt;/entity&gt;</span>
</pre></div>
</div>
<p>Required attributes for <tt class="docutils literal"><span class="pre">&lt;sequence-generator</span> <span class="pre">/&gt;</span></tt>:</p>
<ul class="simple">
<li>sequence-name - The name of the sequence</li>
</ul>
<p>Optional attributes for <tt class="docutils literal"><span class="pre">&lt;sequence-generator</span> <span class="pre">/&gt;</span></tt>:</p>
<ul>
<li><p class="first">allocation-size - By how much steps should the sequence be
incremented when a value is retrieved. Defaults to 1</p>
</li>
<li><p class="first">initial-value - What should the initial value of the sequence
be.</p>
<blockquote>
<div><p><strong>NOTE</strong></p>
<p>If you want to implement a cross-vendor compatible application you
have to specify and additionally define the &lt;sequence-generator /&gt;
element, if Doctrine chooses the sequence strategy for a
platform.</p>
</div></blockquote>
</li>
</ul>
</div>
<div class="section" id="defining-a-mapped-superclass">
<h2>20.5. Defining a Mapped Superclass<a class="headerlink" href="#defining-a-mapped-superclass" title="Permalink to this headline"></a></h2>
<p>Sometimes you want to define a class that multiple entities inherit
from, which itself is not an entity however. The chapter on
<em>Inheritance Mapping</em> describes a Mapped Superclass in detail. You
can define it in XML using the <tt class="docutils literal"><span class="pre">&lt;mapped-superclass</span> <span class="pre">/&gt;</span></tt> tag.</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;doctrine-mapping&gt;</span>
    <span class="nt">&lt;mapped-superclass</span> <span class="na">name=</span><span class="s">&quot;MyProject\BaseClass&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;field</span> <span class="na">name=</span><span class="s">&quot;created&quot;</span> <span class="na">type=</span><span class="s">&quot;datetime&quot;</span> <span class="nt">/&gt;</span>
        <span class="nt">&lt;field</span> <span class="na">name=</span><span class="s">&quot;updated&quot;</span> <span class="na">type=</span><span class="s">&quot;datetime&quot;</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;/mapped-superclass&gt;</span>
<span class="nt">&lt;/doctrine-mapping&gt;</span>
</pre></div>
</div>
<p>Required attributes:</p>
<ul class="simple">
<li>name - Class name of the mapped superclass.</li>
</ul>
<p>You can nest any number of <tt class="docutils literal"><span class="pre">&lt;field</span> <span class="pre">/&gt;</span></tt> and unidirectional
<tt class="docutils literal"><span class="pre">&lt;many-to-one</span> <span class="pre">/&gt;</span></tt> or <tt class="docutils literal"><span class="pre">&lt;one-to-one</span> <span class="pre">/&gt;</span></tt> associations inside a
mapped superclass.</p>
</div>
<div class="section" id="defining-inheritance-mappings">
<h2>20.6. Defining Inheritance Mappings<a class="headerlink" href="#defining-inheritance-mappings" title="Permalink to this headline"></a></h2>
<p>There are currently two inheritance persistence strategies that you
can choose from when defining entities that inherit from each
other. Single Table inheritance saves the fields of the complete
inheritance hierarchy in a single table, joined table inheritance
creates a table for each entity combining the fields using join
conditions.</p>
<p>You can specify the inheritance type in the <tt class="docutils literal"><span class="pre">&lt;entity</span> <span class="pre">/&gt;</span></tt> element
and then use the <tt class="docutils literal"><span class="pre">&lt;discriminator-column</span> <span class="pre">/&gt;</span></tt> and
<tt class="docutils literal"><span class="pre">&lt;discriminator-mapping</span> <span class="pre">/&gt;</span></tt> attributes.</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;entity</span> <span class="na">name=</span><span class="s">&quot;MyProject\Animal&quot;</span> <span class="na">inheritance-type=</span><span class="s">&quot;JOINED&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;discriminator-column</span> <span class="na">name=</span><span class="s">&quot;discr&quot;</span> <span class="na">type=</span><span class="s">&quot;string&quot;</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;discriminator-map&gt;</span>
        <span class="nt">&lt;discriminator-mapping</span> <span class="na">value=</span><span class="s">&quot;cat&quot;</span> <span class="na">class=</span><span class="s">&quot;MyProject\Cat&quot;</span> <span class="nt">/&gt;</span>
        <span class="nt">&lt;discriminator-mapping</span> <span class="na">value=</span><span class="s">&quot;dog&quot;</span> <span class="na">class=</span><span class="s">&quot;MyProject\Dog&quot;</span> <span class="nt">/&gt;</span>
        <span class="nt">&lt;discriminator-mapping</span> <span class="na">value=</span><span class="s">&quot;mouse&quot;</span> <span class="na">class=</span><span class="s">&quot;MyProject\Mouse&quot;</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;/discriminator-map&gt;</span>
<span class="nt">&lt;/entity&gt;</span>
</pre></div>
</div>
<p>The allowed values for inheritance-type attribute are <tt class="docutils literal"><span class="pre">JOINED</span></tt> or
<tt class="docutils literal"><span class="pre">SINGLE_TABLE</span></tt>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">All inheritance related definitions have to be defined on the root
entity of the hierarchy.</p>
</div>
</div>
<div class="section" id="defining-lifecycle-callbacks">
<h2>20.7. Defining Lifecycle Callbacks<a class="headerlink" href="#defining-lifecycle-callbacks" title="Permalink to this headline"></a></h2>
<p>You can define the lifecycle callback methods on your entities
using the <tt class="docutils literal"><span class="pre">&lt;lifecycle-callbacks</span> <span class="pre">/&gt;</span></tt> element:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;entity</span> <span class="na">name=</span><span class="s">&quot;Doctrine\Tests\ORM\Mapping\User&quot;</span> <span class="na">table=</span><span class="s">&quot;cms_users&quot;</span><span class="nt">&gt;</span>

    <span class="nt">&lt;lifecycle-callbacks&gt;</span>
        <span class="nt">&lt;lifecycle-callback</span> <span class="na">type=</span><span class="s">&quot;prePersist&quot;</span> <span class="na">method=</span><span class="s">&quot;onPrePersist&quot;</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;/lifecycle-callbacks&gt;</span>
<span class="nt">&lt;/entity&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="defining-one-to-one-relations">
<h2>20.8. Defining One-To-One Relations<a class="headerlink" href="#defining-one-to-one-relations" title="Permalink to this headline"></a></h2>
<p>You can define One-To-One Relations/Associations using the
<tt class="docutils literal"><span class="pre">&lt;one-to-one</span> <span class="pre">/&gt;</span></tt> element. The required and optional attributes
depend on the associations being on the inverse or owning side.</p>
<p>For the inverse side the mapping is as simple as:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;entity</span> <span class="na">class=</span><span class="s">&quot;MyProject\User&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;one-to-one</span> <span class="na">field=</span><span class="s">&quot;address&quot;</span> <span class="na">target-entity=</span><span class="s">&quot;Address&quot;</span> <span class="na">mapped-by=</span><span class="s">&quot;user&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/entity&gt;</span>
</pre></div>
</div>
<p>Required attributes for inverse One-To-One:</p>
<ul class="simple">
<li>field - Name of the property/field on the entity&#8217;s PHP class.</li>
<li>target-entity - Name of the entity associated entity class. If
this is not qualified the namespace of the current class is
prepended. <em>IMPORTANT:</em> No leading backslash!</li>
<li>mapped-by - Name of the field on the owning side (here Address
entity) that contains the owning side association.</li>
</ul>
<p>For the owning side this mapping would look like:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;entity</span> <span class="na">class=</span><span class="s">&quot;MyProject\Address&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;one-to-one</span> <span class="na">field=</span><span class="s">&quot;user&quot;</span> <span class="na">target-entity=</span><span class="s">&quot;User&quot;</span> <span class="na">inversed-by=</span><span class="s">&quot;address&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/entity&gt;</span>
</pre></div>
</div>
<p>Required attributes for owning One-to-One:</p>
<ul class="simple">
<li>field - Name of the property/field on the entity&#8217;s PHP class.</li>
<li>target-entity - Name of the entity associated entity class. If
this is not qualified the namespace of the current class is
prepended. <em>IMPORTANT:</em> No leading backslash!</li>
</ul>
<p>Optional attributes for owning One-to-One:</p>
<ul class="simple">
<li>inversed-by - If the association is bidirectional the
inversed-by attribute has to be specified with the name of the
field on the inverse entity that contains the back-reference.</li>
<li>orphan-removal - If true, the inverse side entity is always
deleted when the owning side entity is. Defaults to false.</li>
<li>fetch - Either LAZY or EAGER, defaults to LAZY. This attribute
makes only sense on the owning side, the inverse side <em>ALWAYS</em> has
to use the <tt class="docutils literal"><span class="pre">FETCH</span></tt> strategy.</li>
</ul>
<p>The definition for the owning side relies on a bunch of mapping
defaults for the join column names. Without the nested
<tt class="docutils literal"><span class="pre">&lt;join-column</span> <span class="pre">/&gt;</span></tt> element Doctrine assumes to foreign key to be
called <tt class="docutils literal"><span class="pre">user_id</span></tt> on the Address Entities table. This is because
the <tt class="docutils literal"><span class="pre">MyProject\Address</span></tt> entity is the owning side of this
association, which means it contains the foreign key.</p>
<p>The completed explicitly defined mapping is:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;entity</span> <span class="na">class=</span><span class="s">&quot;MyProject\Address&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;one-to-one</span> <span class="na">field=</span><span class="s">&quot;user&quot;</span> <span class="na">target-entity=</span><span class="s">&quot;User&quot;</span> <span class="na">inversed-by=</span><span class="s">&quot;address&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;join-column</span> <span class="na">name=</span><span class="s">&quot;user_id&quot;</span> <span class="na">referenced-column-name=</span><span class="s">&quot;id&quot;</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;/one-to-one&gt;</span>
<span class="nt">&lt;/entity&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="defining-many-to-one-associations">
<h2>20.9. Defining Many-To-One Associations<a class="headerlink" href="#defining-many-to-one-associations" title="Permalink to this headline"></a></h2>
<p>The many-to-one association is <em>ALWAYS</em> the owning side of any
bidirectional association. This simplifies the mapping compared to
the one-to-one case. The minimal mapping for this association looks
like:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;entity</span> <span class="na">class=</span><span class="s">&quot;MyProject\Article&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;many-to-one</span> <span class="na">field=</span><span class="s">&quot;author&quot;</span> <span class="na">target-entity=</span><span class="s">&quot;User&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/entity&gt;</span>
</pre></div>
</div>
<p>Required attributes:</p>
<ul class="simple">
<li>field - Name of the property/field on the entity&#8217;s PHP class.</li>
<li>target-entity - Name of the entity associated entity class. If
this is not qualified the namespace of the current class is
prepended. <em>IMPORTANT:</em> No leading backslash!</li>
</ul>
<p>Optional attributes:</p>
<ul class="simple">
<li>inversed-by - If the association is bidirectional the
inversed-by attribute has to be specified with the name of the
field on the inverse entity that contains the back-reference.</li>
<li>orphan-removal - If true the entity on the inverse side is
always deleted when the owning side entity is and it is not
connected to any other owning side entity anymore. Defaults to
false.</li>
<li>fetch - Either LAZY or EAGER, defaults to LAZY.</li>
</ul>
<p>This definition relies on a bunch of mapping defaults with regards
to the naming of the join-column/foreign key. The explicitly
defined mapping includes a <tt class="docutils literal"><span class="pre">&lt;join-column</span> <span class="pre">/&gt;</span></tt> tag nested inside
the many-to-one association tag:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;entity</span> <span class="na">class=</span><span class="s">&quot;MyProject\Article&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;many-to-one</span> <span class="na">field=</span><span class="s">&quot;author&quot;</span> <span class="na">target-entity=</span><span class="s">&quot;User&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;join-column</span> <span class="na">name=</span><span class="s">&quot;author_id&quot;</span> <span class="na">referenced-column-name=</span><span class="s">&quot;id&quot;</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;/many-to-one&gt;</span>
<span class="nt">&lt;/entity&gt;</span>
</pre></div>
</div>
<p>The join-column attribute <tt class="docutils literal"><span class="pre">name</span></tt> specifies the column name of the
foreign key and the <tt class="docutils literal"><span class="pre">referenced-column-name</span></tt> attribute specifies
the name of the primary key column on the User entity.</p>
</div>
<div class="section" id="defining-one-to-many-associations">
<h2>20.10. Defining One-To-Many Associations<a class="headerlink" href="#defining-one-to-many-associations" title="Permalink to this headline"></a></h2>
<p>The one-to-many association is <em>ALWAYS</em> the inverse side of any
association. There exists no such thing as a uni-directional
one-to-many association, which means this association only ever
exists for bi-directional associations.</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;entity</span> <span class="na">class=</span><span class="s">&quot;MyProject\User&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;one-to-many</span> <span class="na">field=</span><span class="s">&quot;phonenumbers&quot;</span> <span class="na">target-entity=</span><span class="s">&quot;Phonenumber&quot;</span> <span class="na">mapped-by=</span><span class="s">&quot;user&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/entity&gt;</span>
</pre></div>
</div>
<p>Required attributes:</p>
<ul class="simple">
<li>field - Name of the property/field on the entity&#8217;s PHP class.</li>
<li>target-entity - Name of the entity associated entity class. If
this is not qualified the namespace of the current class is
prepended. <em>IMPORTANT:</em> No leading backslash!</li>
<li>mapped-by - Name of the field on the owning side (here
Phonenumber entity) that contains the owning side association.</li>
</ul>
<p>Optional attributes:</p>
<ul class="simple">
<li>fetch - Either LAZY, EXTRA_LAZY or EAGER, defaults to LAZY.</li>
<li>index-by: Index the collection by a field on the target entity.</li>
</ul>
</div>
<div class="section" id="defining-many-to-many-associations">
<h2>20.11. Defining Many-To-Many Associations<a class="headerlink" href="#defining-many-to-many-associations" title="Permalink to this headline"></a></h2>
<p>From all the associations the many-to-many has the most complex
definition. When you rely on the mapping defaults you can omit many
definitions and rely on their implicit values.</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;entity</span> <span class="na">class=</span><span class="s">&quot;MyProject\User&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;many-to-many</span> <span class="na">field=</span><span class="s">&quot;groups&quot;</span> <span class="na">target-entity=</span><span class="s">&quot;Group&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/entity&gt;</span>
</pre></div>
</div>
<p>Required attributes:</p>
<ul class="simple">
<li>field - Name of the property/field on the entity&#8217;s PHP class.</li>
<li>target-entity - Name of the entity associated entity class. If
this is not qualified the namespace of the current class is
prepended. <em>IMPORTANT:</em> No leading backslash!</li>
</ul>
<p>Optional attributes:</p>
<ul class="simple">
<li>mapped-by - Name of the field on the owning side that contains
the owning side association if the defined many-to-many association
is on the inverse side.</li>
<li>inversed-by - If the association is bidirectional the
inversed-by attribute has to be specified with the name of the
field on the inverse entity that contains the back-reference.</li>
<li>fetch - Either LAZY, EXTRA_LAZY or EAGER, defaults to LAZY.</li>
<li>index-by: Index the collection by a field on the target entity.</li>
</ul>
<p>The mapping defaults would lead to a join-table with the name
&#8220;User_Group&#8221; being created that contains two columns &#8220;user_id&#8221;
and &#8220;group_id&#8221;. The explicit definition of this mapping would be:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;entity</span> <span class="na">class=</span><span class="s">&quot;MyProject\User&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;many-to-many</span> <span class="na">field=</span><span class="s">&quot;groups&quot;</span> <span class="na">target-entity=</span><span class="s">&quot;Group&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;join-table</span> <span class="na">name=</span><span class="s">&quot;cms_users_groups&quot;</span><span class="nt">&gt;</span>
            <span class="nt">&lt;join-columns&gt;</span>
                <span class="nt">&lt;join-column</span> <span class="na">name=</span><span class="s">&quot;user_id&quot;</span> <span class="na">referenced-column-name=</span><span class="s">&quot;id&quot;</span><span class="nt">/&gt;</span>
            <span class="nt">&lt;/join-columns&gt;</span>
            <span class="nt">&lt;inverse-join-columns&gt;</span>
                <span class="nt">&lt;join-column</span> <span class="na">name=</span><span class="s">&quot;group_id&quot;</span> <span class="na">referenced-column-name=</span><span class="s">&quot;id&quot;</span><span class="nt">/&gt;</span>
            <span class="nt">&lt;/inverse-join-columns&gt;</span>
        <span class="nt">&lt;/join-table&gt;</span>
    <span class="nt">&lt;/many-to-many&gt;</span>
<span class="nt">&lt;/entity&gt;</span>
</pre></div>
</div>
<p>Here both the <tt class="docutils literal"><span class="pre">&lt;join-columns&gt;</span></tt> and <tt class="docutils literal"><span class="pre">&lt;inverse-join-columns&gt;</span></tt>
tags are necessary to tell Doctrine for which side the specified
join-columns apply. These are nested inside a <tt class="docutils literal"><span class="pre">&lt;join-table</span> <span class="pre">/&gt;</span></tt>
attribute which allows to specify the table name of the
many-to-many join-table.</p>
</div>
<div class="section" id="cascade-element">
<h2>20.12. Cascade Element<a class="headerlink" href="#cascade-element" title="Permalink to this headline"></a></h2>
<p>Doctrine allows cascading of several UnitOfWork operations to
related entities. You can specify the cascade operations in the
<tt class="docutils literal"><span class="pre">&lt;cascade</span> <span class="pre">/&gt;</span></tt> element inside any of the association mapping
tags.</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;entity</span> <span class="na">class=</span><span class="s">&quot;MyProject\User&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;many-to-many</span> <span class="na">field=</span><span class="s">&quot;groups&quot;</span> <span class="na">target-entity=</span><span class="s">&quot;Group&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;cascade&gt;</span>
            <span class="nt">&lt;cascade-all/&gt;</span>
        <span class="nt">&lt;/cascade&gt;</span>
    <span class="nt">&lt;/many-to-many&gt;</span>
<span class="nt">&lt;/entity&gt;</span>
</pre></div>
</div>
<p>Besides <tt class="docutils literal"><span class="pre">&lt;cascade-all</span> <span class="pre">/&gt;</span></tt> the following operations can be
specified by their respective tags:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">&lt;cascade-persist</span> <span class="pre">/&gt;</span></tt></li>
<li><tt class="docutils literal"><span class="pre">&lt;cascade-merge</span> <span class="pre">/&gt;</span></tt></li>
<li><tt class="docutils literal"><span class="pre">&lt;cascade-remove</span> <span class="pre">/&gt;</span></tt></li>
<li><tt class="docutils literal"><span class="pre">&lt;cascade-refresh</span> <span class="pre">/&gt;</span></tt></li>
</ul>
</div>
<div class="section" id="join-column-element">
<h2>20.13. Join Column Element<a class="headerlink" href="#join-column-element" title="Permalink to this headline"></a></h2>
<p>In any explicitly defined association mapping you will need the
<tt class="docutils literal"><span class="pre">&lt;join-column</span> <span class="pre">/&gt;</span></tt> tag. It defines how the foreign key and primary
key names are called that are used for joining two entities.</p>
<p>Required attributes:</p>
<ul class="simple">
<li>name - The column name of the foreign key.</li>
<li>referenced-column-name - The column name of the associated
entities primary key</li>
</ul>
<p>Optional attributes:</p>
<ul class="simple">
<li>unique - If the join column should contain a UNIQUE constraint.
This makes sense for Many-To-Many join-columns only to simulate a
one-to-many unidirectional using a join-table.</li>
<li>nullable - should the join column be nullable, defaults to true.</li>
<li>on-delete - Foreign Key Cascade action to perform when entity is
deleted, defaults to NO ACTION/RESTRICT but can be set to
&#8220;CASCADE&#8221;.</li>
</ul>
</div>
<div class="section" id="defining-order-of-to-many-associations">
<h2>20.14. Defining Order of To-Many Associations<a class="headerlink" href="#defining-order-of-to-many-associations" title="Permalink to this headline"></a></h2>
<p>You can require one-to-many or many-to-many associations to be
retrieved using an additional <tt class="docutils literal"><span class="pre">ORDER</span> <span class="pre">BY</span></tt>.</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;entity</span> <span class="na">class=</span><span class="s">&quot;MyProject\User&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;many-to-many</span> <span class="na">field=</span><span class="s">&quot;groups&quot;</span> <span class="na">target-entity=</span><span class="s">&quot;Group&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;order-by&gt;</span>
            <span class="nt">&lt;order-by-field</span> <span class="na">name=</span><span class="s">&quot;name&quot;</span> <span class="na">direction=</span><span class="s">&quot;ASC&quot;</span> <span class="nt">/&gt;</span>
        <span class="nt">&lt;/order-by&gt;</span>
    <span class="nt">&lt;/many-to-many&gt;</span>
<span class="nt">&lt;/entity&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="defining-indexes-or-unique-constraints">
<h2>20.15. Defining Indexes or Unique Constraints<a class="headerlink" href="#defining-indexes-or-unique-constraints" title="Permalink to this headline"></a></h2>
<p>To define additional indexes or unique constraints on the entities
table you can use the <tt class="docutils literal"><span class="pre">&lt;indexes</span> <span class="pre">/&gt;</span></tt> and
<tt class="docutils literal"><span class="pre">&lt;unique-constraints</span> <span class="pre">/&gt;</span></tt> elements:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;entity</span> <span class="na">name=</span><span class="s">&quot;Doctrine\Tests\ORM\Mapping\User&quot;</span> <span class="na">table=</span><span class="s">&quot;cms_users&quot;</span><span class="nt">&gt;</span>

    <span class="nt">&lt;indexes&gt;</span>
        <span class="nt">&lt;index</span> <span class="na">name=</span><span class="s">&quot;name_idx&quot;</span> <span class="na">columns=</span><span class="s">&quot;name&quot;</span><span class="nt">/&gt;</span>
        <span class="nt">&lt;index</span> <span class="na">columns=</span><span class="s">&quot;user_email&quot;</span><span class="nt">/&gt;</span>
    <span class="nt">&lt;/indexes&gt;</span>

    <span class="nt">&lt;unique-constraints&gt;</span>
        <span class="nt">&lt;unique-constraint</span> <span class="na">columns=</span><span class="s">&quot;name,user_email&quot;</span> <span class="na">name=</span><span class="s">&quot;search_idx&quot;</span> <span class="nt">/&gt;</span>
    <span class="nt">&lt;/unique-constraints&gt;</span>
<span class="nt">&lt;/entity&gt;</span>
</pre></div>
</div>
<p>You have to specify the column and not the entity-class field names
in the index and unique-constraint definitions.</p>
</div>
<div class="section" id="derived-entities-id-syntax">
<h2>20.16. Derived Entities ID syntax<a class="headerlink" href="#derived-entities-id-syntax" title="Permalink to this headline"></a></h2>
<p>If the primary key of an entity contains a foreign key to another entity we speak of a derived
entity relationship. You can define this in XML with the &#8220;association-key&#8221; attribute in the <tt class="docutils literal"><span class="pre">&lt;id&gt;</span></tt> tag.</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;doctrine-mapping</span> <span class="na">xmlns=</span><span class="s">&quot;http://doctrine-project.org/schemas/orm/doctrine-mapping&quot;</span>
      <span class="na">xmlns:xsi=</span><span class="s">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
      <span class="na">xsi:schemaLocation=</span><span class="s">&quot;http://doctrine-project.org/schemas/orm/doctrine-mapping</span>
<span class="s">                    http://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd&quot;</span><span class="nt">&gt;</span>

     <span class="nt">&lt;entity</span> <span class="na">name=</span><span class="s">&quot;Application\Model\ArticleAttribute&quot;</span><span class="nt">&gt;</span>
        <span class="nt">&lt;id</span> <span class="na">name=</span><span class="s">&quot;article&quot;</span> <span class="na">association-key=</span><span class="s">&quot;true&quot;</span> <span class="nt">/&gt;</span>
        <span class="nt">&lt;id</span> <span class="na">name=</span><span class="s">&quot;attribute&quot;</span> <span class="na">type=</span><span class="s">&quot;string&quot;</span> <span class="nt">/&gt;</span>

        <span class="nt">&lt;field</span> <span class="na">name=</span><span class="s">&quot;value&quot;</span> <span class="na">type=</span><span class="s">&quot;string&quot;</span> <span class="nt">/&gt;</span>

        <span class="nt">&lt;many-to-one</span> <span class="na">field=</span><span class="s">&quot;article&quot;</span> <span class="na">target-entity=</span><span class="s">&quot;Article&quot;</span> <span class="na">inversed-by=</span><span class="s">&quot;attributes&quot;</span> <span class="nt">/&gt;</span>
     <span class="nt">&lt;entity&gt;</span>

<span class="nt">&lt;/doctrine-mapping&gt;</span>
</pre></div>
</div>
</div>
</div>


              </div>
                </div>

            </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
             
            <div id="searchbox" style="">
              <h3>Search</h3>
                <form class="search" action="http://readthedocs.org/search/project/" method="get">
                  <input type="text" name="q" size="18">
                  <input type="submit" value="Go">
                  <input type="hidden" name="selected_facets" value="project:">
                </form>
            </div>
            <h3><a href="../index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference internal" href="#">20. XML Mapping</a><ul>
<li><a class="reference internal" href="#simplified-xml-driver">20.1. Simplified XML Driver</a><ul>
<li><a class="reference internal" href="#example">20.1.1. Example</a></li>
<li><a class="reference internal" href="#xml-element-reference">20.1.2. XML-Element Reference</a></li>
</ul>
</li>
<li><a class="reference internal" href="#defining-an-entity">20.2. Defining an Entity</a></li>
<li><a class="reference internal" href="#defining-fields">20.3. Defining Fields</a></li>
<li><a class="reference internal" href="#defining-identity-and-generator-strategies">20.4. Defining Identity and Generator Strategies</a></li>
<li><a class="reference internal" href="#defining-a-mapped-superclass">20.5. Defining a Mapped Superclass</a></li>
<li><a class="reference internal" href="#defining-inheritance-mappings">20.6. Defining Inheritance Mappings</a></li>
<li><a class="reference internal" href="#defining-lifecycle-callbacks">20.7. Defining Lifecycle Callbacks</a></li>
<li><a class="reference internal" href="#defining-one-to-one-relations">20.8. Defining One-To-One Relations</a></li>
<li><a class="reference internal" href="#defining-many-to-one-associations">20.9. Defining Many-To-One Associations</a></li>
<li><a class="reference internal" href="#defining-one-to-many-associations">20.10. Defining One-To-Many Associations</a></li>
<li><a class="reference internal" href="#defining-many-to-many-associations">20.11. Defining Many-To-Many Associations</a></li>
<li><a class="reference internal" href="#cascade-element">20.12. Cascade Element</a></li>
<li><a class="reference internal" href="#join-column-element">20.13. Join Column Element</a></li>
<li><a class="reference internal" href="#defining-order-of-to-many-associations">20.14. Defining Order of To-Many Associations</a></li>
<li><a class="reference internal" href="#defining-indexes-or-unique-constraints">20.15. Defining Indexes or Unique Constraints</a></li>
<li><a class="reference internal" href="#derived-entities-id-syntax">20.16. Derived Entities ID syntax</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="partial-objects.html"
                                  title="previous chapter">19. Partial Objects</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="yaml-mapping.html"
                                  title="next chapter">21. YAML Mapping</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/reference/xml-mapping.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
        </div>
      </div>
          <div class="clearer"></div>
        </div>
          <div class="footer">
              &copy; Copyright 2010-12, Doctrine Project Team.
              Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2.3.
            <br/>
            <a target="_BLANK" href="http://www.servergrove.com"><img src="../_static/servergrove.jpg" /></a>      <br/><br/>
            <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
              <input type="hidden" name="cmd" value="_s-xclick" />
              <input type="hidden" name="hosted_button_id" value="BAE2E3XANQ77Y" />
              <input type="Submit" value="Donate via PayPal" />
            </form>
          </div>
      </div>

      <div id="bot-rcnr">
        <div class="tl"><!-- corner --></div>
      </div>
    </div>

  <a class="githublink" href="http://github.com/doctrine">Fork me on GitHub</a>
  </body>
</html>