1
0
mirror of synced 2025-01-19 06:51:40 +03:00
doctrine2/classes/Collection/Batch.class.php

178 lines
5.4 KiB
PHP
Raw Normal View History

2006-04-13 20:37:28 +00:00
<?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Collection.class.php");
/**
* Doctrine_Collection_Batch a collection of data access objects,
* with batch load strategy
*
*
* @author Konsta Vesterinen
* @package Doctrine ORM
* @url www.phpdoctrine.com
* @license LGPL
* @version 1.0 alpha
*/
class Doctrine_Collection_Batch extends Doctrine_Collection {
/**
* @var integer $batchSize batch size
*/
private $batchSize;
/**
* @var array $loaded an array containing the loaded batches, keys representing the batch indexes
*/
private $loaded = array();
public function __construct(Doctrine_DQL_Parser $graph,$key) {
parent::__construct($graph->getTable($key));
$this->data = $graph->getData($key);
if( ! is_array($this->data))
$this->data = array();
$this->batchSize = $this->getTable()->getAttribute(Doctrine::ATTR_BATCH_SIZE);
}
/**
* @param integer $batchSize batch size
*/
public function setBatchSize($batchSize) {
$batchSize = (int) $batchSize;
if($batchSize <= 0)
return false;
$this->batchSize = $batchSize;
return true;
}
/**
* @return integer
*/
public function getBatchSize() {
return $this->batchSize;
}
/**
* load load a specified element, by loading the batch the element is part of
* @param Doctrine_Record $record data access object
* @return boolean whether or not the load operation was successful
*/
public function load(Doctrine_Record $record) {
if(empty($this->data))
return false;
$id = $record->getID();
foreach($this->data as $key => $v) {
if(is_object($v)) {
if($v->getID() == $id)
break;
} elseif(is_array($v["id"])) {
if($v["id"] == $id)
break;
}
}
$x = floor($key / $this->batchSize);
if( ! isset($this->loaded[$x])) {
$e = $x * $this->batchSize;
$e2 = ($x + 1)* $this->batchSize;
$a = array();
$proxies = array();
for($i = $e; $i < $e2 && $i < $this->count(); $i++):
if(is_object($this->data[$i]))
$id = $this->data[$i]->getID();
elseif(is_array($this->data[$i]))
$id = $this->data[$i]["id"];
$load = false;
// check the cache
// no need of fetching the same data twice
try {
$record = $this->table->getCache()->fetch($id);
} catch(InvalidKeyException $ex) {
$load = true;
}
if($load)
$a[] = $id;
endfor;
$c = count($a);
$query = $this->table->getQuery()." WHERE ";
$query .= ($c > 1)?"id IN (":"id = ";
$query .= substr(str_repeat("?, ",count($a)),0,-2);
$query .= ($c > 1)?")":"";
$stmt = $this->table->getSession()->execute($query,$a);
2006-04-14 10:20:19 +00:00
2006-04-13 20:37:28 +00:00
while($row = $stmt->fetch(PDO::FETCH_ASSOC)):
2006-04-14 10:20:19 +00:00
2006-04-13 20:37:28 +00:00
$this->table->setData($row);
if(is_object($this->data[$e])) {
$this->data[$e]->factoryRefresh($this->table);
} else {
$this->data[$e] = $this->table->getRecord();
}
$e++;
endwhile;
2006-04-14 10:20:19 +00:00
2006-04-13 20:37:28 +00:00
$this->loaded[$x] = true;
return true;
} else {
return false;
}
}
/**
* get
2006-04-14 10:20:19 +00:00
* @param mixed $key the key of the record
* @return object Doctrine_Record record
2006-04-13 20:37:28 +00:00
*/
public function get($key) {
if(isset($this->data[$key])) {
switch(gettype($this->data[$key])):
case "array":
try {
// try to fetch the Doctrine_Record from cache
if( ! isset($this->data[$key]["id"]))
throw new InvalidKeyException();
2006-04-14 10:20:19 +00:00
$this->data[$key] = $this->table->getCache()->fetch($this->data[$key]["id"]);
2006-04-13 20:37:28 +00:00
} catch(InvalidKeyException $e) {
// Doctrine_Record didn't exist in cache
$this->table->setData($this->data[$key]);
2006-04-14 10:20:19 +00:00
$this->data[$key] = $this->table->getProxy();
2006-04-13 20:37:28 +00:00
}
2006-04-14 10:20:19 +00:00
$this->data[$key]->addCollection($this);
2006-04-13 20:37:28 +00:00
break;
endswitch;
} else {
$this->expand();
2006-04-14 10:20:19 +00:00
if( ! isset($this->data[$key]))
$this->data[$key] = $this->table->create();
2006-04-13 20:37:28 +00:00
}
if(isset($this->reference_field))
2006-04-14 10:20:19 +00:00
$this->data[$key]->set($this->reference_field,$this->reference);
2006-04-13 20:37:28 +00:00
return $this->data[$key];
}
/**
* @return Doctrine_BatchIterator
*/
public function getIterator() {
return new Doctrine_BatchIterator($this);
}
}
?>