forked from doctrine/orm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLazyCriteriaCollection.php
133 lines (117 loc) · 3.78 KB
/
LazyCriteriaCollection.php
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
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM;
use Doctrine\Common\Collections\AbstractLazyCollection;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Selectable;
use Doctrine\ORM\Persisters\Entity\BasicEntityPersister;
use Doctrine\ORM\Persisters\Entity\EntityPersister;
/**
* A lazy collection that allow a fast count when using criteria object
* Once count gets executed once without collection being initialized, result
* is cached and returned on subsequent calls until collection gets loaded,
* then returning the number of loaded results.
*
* @since 2.5
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Michaël Gallego <mic.gallego@gmail.com>
*/
class LazyCriteriaCollection extends AbstractLazyCollection implements Selectable
{
/**
* @var BasicEntityPersister
*/
protected $entityPersister;
/**
* @var Criteria
*/
protected $criteria;
/**
* @var integer|null
*/
private $count;
/**
* @param EntityPersister $entityPersister
* @param Criteria $criteria
*/
public function __construct(EntityPersister $entityPersister, Criteria $criteria)
{
$this->entityPersister = $entityPersister;
$this->criteria = $criteria;
}
/**
* Do an efficient count on the collection
*
* @return integer
*/
public function count()
{
if ($this->isInitialized()) {
return $this->collection->count();
}
// Return cached result in case count query was already executed
if ($this->count !== null) {
return $this->count;
}
return $this->count = $this->entityPersister->count($this->criteria);
}
/**
* check if collection is empty without loading it
*
* @return boolean TRUE if the collection is empty, FALSE otherwise.
*/
public function isEmpty()
{
if ($this->isInitialized()) {
return $this->collection->isEmpty();
}
return !$this->count();
}
/**
* Do an optimized search of an element
*
* @param object $element
*
* @return bool
*/
public function contains($element)
{
if ($this->isInitialized()) {
return $this->collection->contains($element);
}
return $this->entityPersister->exists($element, $this->criteria);
}
/**
* {@inheritDoc}
*/
public function matching(Criteria $criteria)
{
$this->initialize();
return $this->collection->matching($criteria);
}
/**
* {@inheritDoc}
*/
protected function doInitialize()
{
$elements = $this->entityPersister->loadCriteria($this->criteria);
$this->collection = new ArrayCollection($elements);
}
}