Merge pull request #4108 from Turnerj/fix/many-many-extrafields-performance

Fix/Performance: Combine multiple queries into one for ManyManyList::getExtraData
This commit is contained in:
Damian Mooyman 2015-05-29 10:26:56 +12:00
commit 71d766a8d8

View File

@ -362,17 +362,21 @@ class ManyManyList extends RelationList {
user_error('ComponentSet::getExtraData() passed a non-numeric child ID', E_USER_ERROR); user_error('ComponentSet::getExtraData() passed a non-numeric child ID', E_USER_ERROR);
} }
// @todo Optimize into a single query instead of one per extra field
if($this->extraFields) { if($this->extraFields) {
foreach($this->extraFields as $fieldName => $dbFieldSpec) { $cleanExtraFields = array();
$query = new SQLQuery("\"{$fieldName}\"", "\"{$this->joinTable}\""); foreach ($this->extraFields as $fieldName => $dbFieldSpec) {
$cleanExtraFields[] = "\"{$fieldName}\"";
}
$query = new SQLQuery($cleanExtraFields, "\"{$this->joinTable}\"");
if($filter = $this->foreignIDWriteFilter($this->getForeignID())) { if($filter = $this->foreignIDWriteFilter($this->getForeignID())) {
$query->setWhere($filter); $query->setWhere($filter);
} else { } else {
user_error("Can't call ManyManyList::getExtraData() until a foreign ID is set", E_USER_WARNING); user_error("Can't call ManyManyList::getExtraData() until a foreign ID is set", E_USER_WARNING);
} }
$query->addWhere("\"{$this->localKey}\" = {$itemID}"); $query->addWhere("\"{$this->localKey}\" = {$itemID}");
$result[$fieldName] = $query->execute()->value(); $queryResult = $query->execute()->current();
foreach ($queryResult as $fieldName => $value) {
$result[$fieldName] = $value;
} }
} }