Skip to content

Commit 1a1a14e

Browse files
committed
Optimize valueToWidget
As this is called multiple times by dc-general, we can cache the result. We also only obtain the required value columns.
1 parent 37569e0 commit 1a1a14e

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/MetaModels/Attribute/Select/MetaModelSelect.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,11 @@ protected function itemsToValues(IItems $items)
126126
* Retrieve the values with the given ids.
127127
*
128128
* @param string[] $valueIds The ids of the values to retrieve.
129+
* @param array $attrOnly The attribute names to obtain.
129130
*
130131
* @return array
131132
*/
132-
protected function getValuesById($valueIds)
133+
protected function getValuesById($valueIds, $attrOnly = [])
133134
{
134135
$recursionKey = $this->getMetaModel()->getTableName();
135136

@@ -142,7 +143,7 @@ protected function getValuesById($valueIds)
142143

143144
$metaModel = $this->getSelectMetaModel();
144145
$filter = $metaModel->getEmptyFilter()->addFilterRule(new StaticIdList($valueIds));
145-
$items = $metaModel->findByFilter($filter, 'id');
146+
$items = $metaModel->findByFilter($filter, 'id', 0, 0, 'ASC', $attrOnly);
146147
unset($tables[$recursionKey]);
147148

148149
return $this->itemsToValues($items);
@@ -172,6 +173,11 @@ public function valueToWidget($varValue)
172173
*/
173174
public function widgetToValue($varValue, $itemId)
174175
{
176+
static $cache;
177+
if (isset($cache[$this->get('id')][$varValue])) {
178+
return $cache[$this->get('id')][$varValue];
179+
}
180+
175181
$model = $this->getSelectMetaModel();
176182
$alias = $this->getIdColumn();
177183
$attribute = $model->getAttribute($alias);
@@ -220,9 +226,12 @@ public function widgetToValue($varValue, $itemId)
220226
}
221227
}
222228

223-
$value = $this->getValuesById(array($valueId));
229+
$value = $this->getValuesById(
230+
[$valueId],
231+
[$this->getAliasColumn(), $this->getValueColumn(), $this->getIdColumn(), $this->getSortingColumn()]
232+
);
224233

225-
return $value[$valueId];
234+
return $cache[$this->get('id')][$varValue] = $value[$valueId];
226235
}
227236

228237
/**

0 commit comments

Comments
 (0)