I'm trying to get some data with eager loading, which results in more queries than before.

Is there another way to tune down the number of queries as low as possible? As you can see i only need the posBrandKey (text) and id values.

$criteria = craft()->elements->getCriteria(ElementType::Category);
$criteria->status = null;
$criteria->limit = null;
$criteria->group = 'productBrand';
$criteria->with = array('posBrandKey');

$brands = array();
foreach ($criteria as $entry) {
    $key = $entry->posBrandKey;
    $brands[$key] = $entry->id;

My goal is to have the best performance possible, this is one of the many queries I call in the same script.

1 Answer 1


Eager loading only works when your field is a RelationField it does not work with Text fields. If you goal is to reduce the length of the queries and to maximize your performance you could fetch the raw rows instead of populating the elements.

$criteria = craft()->elements->getCriteria(ElementType::Category);
$criteria->status = null;
$criteria->limit = null;
$criteria->group = 'productBrand';

// create a query
$query = craft()->elements->buildElementsQuery($criteria);
// select only the rows you want/need
$query->select('elements.id as id, content.field_posBrandKey as posBrandKey');
$brands = [];
// execute it
$rows = $query->queryAll();

foreach($rows as $row){
    $brands[$row['posBrandKey']] = $row['id'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.