1
0
mirror of synced 2024-12-14 23:26:04 +03:00
doctrine2/lib/Doctrine/Collection/Batch.php

187 lines
6.1 KiB
PHP
Raw Normal View History

2006-05-30 12:59:08 +04:00
<?php
2006-09-28 01:21:33 +04:00
/*
* $Id$
*
* 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 LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
2006-05-30 12:59:08 +04:00
Doctrine::autoload('Doctrine_Collection');
/**
* Doctrine_Collection_Batch a collection of records,
* with batch load strategy
*
*
2006-12-29 17:01:31 +03:00
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @package Doctrine
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version $Revision$
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
*/
2006-12-29 17:40:47 +03:00
class Doctrine_Collection_Batch extends Doctrine_Collection
{
2006-05-30 12:59:08 +04:00
/**
* @var integer $batchSize batch size
*/
private $batchSize;
/**
* @var array $loaded an array containing the loaded batches, keys representing the batch indexes
*/
private $loaded = array();
2006-12-29 17:01:31 +03:00
2006-12-29 17:40:47 +03:00
public function __construct(Doctrine_Table $table)
{
2006-05-30 12:59:08 +04:00
parent::__construct($table);
$this->batchSize = $this->getTable()->getAttribute(Doctrine::ATTR_BATCH_SIZE);
}
/**
* @param integer $batchSize batch size
* @return boolean
*/
2006-12-29 17:40:47 +03:00
public function setBatchSize($batchSize)
{
2006-05-30 12:59:08 +04:00
$batchSize = (int) $batchSize;
2006-12-29 17:01:31 +03:00
if ($batchSize <= 0) {
2006-05-30 12:59:08 +04:00
return false;
2006-12-29 17:01:31 +03:00
}
2006-05-30 12:59:08 +04:00
$this->batchSize = $batchSize;
return true;
}
/**
* returns the batch size of this collection
*
* @return integer
*/
2006-12-29 17:40:47 +03:00
public function getBatchSize()
{
2006-05-30 12:59:08 +04:00
return $this->batchSize;
}
/**
2006-12-29 17:01:31 +03:00
* load
2006-05-30 12:59:08 +04:00
* loads a specified element, by loading the batch the element is part of
*
* @param Doctrine_Record $record record to be loaded
* @return boolean whether or not the load operation was successful
*/
2006-12-29 17:40:47 +03:00
public function load(Doctrine_Record $record)
{
2006-12-29 17:01:31 +03:00
if (empty($this->data)) {
2006-05-30 12:59:08 +04:00
return false;
2006-12-29 17:01:31 +03:00
}
$id = $record->obtainIdentifier();
2006-05-30 12:59:08 +04:00
$identifier = $this->table->getIdentifier();
2006-12-29 17:01:31 +03:00
foreach ($this->data as $key => $v) {
if (is_object($v)) {
if ($v->obtainIdentifier() == $id) {
2006-05-30 12:59:08 +04:00
break;
2006-12-29 17:01:31 +03:00
}
} elseif (is_array($v[$identifier])) {
if ($v[$identifier] == $id) {
2006-05-30 12:59:08 +04:00
break;
2006-12-29 17:01:31 +03:00
}
2006-05-30 12:59:08 +04:00
}
}
$x = floor($key / $this->batchSize);
2006-12-29 17:01:31 +03:00
if ( ! isset($this->loaded[$x])) {
2006-05-30 12:59:08 +04:00
$e = $x * $this->batchSize;
$e2 = ($x + 1)* $this->batchSize;
$a = array();
$proxies = array();
2006-12-29 17:01:31 +03:00
for ($i = $e; $i < $e2 && $i < $this->count(); $i++) {
if ($this->data[$i] instanceof Doctrine_Record) {
$id = $this->data[$i]->getIncremented();
2006-12-29 17:01:31 +03:00
} elseif (is_array($this->data[$i])) {
2006-05-30 12:59:08 +04:00
$id = $this->data[$i][$identifier];
2006-12-29 17:01:31 +03:00
}
2006-05-30 12:59:08 +04:00
$a[$i] = $id;
2006-12-29 17:01:31 +03:00
};
2006-05-30 12:59:08 +04:00
$c = count($a);
$pk = $this->table->getPrimaryKeys();
$query = $this->table->getQuery()." WHERE ";
$query .= ($c > 1)?$identifier." IN (":$pk[0]." = ";
$query .= substr(str_repeat("?, ",count($a)),0,-2);
$query .= ($c > 1)?") ORDER BY ".$pk[0]." ASC":"";
2006-08-22 03:19:48 +04:00
$stmt = $this->table->getConnection()->execute($query,array_values($a));
2006-05-30 12:59:08 +04:00
2006-12-29 17:01:31 +03:00
foreach ($a as $k => $id) {
2006-05-30 12:59:08 +04:00
$row = $stmt->fetch(PDO::FETCH_ASSOC);
2006-12-29 17:01:31 +03:00
if ($row === false) {
2006-05-30 12:59:08 +04:00
break;
2006-12-29 17:01:31 +03:00
}
2006-05-30 12:59:08 +04:00
$this->table->setData($row);
2006-12-29 17:01:31 +03:00
if (is_object($this->data[$k])) {
2006-05-30 12:59:08 +04:00
$this->data[$k]->factoryRefresh($this->table);
} else {
$this->data[$k] = $this->table->getRecord();
}
}
$this->loaded[$x] = true;
return true;
} else {
return false;
}
}
/**
* get
* @param mixed $key the key of the record
* @return object Doctrine_Record record
*/
2006-12-29 17:40:47 +03:00
public function get($key)
{
2006-12-29 17:01:31 +03:00
if (isset($this->data[$key])) {
switch (gettype($this->data[$key])) {
case "array":
// Doctrine_Record didn't exist in cache
$this->table->setData($this->data[$key]);
$this->data[$key] = $this->table->getProxy();
2006-12-30 00:46:14 +03:00
$this->data[$key]->addCollection($this);
break;
2006-12-29 17:01:31 +03:00
};
2006-05-30 12:59:08 +04:00
} else {
$this->expand($key);
2006-12-29 17:01:31 +03:00
if ( ! isset($this->data[$key])) {
2006-05-30 12:59:08 +04:00
$this->data[$key] = $this->table->create();
2006-12-29 17:01:31 +03:00
}
2006-05-30 12:59:08 +04:00
}
2006-12-29 17:01:31 +03:00
if (isset($this->reference_field)) {
2006-09-28 01:21:33 +04:00
$this->data[$key]->set($this->reference_field, $this->reference, false);
2006-12-29 17:01:31 +03:00
}
2006-05-30 12:59:08 +04:00
return $this->data[$key];
}
/**
* @return Doctrine_Iterator
*/
2006-12-29 17:40:47 +03:00
public function getIterator()
{
return new Doctrine_Collection_Iterator_Expandable($this);
2006-05-30 12:59:08 +04:00
}
}