1
0
mirror of synced 2025-01-18 22:41:43 +03:00
doctrine2/classes/Collection/Batch.class.php

164 lines
4.8 KiB
PHP
Raw Normal View History

2006-04-13 20:37:28 +00:00
<?php
require_once(Doctrine::getPath().DIRECTORY_SEPARATOR."Collection.class.php");
/**
2006-04-19 16:01:36 +00:00
* Doctrine_Collection_Batch a collection of records,
* with batch load strategy
2006-04-13 20:37:28 +00:00
*
*
* @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();
2006-04-19 16:01:36 +00:00
public function __construct(Doctrine_Table $table) {
parent::__construct($table);
2006-04-13 20:37:28 +00:00
$this->batchSize = $this->getTable()->getAttribute(Doctrine::ATTR_BATCH_SIZE);
}
/**
* @param integer $batchSize batch size
* @return boolean
2006-04-13 20:37:28 +00:00
*/
public function setBatchSize($batchSize) {
$batchSize = (int) $batchSize;
if($batchSize <= 0)
return false;
$this->batchSize = $batchSize;
return true;
}
/**
* returns the batch size of this collection
*
2006-04-13 20:37:28 +00:00
* @return integer
*/
public function getBatchSize() {
return $this->batchSize;
}
/**
* load
* 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-04-13 20:37:28 +00:00
*/
public function load(Doctrine_Record $record) {
if(empty($this->data))
return false;
$id = $record->getID();
2006-04-23 08:12:01 +00:00
$identifier = $this->table->getIdentifier();
2006-04-13 20:37:28 +00:00
foreach($this->data as $key => $v) {
if(is_object($v)) {
if($v->getID() == $id)
break;
2006-04-23 08:12:01 +00:00
} elseif(is_array($v[$identifier])) {
if($v[$identifier] == $id)
2006-04-13 20:37:28 +00:00
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($this->data[$i] instanceof Doctrine_Record)
2006-04-13 20:37:28 +00:00
$id = $this->data[$i]->getID();
elseif(is_array($this->data[$i]))
2006-04-23 08:12:01 +00:00
$id = $this->data[$i][$identifier];
2006-04-13 20:37:28 +00:00
2006-04-15 10:15:16 +00:00
$a[$i] = $id;
2006-04-13 20:37:28 +00:00
endfor;
$c = count($a);
2006-04-16 22:27:01 +00:00
$pk = $this->table->getPrimaryKeys();
2006-04-13 20:37:28 +00:00
$query = $this->table->getQuery()." WHERE ";
2006-04-23 08:12:01 +00:00
$query .= ($c > 1)?$identifier." IN (":$pk[0]." = ";
2006-04-13 20:37:28 +00:00
$query .= substr(str_repeat("?, ",count($a)),0,-2);
2006-04-16 22:27:01 +00:00
$query .= ($c > 1)?") ORDER BY ".$pk[0]." ASC":"";
2006-04-15 10:15:16 +00:00
$stmt = $this->table->getSession()->execute($query,array_values($a));
2006-04-13 20:37:28 +00:00
2006-04-16 22:17:23 +00:00
foreach($a as $k => $id) {
2006-04-15 10:15:16 +00:00
$row = $stmt->fetch(PDO::FETCH_ASSOC);
2006-04-16 22:17:23 +00:00
2006-04-15 10:15:16 +00:00
if($row === false)
break;
2006-04-14 10:20:19 +00:00
2006-04-13 20:37:28 +00:00
$this->table->setData($row);
2006-04-15 10:15:16 +00:00
if(is_object($this->data[$k])) {
$this->data[$k]->factoryRefresh($this->table);
2006-04-13 20:37:28 +00:00
} else {
2006-04-15 10:15:16 +00:00
$this->data[$k] = $this->table->getRecord();
2006-04-13 20:37:28 +00:00
}
2006-04-15 10:15:16 +00:00
}
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;
}
}
2006-04-13 20:37:28 +00:00
/**
* 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":
2006-04-15 10:15:16 +00:00
// Doctrine_Record didn't exist in cache
$this->table->setData($this->data[$key]);
$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 {
2006-04-19 16:01:36 +00:00
$this->expand($key);
2006-04-13 20:37:28 +00:00
2006-04-14 10:20:19 +00:00
if( ! isset($this->data[$key]))
$this->data[$key] = $this->table->create();
2006-04-16 20:38:17 +00:00
2006-04-13 20:37:28 +00:00
}
if(isset($this->reference_field))
2006-04-27 20:24:59 +00:00
$this->data[$key]->rawSet($this->reference_field,$this->reference);
2006-04-14 10:20:19 +00:00
2006-04-13 20:37:28 +00:00
return $this->data[$key];
}
/**
2006-04-19 16:01:36 +00:00
* @return Doctrine_Iterator
2006-04-13 20:37:28 +00:00
*/
public function getIterator() {
2006-04-19 16:01:36 +00:00
return new Doctrine_Iterator_Expandable($this);
2006-04-13 20:37:28 +00:00
}
}
?>