2

Can anyone help me work out why the following returns the error "The merge filter only works with arrays or hashes."?

{# CREATE ARRAY TO HOLD ALL OF TODAY'S EVENTS #}
{% set todaysEvents = [] %}

{# GET TODAY'S ONE-OFF EVENTS #}
{# RETURNS ENTRIES NICELY IF LOOPED THROUGH #}
{% set todaysOneOffEvents = craft.entries.section('events').eventDate('= '~today | date("Y-m-d")) %}

{# ADD TODAY'S ONE-OFF EVENTS TO TODAY'S EVENTS %}
{# THIS RETURNS MERGE ARRAY ERROR #}
{% set todaysEvents = todaysEvents|merge(todaysOneOffEvents) %}
  • Thanks to this I finally learned the difference between an ElementCriteriaModel an an array of entries. The magic happens within the find() function :) Thanks for the question, Clive! – carlcs Oct 21 '14 at 15:11
6

Try adding find() to your ElementCriteriaModel, I think without that function you're not actually saving your entries as an array in your variable todaysOneOffEvents yet.

Here's what I found in the Docs:

You don’t actually need to call find() yourself though: It will be called automatically as soon as the ElementCriteriaModel is treated like an array (that is, as soon as you check how many elements there are using the |length filter, or start looping through the elements with a {% for %} loop).

We learn that merging doesn't "treat it like an array", good to know.

Edit: You can't use the length filter to "convert" the content of the variable to an actual array. Removed that part from the answer.

  • That's the bunny. Cheers. – Clive Portman Oct 22 '14 at 14:00

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.