Answers.Body: <p>You shouldn't need to use nested array syntax, just normal strings should work.</p>
  
  <p>100% untested, but something like this:</p>
  
  <pre><code>if ($criteria-&gt;upcomingOnly) {
      $query-&gt;andWhere(['or',
              'events.eventStart &gt;= :date',
              'events.eventEnd =&gt; :date',
          ],
          [':date' =&gt; new DateTime('now')-&gt;format('Y-m-d')]
      );
  }
  </code></pre>
  
  <p>(I fixed the missing quote, so no, there is nothing else I would like to add to this post...)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-06T22:15:25.467Z
Answers.LastActivityDate: 2017-10-09T17:20:20.607Z
Answers.LastEditDate: 2017-10-09T17:20:20.607Z
Answers.UserDisplayName: Brad Bell
Body: <p>I'm trying to do an <code>andWhere()</code> "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?</p>
  
  <pre><code>if($criteria-&gt;upcomingOnly) {
      $today = new DateTime('now');
      $query-&gt;andWhere(['or',
          ['events.eventStart' =&gt; '&gt;= ' . $today-&gt;format('Y-m-d')],
          ['events.eventEnd' =&gt; '&gt;= ' . $today-&gt;format('Y-m-d')]
      ]);
  }
  </code></pre>
  
  <p>Basically the resulting query is </p>
  
  <pre><code>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='&gt;= 2017-10-06', :pZQmaPhwE8='&gt;= 2017-10-06'
  </code></pre>
  
  <p>And I would like to get something like this:</p>
  
  <pre><code>WHERE ((elements_i18n.locale = :locale) AND (elements.archived = 0)) AND ((`events`.`eventStart`&gt;=:pzek8XoNML) OR (`events`.`eventEnd`&gt;=:pZQmaPhwE8))
  GROUP BY `elements`.`id`
  ORDER BY `eventStart` ASC LIMIT 50. Bound with :locale='en_us', :pzek8XoNML='2017-10-06', :pZQmaPhwE8='2017-10-06'
  </code></pre>
  
Tags: <plugin-development><database><elementcriteriamodel><elementtype>
Title: ElementCriteriaModel andWhere() with different comparison operators?
ThreadText: # ElementCriteriaModel andWhere() with different comparison operators?
  
  (Asked by: username_0 on 2017-10-06)
  
  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'
  
  ```
  
  **Tags:** plugin-development, database, elementcriteriamodel, elementtype
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-10-06. Score: 2 | Accepted answer
  
  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...)
  
  
  ---
  
  