Answers.Body: <p>You can apply conditions within the same field but you can't create conditions for multiple fields.</p>
  
  <p><a href="https://craftcms.stackexchange.com/questions/8992/how-to-show-entries-which-are-live-between-dates-regardless-of-postdate-or-expir">Here</a> are some great examples</p>
  
  <pre><code>$criteria-&gt;expiryDate = 'or, :empty:, =' . $yourDateValue;
  </code></pre>
  
  <p>However you can't combine conditions for multiple fields. So you can't say <code>Where (fieldX = 4 OR FieldY = 5)</code> you would have to create an sql object based on your <code>ElementCriteriaModel</code> and use Yii's great <a href="http://www.yiiframework.com/doc/guide/1.1/de/database.query-builder" rel="nofollow noreferrer">Query Builder</a> for that and use the <a href="http://www.yiiframework.com/doc/api/1.1/CDbCommand#orWhere-detail" rel="nofollow noreferrer">OrWhere</a> function</p>
  
  <pre><code>$query = craft()-&gt;elements-&gt;buildElementsQuery($criteria);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-22T14:00:31.817Z
Answers.Text: Thanks a lot! One more non-obvious feature discovered for me :) Can you advice some good doc which covers all such cases? Official documentation is way too poor.
Answers.UserDisplayName: Stalinko
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-22T14:11:20.560Z
Answers.Text: Craft has a really awesome documentation. Nearly every aspect is really well documented with many use cases but like it's already stated on their website Craft was made for developers, so most of the time you'll work with PHPDocs. If you have a good IDE and a little bit knowledge how to use it, you'll receive all information you need in no time. If you prefer to use notepad it's going to be a mess... Have a look here https://github.com/craftcms/cms/blob/master-v2/src/helpers/DbHelper.php#L426 That's the function that converts the strings you insert in your attribute into a db query
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-22T14:16:56.160Z
Answers.Text: While other CMS have a better website with more hints - their PHP doc is a real mess with no abilities for autocomplete or typesetting. A few people prefer it that way since they don't use PHP that much but I think it's definitely much more important to have a good PHP doc for a well designed auto suggestion and code completion, having all necessary hints in my IDE is way faster for me than having to search in the internet
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-22T14:37:48.420Z
Answers.Text: Thanks, Robin. But "awesome documentation" isn't about Craft. When I say "good docs" I mean some manual so that I can read it completely and won't have to ask stupid questions like this anymore. With craft such basic things like "how to create an element with relations programmatically" or "how to make complex query" turn into a real quest or a big investigation. I learned the code for an hour before I asked this question, and IDE with comments didn't help. I worked with many FWs before but never had such difficulties like with craft :(
Answers.UserDisplayName: Stalinko
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-22T14:43:52.687Z
Answers.Text: Well if you are searching for a manual you are a little bit lost because there is non :P I learned everything by looking up the source code and I suggest you to do the same. Most questions can be answered by just looking how they did it in nearly no time... It's much faster. I used many CMS and Frameworks before and none of them were so easy to use like Craft because they had no good doc (I'm looking at you wordpress). Seems like it's all opinion based.
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-22T10:00:55.853Z
Answers.LastActivityDate: 2018-02-22T10:07:33.307Z
Answers.LastEditDate: 2018-02-22T10:07:33.307Z
Answers.UserDisplayName: Robin Schambach
Body: <p>How to fetch Elements with a complex DB query?
  E.g. with some nested AND/OR conditions or just apply two conditions to one field?</p>
  
  <p>For example my Elements have a field <code>eventDate</code>. I want to fetch all Elements where <code>eventDate</code> is <code>NULL</code> or equals "1900-01-01". </p>
  
  <p>The standard way to build Element Criteria is:</p>
  
  <pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
  $criteria-&gt;eventDate = '=...';
  $models = $criteria-&gt;find();
  </code></pre>
  
  <p>It doesn't provide an ability to apply several conditions for same field. Is it possible at all without assembling huge queries manually?</p>
  
Tags: <elementcriteriamodel><query><element>
Title: How to build complex Element Criteria?
ThreadText: # How to build complex Element Criteria?
  
  (Asked by: username_0 on 2018-02-22)
  
  How to fetch Elements with a complex DB query?
  E.g. with some nested AND/OR conditions or just apply two conditions to one field?
  
  For example my Elements have a field `eventDate`. I want to fetch all Elements where `eventDate` is `NULL` or equals "1900-01-01".
  
  The standard way to build Element Criteria is:
  
  ```
  $criteria = craft()->elements->getCriteria(ElementType::Entry);
  $criteria->eventDate = '=...';
  $models = $criteria->find();
  
  ```
  
  It doesn't provide an ability to apply several conditions for same field. Is it possible at all without assembling huge queries manually?
  
  **Tags:** elementcriteriamodel, query, element
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-02-22. Score: 1 | Accepted answer
  
  You can apply conditions within the same field but you can't create conditions for multiple fields.
  
  [Here](https://craftcms.stackexchange.com/questions/8992/how-to-show-entries-which-are-live-between-dates-regardless-of-postdate-or-expir) are some great examples
  
  ```
  $criteria->expiryDate = 'or, :empty:, =' . $yourDateValue;
  
  ```
  
  However you can't combine conditions for multiple fields. So you can't say `Where (fieldX = 4 OR FieldY = 5)` you would have to create an sql object based on your `ElementCriteriaModel` and use Yii's great [Query Builder](http://www.yiiframework.com/doc/guide/1.1/de/database.query-builder) for that and use the [OrWhere](http://www.yiiframework.com/doc/api/1.1/CDbCommand#orWhere-detail) function
  
  ```
  $query = craft()->elements->buildElementsQuery($criteria);
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks a lot! One more non-obvious feature discovered for me :) Can you advice some good doc which covers all such cases? Official documentation is way too poor.  
  > **username_1**: Craft has a really awesome documentation. Nearly every aspect is really well documented with many use cases but like it's already stated on their website Craft was made for developers, so most of the time you'll work with PHPDocs. If you have a good IDE and a little bit knowledge how to use it, you'll receive all information you need in no time. If you prefer to use notepad it's going to be a mess... Have a look here https://github.com/craftcms/cms/blob/master-v2/src/helpers/DbHelper.php#L426 That's the function that converts the strings you insert in your attribute into a db query  
  > **username_1**: While other CMS have a better website with more hints - their PHP doc is a real mess with no abilities for autocomplete or typesetting. A few people prefer it that way since they don't use PHP that much but I think it's definitely much more important to have a good PHP doc for a well designed auto suggestion and code completion, having all necessary hints in my IDE is way faster for me than having to search in the internet  
  > **username_0**: Thanks, username_1. But "awesome documentation" isn't about Craft. When I say "good docs" I mean some manual so that I can read it completely and won't have to ask stupid questions like this anymore. With craft such basic things like "how to create an element with relations programmatically" or "how to make complex query" turn into a real quest or a big investigation. I learned the code for an hour before I asked this question, and IDE with comments didn't help. I worked with many FWs before but never had such difficulties like with craft :(  
  > **username_1**: Well if you are searching for a manual you are a little bit lost because there is non :P I learned everything by looking up the source code and I suggest you to do the same. Most questions can be answered by just looking how they did it in nearly no time... It's much faster. I used many CMS and Frameworks before and none of them were so easy to use like Craft because they had no good doc (I'm looking at you wordpress). Seems like it's all opinion based.  
  
  ---
  
  