mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
d8e9af8af8
Database abstraction broken up into controller, connector, query builder, and schema manager, each independently configurable via YAML / Injector Creation of new DBQueryGenerator for database specific generation of SQL Support for parameterised queries, move of code base to use these over escaped conditions Refactor of SQLQuery into separate query classes for each of INSERT UPDATE DELETE and SELECT Support for PDO Installation process upgraded to use new ORM SS_DatabaseException created to handle database errors, maintaining details of raw sql and parameter details for user code designed interested in that data. Renamed DB static methods to conform correctly to naming conventions (e.g. DB::getConn -> DB::get_conn) 3.2 upgrade docs Performance Optimisation and simplification of code to use more concise API API Ability for database adapters to register extensions to ConfigureFromEnv.php
93 lines
2.3 KiB
PHP
93 lines
2.3 KiB
PHP
<?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.
|
|
*
|
|
* @package framework
|
|
* @subpackage model
|
|
*/
|
|
class SQLMap extends Object implements IteratorAggregate {
|
|
/**
|
|
* The query used to generate the map.
|
|
* @var SQLSelect
|
|
*/
|
|
protected $query;
|
|
protected $keyField, $titleField;
|
|
|
|
/**
|
|
* Construct a SQLMap.
|
|
*
|
|
* @param SQLSelect $query The query to generate this map. THis isn't executed until it's needed.
|
|
*/
|
|
public function __construct(SQLSelect $query, $keyField = "ID", $titleField = "Title") {
|
|
Deprecation::notice('3.0', 'Use SS_Map or DataList::map() instead.', Deprecation::SCOPE_CLASS);
|
|
|
|
if(!$query) {
|
|
user_error('SQLMap constructed with null query.', E_USER_ERROR);
|
|
}
|
|
|
|
$this->query = $query;
|
|
$this->keyField = $keyField;
|
|
$this->titleField = $titleField;
|
|
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
$oldWhere = $this->query->getWhere();
|
|
$this->query->where(array(
|
|
"\"$baseTable\".\"ID\" = ?" => $id
|
|
));
|
|
$record = $this->query->execute()->first();
|
|
$this->query->setWhere($oldWhere);
|
|
if($record) {
|
|
$className = $record['ClassName'];
|
|
$obj = new $className($record);
|
|
return $obj->Title;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function getIterator() {
|
|
$this->genItems();
|
|
return new SS_Map_Iterator($this->items->getIterator(), $this->keyField, $this->titleField);
|
|
}
|
|
|
|
/**
|
|
* Get the items in this class.
|
|
* @return SS_List
|
|
*/
|
|
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 ArrayList();
|
|
$items = $this->query->execute();
|
|
|
|
foreach($items as $item) {
|
|
$className = isset($item['RecordClassName']) ? $item['RecordClassName'] : $item['ClassName'];
|
|
|
|
if(!$className) {
|
|
user_error('SQLMap query could not retrieve className', E_USER_ERROR);
|
|
}
|
|
|
|
$this->items->push(new $className($item));
|
|
}
|
|
}
|
|
}
|
|
}
|