extra-lazy-associations.rst 3.2 KB

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