1

I apologize if this has been asked or addressed already, but I have a question concerning Element Queries. The docs show two possible ways:

Chained Methods

{% set query = craft.entries()
    .section('news')
    .limit(10) %}

Batch-set Parameters

{% set query = craft.entries({
    section: 'news',
    limit: 10
}) %}

The examples shown above (from the docs) obviously accomplish the exact same thing--but what is the difference, if any?

It is my assumption that the batch-set way of querying elements allows for more options, is that true? And does either way have an effect on performance?

3

general

The first way creates an object of type EntryQuery, you then call the 2 functions section and limit

In PHP you would do the following:

$query = Entry::find()->section('news')->limit(10);

The second creates an array section => 'news', 'limit' => 10 you then call Yii2::configure($query, $array) with your EntryQuery which does:

public static function configure($object, $properties)
{
    foreach ($properties as $name => $value) {
        $object->$name = $value;
    }

    return $object;
}

Thus you don't call functions but define the properties directly. In PHP it would look like

$query = Entry::find();
Craft::configure($query, ['section' => 'news', 'limit' => 10]);

Performance

Because of the above it's important what you do. The first way calls functions the second way calls properties.

As for your comparison the first way is a little bit faster because you didn't attach functions that requires the use of magic methods => __call (in the ElementQueryBehavior) as soon as you search/filter for custom fields the Craft::configure way will always be a little bit faster because it can set the values directly instead of using the resource heavy __call function

For example:

// faster
$query = Entry::find()
    ->section('news')
    ->limit(10)

// slower
$query = Entry::find();
Craft::configure($query, [
    'section' => 'news', 
    'limit' => 10
]);

Against

// slower
$query = Entry::find()
    ->section('news')
    ->limit(10)
    ->multiselect('test')
    ->subline('foo');

// faster
$query = Entry::find();
Craft::configure($query, [
    'section' => 'news',
    'limit' => 10,
    'multiselect' => 'test',
    'subline' => 'foo'
]);

Conclusion

Keep in mind when I say "slower" or "faster" I'm talking about a few milliseconds (when I run the last example 1000 times its 511ms vs 497ms) so in the end it does not really matter. If it's really important in terms of speed: as soon as you use the __call method because you need to change properties in the ElementQueryBehavior you would prefer to use Craft::configure.

Besides that: there is absolutely no difference between those two.. There are no hidden "benefits" you don't have more options, you can't do something "better"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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