2013-12-19 23:07:50 +04:00
|
|
|
<?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;
|
|
|
|
|
2014-02-05 14:20:00 +04:00
|
|
|
use Doctrine\Common\Collections\AbstractLazyCollection;
|
2013-12-19 23:07:50 +04:00
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
|
|
use Doctrine\Common\Collections\Criteria;
|
2014-01-04 22:11:55 +04:00
|
|
|
use Doctrine\Common\Collections\Selectable;
|
2015-01-15 08:01:52 +03:00
|
|
|
use Doctrine\ORM\Persisters\Entity\BasicEntityPersister;
|
|
|
|
use Doctrine\ORM\Persisters\Entity\EntityPersister;
|
2013-12-19 23:07:50 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A lazy collection that allow a fast count when using criteria object
|
2014-05-16 08:22:11 +04:00
|
|
|
* Once count gets executed once without collection being initialized, result
|
|
|
|
* is cached and returned on subsequent calls until collection gets loaded,
|
2014-05-17 14:54:25 +04:00
|
|
|
* then returning the number of loaded results.
|
2013-12-19 23:07:50 +04:00
|
|
|
*
|
|
|
|
* @since 2.5
|
2014-05-16 08:22:11 +04:00
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
2013-12-19 23:07:50 +04:00
|
|
|
* @author Michaël Gallego <mic.gallego@gmail.com>
|
|
|
|
*/
|
2014-02-05 14:20:00 +04:00
|
|
|
class LazyCriteriaCollection extends AbstractLazyCollection implements Selectable
|
2013-12-19 23:07:50 +04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var BasicEntityPersister
|
|
|
|
*/
|
|
|
|
protected $entityPersister;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Criteria
|
|
|
|
*/
|
|
|
|
protected $criteria;
|
|
|
|
|
2014-05-16 08:22:11 +04:00
|
|
|
/**
|
2014-05-17 21:08:25 +04:00
|
|
|
* @var integer|null
|
2014-05-16 08:22:11 +04:00
|
|
|
*/
|
|
|
|
private $count;
|
|
|
|
|
2013-12-19 23:07:50 +04:00
|
|
|
/**
|
2014-01-05 16:41:34 +04:00
|
|
|
* @param EntityPersister $entityPersister
|
|
|
|
* @param Criteria $criteria
|
2013-12-19 23:07:50 +04:00
|
|
|
*/
|
2014-01-05 16:41:34 +04:00
|
|
|
public function __construct(EntityPersister $entityPersister, Criteria $criteria)
|
2013-12-19 23:07:50 +04:00
|
|
|
{
|
|
|
|
$this->entityPersister = $entityPersister;
|
|
|
|
$this->criteria = $criteria;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do an efficient count on the collection
|
|
|
|
*
|
2014-05-16 08:22:11 +04:00
|
|
|
* @return integer
|
2013-12-19 23:07:50 +04:00
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
2014-02-05 14:20:00 +04:00
|
|
|
if ($this->isInitialized()) {
|
2013-12-19 23:48:09 +04:00
|
|
|
return $this->collection->count();
|
2013-12-19 23:07:50 +04:00
|
|
|
}
|
|
|
|
|
2014-05-16 08:22:11 +04:00
|
|
|
// Return cached result in case count query was already executed
|
2014-05-16 22:03:20 +04:00
|
|
|
if ($this->count !== null) {
|
2014-05-16 08:22:11 +04:00
|
|
|
return $this->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->count = $this->entityPersister->count($this->criteria);
|
2013-12-19 23:07:50 +04:00
|
|
|
}
|
|
|
|
|
2014-05-17 14:54:25 +04:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2013-12-19 23:07:50 +04:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2014-02-14 02:26:23 +04:00
|
|
|
public function matching(Criteria $criteria)
|
2013-12-19 23:07:50 +04:00
|
|
|
{
|
2014-02-14 02:26:23 +04:00
|
|
|
$this->initialize();
|
2014-05-16 08:22:11 +04:00
|
|
|
|
2014-02-14 02:26:23 +04:00
|
|
|
return $this->collection->matching($criteria);
|
2013-12-19 23:07:50 +04:00
|
|
|
}
|
2014-01-04 22:11:55 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2014-02-14 02:26:23 +04:00
|
|
|
protected function doInitialize()
|
2014-01-04 22:11:55 +04:00
|
|
|
{
|
2014-02-14 02:26:23 +04:00
|
|
|
$elements = $this->entityPersister->loadCriteria($this->criteria);
|
|
|
|
$this->collection = new ArrayCollection($elements);
|
2014-01-04 22:11:55 +04:00
|
|
|
}
|
2013-12-19 23:07:50 +04:00
|
|
|
}
|