LazyCriteriaCollection.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM;
  20. use Doctrine\Common\Collections\AbstractLazyCollection;
  21. use Doctrine\Common\Collections\ArrayCollection;
  22. use Doctrine\Common\Collections\Criteria;
  23. use Doctrine\Common\Collections\Selectable;
  24. use Doctrine\ORM\Persisters\Entity\EntityPersister;
  25. /**
  26. * A lazy collection that allow a fast count when using criteria object
  27. * Once count gets executed once without collection being initialized, result
  28. * is cached and returned on subsequent calls until collection gets loaded,
  29. * then returning the number of loaded results.
  30. */
  31. class LazyCriteriaCollection extends AbstractLazyCollection implements Selectable
  32. {
  33. /** @var EntityPersister */
  34. protected $entityPersister;
  35. /** @var Criteria */
  36. protected $criteria;
  37. /** @var int|null */
  38. private $count;
  39. public function __construct(EntityPersister $entityPersister, Criteria $criteria)
  40. {
  41. $this->entityPersister = $entityPersister;
  42. $this->criteria = $criteria;
  43. }
  44. /**
  45. * Do an efficient count on the collection
  46. *
  47. * @return int
  48. */
  49. public function count()
  50. {
  51. if ($this->isInitialized()) {
  52. return $this->collection->count();
  53. }
  54. // Return cached result in case count query was already executed
  55. if ($this->count !== null) {
  56. return $this->count;
  57. }
  58. return $this->count = $this->entityPersister->count($this->criteria);
  59. }
  60. /**
  61. * check if collection is empty without loading it
  62. *
  63. * @return bool TRUE if the collection is empty, FALSE otherwise.
  64. */
  65. public function isEmpty()
  66. {
  67. if ($this->isInitialized()) {
  68. return $this->collection->isEmpty();
  69. }
  70. return ! $this->count();
  71. }
  72. /**
  73. * Do an optimized search of an element
  74. *
  75. * @param object $element
  76. *
  77. * @return bool
  78. */
  79. public function contains($element)
  80. {
  81. if ($this->isInitialized()) {
  82. return $this->collection->contains($element);
  83. }
  84. return $this->entityPersister->exists($element, $this->criteria);
  85. }
  86. /**
  87. * {@inheritDoc}
  88. */
  89. public function matching(Criteria $criteria)
  90. {
  91. $this->initialize();
  92. return $this->collection->matching($criteria);
  93. }
  94. /**
  95. * {@inheritDoc}
  96. */
  97. protected function doInitialize()
  98. {
  99. $elements = $this->entityPersister->loadCriteria($this->criteria);
  100. $this->collection = new ArrayCollection($elements);
  101. }
  102. }