2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This is a class used to represent key->value pairs generated from database queries.
|
|
|
|
* The query isn't actually executed until you need it.
|
2010-04-23 02:11:41 +02:00
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage model
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2008-03-11 02:31:43 +01:00
|
|
|
class SQLMap extends Object implements IteratorAggregate {
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* The query used to generate the map.
|
|
|
|
* @var SQLQuery
|
|
|
|
*/
|
|
|
|
protected $query;
|
2009-01-05 07:19:48 +01:00
|
|
|
protected $keyField, $titleField;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a SQLMap.
|
|
|
|
* @param SQLQuery $query The query to generate this map. THis isn't executed until it's needed.
|
|
|
|
*/
|
2009-01-05 07:19:48 +01:00
|
|
|
public function __construct(SQLQuery $query, $keyField = "ID", $titleField = "Title") {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$query) {
|
|
|
|
user_error('SQLMap constructed with null query.', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->query = $query;
|
2009-01-05 07:19:48 +01:00
|
|
|
$this->keyField = $keyField;
|
|
|
|
$this->titleField = $titleField;
|
|
|
|
|
|
|
|
parent::__construct();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of an item.
|
|
|
|
* @param string|int $id The id of the item.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getItem($id) {
|
|
|
|
if($id) {
|
|
|
|
$baseTable = reset($this->query->from);
|
2010-10-19 03:25:32 +02:00
|
|
|
$this->query->where[] = "$baseTable.\"ID\" = $id";
|
2007-07-19 12:40:28 +02:00
|
|
|
$record = $this->query->execute()->first();
|
|
|
|
if($record) {
|
|
|
|
$className = $record['ClassName'];
|
|
|
|
$obj = new $className($record);
|
|
|
|
return $obj->Title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-11 02:31:43 +01:00
|
|
|
public function getIterator() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->genItems();
|
2009-01-05 07:19:48 +01:00
|
|
|
return new SQLMap_Iterator($this->items->getIterator(), $this->keyField, $this->titleField);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the items in this class.
|
|
|
|
* @return DataObjectSet
|
|
|
|
*/
|
|
|
|
public function getItems() {
|
|
|
|
$this->genItems();
|
|
|
|
return $this->items;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the items in this map. This is used by
|
|
|
|
* getItems() if the items have not been generated already.
|
|
|
|
*/
|
|
|
|
protected function genItems() {
|
|
|
|
if(!isset($this->items)) {
|
|
|
|
$this->items = new DataObjectSet();
|
|
|
|
$items = $this->query->execute();
|
|
|
|
|
|
|
|
foreach($items as $item) {
|
|
|
|
$className = isset($item['RecordClassName']) ? $item['RecordClassName'] : $item['ClassName'];
|
2007-08-07 03:20:20 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!$className) {
|
|
|
|
user_error('SQLMap query could not retrieve className', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->items->push(new $className($item));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-23 02:11:41 +02:00
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage model
|
|
|
|
*/
|
2008-04-26 08:44:06 +02:00
|
|
|
class SQLMap_Iterator extends Object implements Iterator {
|
|
|
|
protected $items;
|
2009-01-05 07:19:48 +01:00
|
|
|
protected $keyField, $titleField;
|
2008-04-26 08:44:06 +02:00
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
function __construct(Iterator $items, $keyField, $titleField) {
|
2008-04-26 08:44:06 +02:00
|
|
|
$this->items = $items;
|
2009-01-05 07:19:48 +01:00
|
|
|
$this->keyField = $keyField;
|
|
|
|
$this->titleField = $titleField;
|
2008-04-26 08:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Iterator functions - necessary for foreach to work
|
|
|
|
*/
|
|
|
|
public function rewind() {
|
2009-01-05 07:19:48 +01:00
|
|
|
return $this->items->rewind() ? $this->items->rewind()->{$this->titleField} : null;
|
2008-04-26 08:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function current() {
|
2009-01-05 07:19:48 +01:00
|
|
|
return $this->items->current()->{$this->titleField};
|
2008-04-26 08:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function key() {
|
2009-01-05 07:19:48 +01:00
|
|
|
return $this->items->current()->{$this->keyField};
|
2008-04-26 08:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function next() {
|
|
|
|
$next = $this->items->next();
|
2009-01-05 07:19:48 +01:00
|
|
|
return isset($next->{$this->titleField}) ? $next->{$this->titleField} : null;
|
2008-04-26 08:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function valid() {
|
|
|
|
return $this->items->valid();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|