Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 83 lines (61 sloc) 3.257 kb
eea5b71 @beberlei Add tutorial on extra lazy associations
beberlei authored
1 Extra Lazy Associations
2 =======================
3
4 In many cases associations between entities can get pretty large. Even in a simple scenario like a blog.
ffa2a54 @rouffj Fix typo
rouffj authored
5 where posts can be commented, you always have to assume that a post draws hundreds of comments.
eea5b71 @beberlei Add tutorial on extra lazy associations
beberlei authored
6 In Doctrine 2.0 if you accessed an association it would always get loaded completly into memory. This
7 can lead to pretty serious performance problems, if your associations contain several hundrets or thousands
8 of entities.
9
10 With Doctrine 2.1 a feature called **Extra Lazy** is introduced for associations. Associations
11 are marked as **Lazy** by default, which means the whole collection object for an association is populated
12 the first time its accessed. If you mark an association as extra lazy the following methods on collections
13 can be called without triggering a full load of the collection:
14
15 - ``Collection#contains($entity)``
16 - ``Collection#count()``
17 - ``Collection#slice($offset, $length = null)``
18
19 For each of this three methods the following semantics apply:
20
21 - For each call, if the Collection is not yet loaded, issue a straight SELECT statement against the database.
22 - For each call, if the collection is already loaded, fallback to the default functionality for lazy collections. No additional SELECT statements are executed.
23
24 Additionally even with Doctrine 2.0 the following methods do not trigger the collection load:
25
26 - ``Collection#add($entity)``
ecb13a8 @beberlei Bugfix
beberlei authored
27 - ``Collection#offsetSet($key, $entity)`` - ArrayAccess with no specific key ``$coll[] = $entity``, it does
eea5b71 @beberlei Add tutorial on extra lazy associations
beberlei authored
28 not work when setting specific keys like ``$coll[0] = $entity``.
29
30 With extra lazy collections you can now not only add entities to large collections but also paginate them
31 easily using a combination of ``count`` and ``slice``.
32
33
34 Enabling Extra-Lazy Associations
35 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
36
37 The mapping configuration is simple. Instead of using the default value of ``fetch="LAZY"`` you have to
38 switch to extra lazy as shown in these examples:
39
40 .. configuration-block::
41
42 .. code-block:: php
43
44 <?php
45 namespace Doctrine\Tests\Models\CMS;
46
47 /**
48 * @Entity
49 */
50 class CmsGroup
51 {
52 /**
53 * @ManyToMany(targetEntity="CmsUser", mappedBy="groups", fetch="EXTRA_LAZY")
54 */
55 public $users;
56 }
57
58 .. code-block:: xml
59
60 <?xml version="1.0" encoding="UTF-8"?>
61 <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
62 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
63 xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
64 http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
65
66 <entity name="Doctrine\Tests\Models\CMS\CmsGroup">
67 <!-- ... -->
68 <many-to-many field="users" target-entity="CmsUser" mapped-by="groups" fetch="EXTRA_LAZY" />
69 </entity>
70 </doctrine-mapping>
71
72 .. code-block:: yaml
73
74 Doctrine\Tests\Models\CMS\CmsGroup:
75 type: entity
76 # ...
77 manyToMany:
78 users:
79 targetEntity: CmsUser
80 mappedBy: groups
81 fetch: EXTRA_LAZY
82
Something went wrong with that request. Please try again.