1

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?

1

You can apply conditions within the same field but you can't create conditions for multiple fields.

Here 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 for that and use the OrWhere function

$query = craft()->elements->buildElementsQuery($criteria);
5
  • 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.
    – Stalinko
    Feb 22 '18 at 14:00
  • 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 github.com/craftcms/cms/blob/master-v2/src/helpers/… That's the function that converts the strings you insert in your attribute into a db query Feb 22 '18 at 14:11
  • 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 Feb 22 '18 at 14:16
  • 1
    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 :(
    – Stalinko
    Feb 22 '18 at 14:37
  • 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. Feb 22 '18 at 14:43

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.