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
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @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") {
|
2012-07-13 11:37:35 +02:00
|
|
|
Deprecation::notice('3.0', 'Use SS_Map or DataList::map() instead.', Deprecation::SCOPE_CLASS);
|
2011-10-29 03:04:17 +02:00
|
|
|
|
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);
|
2011-09-14 10:57:29 +02:00
|
|
|
$where = "$baseTable.\"ID\" = $id";
|
|
|
|
$this->query->where[sha1($where)] = $where;
|
2007-07-19 12:40:28 +02:00
|
|
|
$record = $this->query->execute()->first();
|
2011-09-14 10:57:29 +02:00
|
|
|
unset($this->query->where[sha1($where)]);
|
2007-07-19 12:40:28 +02:00
|
|
|
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();
|
2011-10-29 03:04:17 +02:00
|
|
|
return new SS_Map_Iterator($this->items->getIterator(), $this->keyField, $this->titleField);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the items in this class.
|
2011-10-26 08:09:04 +02:00
|
|
|
* @return SS_List
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
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)) {
|
2011-05-05 12:40:24 +02:00
|
|
|
$this->items = new ArrayList();
|
2007-07-19 12:40:28 +02:00
|
|
|
$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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|