mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
ENHANCEMENT: Update ForeignKey and Primary key default fields to use SQLMap for their dropdown source for better performance.
API CHANGE: Add keyField and titleField arguments to SQLMap BUGFIX: Get DrodpownField::$emptyString working when used with a SQLMap source git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@69360 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
c0fc790e32
commit
1a9180abaa
@ -11,17 +11,22 @@ class SQLMap extends Object implements IteratorAggregate {
|
||||
* @var SQLQuery
|
||||
*/
|
||||
protected $query;
|
||||
protected $keyField, $titleField;
|
||||
|
||||
/**
|
||||
* Construct a SQLMap.
|
||||
* @param SQLQuery $query The query to generate this map. THis isn't executed until it's needed.
|
||||
*/
|
||||
public function __construct(SQLQuery $query) {
|
||||
public function __construct(SQLQuery $query, $keyField = "ID", $titleField = "Title") {
|
||||
if(!$query) {
|
||||
user_error('SQLMap constructed with null query.', E_USER_ERROR);
|
||||
}
|
||||
|
||||
$this->query = $query;
|
||||
$this->keyField = $keyField;
|
||||
$this->titleField = $titleField;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -44,7 +49,7 @@ class SQLMap extends Object implements IteratorAggregate {
|
||||
|
||||
public function getIterator() {
|
||||
$this->genItems();
|
||||
return new SQLMap_Iterator($this->items->getIterator());
|
||||
return new SQLMap_Iterator($this->items->getIterator(), $this->keyField, $this->titleField);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -80,9 +85,12 @@ class SQLMap extends Object implements IteratorAggregate {
|
||||
|
||||
class SQLMap_Iterator extends Object implements Iterator {
|
||||
protected $items;
|
||||
protected $keyField, $titleField;
|
||||
|
||||
function __construct(Iterator $items) {
|
||||
function __construct(Iterator $items, $keyField, $titleField) {
|
||||
$this->items = $items;
|
||||
$this->keyField = $keyField;
|
||||
$this->titleField = $titleField;
|
||||
}
|
||||
|
||||
|
||||
@ -90,20 +98,20 @@ class SQLMap_Iterator extends Object implements Iterator {
|
||||
* Iterator functions - necessary for foreach to work
|
||||
*/
|
||||
public function rewind() {
|
||||
return $this->items->rewind() ? $this->items->rewind()->Title : null;
|
||||
return $this->items->rewind() ? $this->items->rewind()->{$this->titleField} : null;
|
||||
}
|
||||
|
||||
public function current() {
|
||||
return $this->items->current()->Title;
|
||||
return $this->items->current()->{$this->titleField};
|
||||
}
|
||||
|
||||
public function key() {
|
||||
return $this->items->current()->ID;
|
||||
return $this->items->current()->{$this->keyField};
|
||||
}
|
||||
|
||||
public function next() {
|
||||
$next = $this->items->next();
|
||||
return isset($next->Title) ? $next->Title : null;
|
||||
return isset($next->{$this->titleField}) ? $next->{$this->titleField} : null;
|
||||
}
|
||||
|
||||
public function valid() {
|
||||
|
@ -43,9 +43,8 @@ class ForeignKey extends Int {
|
||||
$field = new FileField($relationName, $title, $this->value);
|
||||
}
|
||||
} else {
|
||||
$objs = DataObject::get($hasOneClass);
|
||||
$titleField = (singleton($hasOneClass)->hasField('Title')) ? "Title" : "Name";
|
||||
$map = ($objs) ? $objs->toDropdownMap("ID", $titleField) : false;
|
||||
$map = new SQLMap(singleton($hasOneClass)->extendedSQL(), "ID", $titleField);
|
||||
$field = new DropdownField($this->name, $title, $map, null, null, ' ');
|
||||
}
|
||||
|
||||
|
@ -22,12 +22,8 @@ class PrimaryKey extends Int {
|
||||
}
|
||||
|
||||
public function scaffoldFormField($title = null, $params = null) {
|
||||
$objs = DataObject::get($this->object->class);
|
||||
|
||||
$titleField = (singleton($this->object->class)->hasField('Title')) ? "Title" : "Name";
|
||||
|
||||
$map = ($objs) ? $objs->toDropdownMap("ID", $titleField) : false;
|
||||
|
||||
$titleField = ($this->object->hasField('Title')) ? "Title" : "Name";
|
||||
$map = new SQLMap($this->object->extendedSQL(), "ID", $titleField);
|
||||
return new DropdownField($this->name, $title, $map, null, null, ' ');
|
||||
}
|
||||
}
|
||||
|
@ -71,6 +71,11 @@ class DropdownField extends FormField {
|
||||
|
||||
$source = $this->getSource();
|
||||
if($source) {
|
||||
// For SQLMap sources, the empty string needs to be added specially
|
||||
if(is_object($source) && $this->emptyString) {
|
||||
$options .= $this->createTag('option', array('value' => ''), $this->emptyString);
|
||||
}
|
||||
|
||||
foreach($source as $value => $title) {
|
||||
$selected = ($value == $this->value) ? 'selected' : null;
|
||||
if($selected && $this->value != 0) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user