This file is indexed.

/usr/share/doc/php-doctrine-orm/html/_sources/tutorials/composite-primary-keys.txt 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
Composite and Foreign Keys as Primary Key
=========================================

.. versionadded:: 2.1

Doctrine 2 supports composite primary keys natively. Composite keys are a very powerful relational database concept
and we took good care to make sure Doctrine 2 supports as many of the composite primary key use-cases.
For Doctrine 2.0 composite keys of primitive data-types are supported, for Doctrine 2.1 even foreign keys as
primary keys are supported.

This tutorial shows how the semantics of composite primary keys work and how they map to the database.

General Considerations
~~~~~~~~~~~~~~~~~~~~~~

Every entity with a composite key cannot use an id generator other than "ASSIGNED". That means
the ID fields have to have their values set before you call ``EntityManager#persist($entity)``.

Primitive Types only
~~~~~~~~~~~~~~~~~~~~

Even in version 2.0 you can have composite keys as long as they only consist of the primitive types
``integer`` and ``string``. Suppose you want to create a database of cars and use the model-name
and year of production as primary keys:

.. configuration-block::

    .. code-block:: php

        <?php
        namespace VehicleCatalogue\Model;

        /**
         * @Entity
         */
        class Car
        {
            /** @Id @Column(type="string") */
            private $name;
            /** @Id @Column(type="integer") */
            private $year

            public function __construct($name, $year)
            {
                $this->name = $name;
                $this->year = $year;
            }

            public function getModelName()
            {
                return $this->name;
            }

            public function getYearOfProduction()
            {
                return $this->year;
            }
        }

    .. code-block:: xml

        <?xml version="1.0" encoding="UTF-8"?>
        <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                                  http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

            <entity name="VehicleCatalogue\Model\Car">
                <id field="name" type="string" />
                <id field="year" type="integer" />
            </entity>
        </doctrine-mapping>

    .. code-block:: yaml

        VehicleCatalogue\Model\Car:
          type: entity
          id:
            name:
              type: string
            year:
              type: integer

Now you can use this entity:

.. code-block:: php

    <?php
    namespace VehicleCatalogue\Model;

    // $em is the EntityManager

    $car = new Car("Audi A8", 2010);
    $em->persist($car);
    $em->flush();

And for querying you can use arrays to both DQL and EntityRepositories:

.. code-block:: php

    <?php
    namespace VehicleCatalogue\Model;

    // $em is the EntityManager
    $audi = $em->find("VehicleCatalogue\Model\Car", array("name" => "Audi A8", "year" => 2010));

    $dql = "SELECT c FROM VehicleCatalogue\Model\Car c WHERE c.id = ?1";
    $audi = $em->createQuery($dql)
               ->setParameter(1, array("name" => "Audi A8", "year" => 2010))
               ->getSingleResult();

You can also use this entity in associations. Doctrine will then generate two foreign keys one for ``name``
and to ``year`` to the related entities.

.. note::

    This example shows how you can nicely solve the requirement for existing
    values before ``EntityManager#persist()``: By adding them as mandatory values for the constructor.

Identity through foreign Entities
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. note::

    Identity through foreign entities is only supported with Doctrine 2.1

There are tons of use-cases where the identity of an Entity should be determined by the entity
of one or many parent entities.

-   Dynamic Attributes of an Entity (for example Article). Each Article has many
    attributes with primary key "article_id" and "attribute_name".
-   Address object of a Person, the primary key of the address is "user_id". This is not a case of a composite primary
    key, but the identity is derived through a foreign entity and a foreign key.
-   Join Tables with metadata can be modelled as Entity, for example connections between two articles
    with a little description and a score.

The semantics of mapping identity through foreign entities are easy:

-   Only allowed on Many-To-One or One-To-One associations.
-   Plug an ``@Id`` annotation onto every association.
-   Set an attribute ``association-key`` with the field name of the association in XML.
-   Set a key ``associationKey:`` with the field name of the association in YAML.

Use-Case 1: Dynamic Attributes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We keep up the example of an Article with arbitrary attributes, the mapping looks like this:

.. configuration-block::

    .. code-block:: php

        <?php
        namespace Application\Model;

        use Doctrine\Common\Collections\ArrayCollection;

        /**
         * @Entity
         */
        class Article
        {
            /** @Id @Column(type="integer") @GeneratedValue */
            private $id;
            /** @Column(type="string") */
            private $title;

            /**
             * @OneToMany(targetEntity="ArticleAttribute", mappedBy="article", cascade={"ALL"}, indexBy="attribute")
             */
            private $attributes;

            public function addAttribute($name, $value)
            {
                $this->attributes[$name] = new ArticleAttribute($name, $value, $this);
            }
        }

        /**
         * @Entity
         */
        class ArticleAttribute
        {
            /** @Id @ManyToOne(targetEntity="Article", inversedBy="attributes") */
            private $article;

            /** @Id @Column(type="string") */
            private $attribute;

            /** @Column(type="string") */
            private $value;

            public function __construct($name, $value, $article)
            {
                $this->attribute = $name;
                $this->value = $value;
                $this->article = $article;
            }
        }

    .. code-block:: xml

        <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                            http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

             <entity name="Application\Model\ArticleAttribute">
                <id name="article" association-key="true" />
                <id name="attribute" type="string" />
                
                <field name="value" type="string" />

                <many-to-one field="article" target-entity="Article" inversed-by="attributes" />
             <entity>

        </doctrine-mapping>

    .. code-block:: yaml

        Application\Model\ArticleAttribute:
          type: entity
          id:
            article:
              associationKey: true
            attribute:
              type: string
          fields:
            value:
              type: string
          manyToOne:
            article:
              targetEntity: Article
              inversedBy: attributes


Use-Case 2: Simple Derived Identity
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sometimes you have the requirement that two objects are related by a One-To-One association
and that the dependent class should re-use the primary key of the class it depends on.
One good example for this is a user-address relationship:

.. configuration-block::

    .. code-block:: php

        <?php
        /**
         * @Entity
         */
        class User
        {
            /** @Id @Column(type="integer") @GeneratedValue */
            private $id;
        }

        /**
         * @Entity
         */
        class Address
        {
            /** @Id @OneToOne(targetEntity="User") */
            private $user;
        }

    .. code-block:: yaml

        User:
          type: entity
          id:
            id:
              type: integer
              generator:
                strategy: AUTO

        Address:
          type: entity
          id:
            user:
              associationKey: true
          oneToOne:
            user:
              targetEntity: User


Use-Case 3: Join-Table with Metadata
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In the classic order product shop example there is the concept of the order item
which contains references to order and product and additional data such as the amount
of products purchased and maybe even the current price.

.. code-block:: php

    <?php
    use Doctrine\Common\Collections\ArrayCollection;

    /** @Entity */
    class Order
    {
        /** @Id @Column(type="integer") @GeneratedValue */
        private $id;

        /** @ManyToOne(targetEntity="Customer") */
        private $customer;
        /** @OneToMany(targetEntity="OrderItem", mappedBy="order") */
        private $items;

        /** @Column(type="boolean") */
        private $payed = false;
        /** @Column(type="boolean") */
        private $shipped = false;
        /** @Column(type="datetime") */
        private $created;

        public function __construct(Customer $customer)
        {
            $this->customer = $customer;
            $this->items = new ArrayCollection();
            $this->created = new \DateTime("now");
        }
    }

    /** @Entity */
    class Product
    {
        /** @Id @Column(type="integer") @GeneratedValue */
        private $id;

        /** @Column(type="string") */
        private $name;

        /** @Column(type="decimal") */
        private $currentPrice;

        public function getCurrentPrice()
        {
            return $this->currentPrice;
        }
    }

    /** @Entity */
    class OrderItem
    {
        /** @Id @ManyToOne(targetEntity="Order") */
        private $order;

        /** @Id @ManyToOne(targetEntity="Product") */
        private $product;

        /** @Column(type="integer") */
        private $amount = 1;

        /** @Column(type="decimal") */
        private $offeredPrice;

        public function __construct(Order $order, Product $product, $amount = 1)
        {
            $this->order = $order;
            $this->product = $product;
            $this->offeredPrice = $product->getCurrentPrice();
        }
    }


Performance Considerations
~~~~~~~~~~~~~~~~~~~~~~~~~~

Using composite keys always comes with a performance hit compared to using entities with
a simple surrogate key. This performance impact is mostly due to additional PHP code that is
necessary to handle this kind of keys, most notably when using derived identifiers.

On the SQL side there is not much overhead as no additional or unexpected queries have to be
executed to manage entities with derived foreign keys.