1

I'm trying to do an andWhere() "OR" group for a special criteria value so basically when building an element criteria I want to just set `$criteria->upcomingOnly to true, and then have the following code run. The problem is that the greater than or equal to is being added to the value and its doing an equals comparison on it which obviously is going to return nothing. Anyone have any idea what I need to change to get desired result?

if($criteria->upcomingOnly) {
    $today = new DateTime('now');
    $query->andWhere(['or',
        ['events.eventStart' => '>= ' . $today->format('Y-m-d')],
        ['events.eventEnd' => '>= ' . $today->format('Y-m-d')]
    ]);
}

Basically the resulting query is

WHERE ((elements_i18n.locale = :locale) AND (elements.archived = 0)) AND ((`events`.`eventStart`=:pzek8XoNML) OR (`events`.`eventEnd`=:pZQmaPhwE8))
GROUP BY `elements`.`id`
ORDER BY `eventStart` ASC LIMIT 50. Bound with :locale='en_us', :pzek8XoNML='>= 2017-10-06', :pZQmaPhwE8='>= 2017-10-06'

And I would like to get something like this:

WHERE ((elements_i18n.locale = :locale) AND (elements.archived = 0)) AND ((`events`.`eventStart`>=:pzek8XoNML) OR (`events`.`eventEnd`>=:pZQmaPhwE8))
GROUP BY `elements`.`id`
ORDER BY `eventStart` ASC LIMIT 50. Bound with :locale='en_us', :pzek8XoNML='2017-10-06', :pZQmaPhwE8='2017-10-06'

1 Answer 1

2

You shouldn't need to use nested array syntax, just normal strings should work.

100% untested, but something like this:

if ($criteria->upcomingOnly) {
    $query->andWhere(['or',
            'events.eventStart >= :date',
            'events.eventEnd => :date',
        ],
        [':date' => new DateTime('now')->format('Y-m-d')]
    );
}

(I fixed the missing quote, so no, there is nothing else I would like to add to this post...)

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.