2

I'm trying to use the Element Criteria Model to retrieve all entries where value X or value Y is not null?

I can get all entries where values and X and Y are not empty but not sure how to achieve this with either X or Y being not empty. any thoughts?

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->limit = NULL;

foreach ($fields AS $field) {
    $criteria->$field = ':notempty:';
}

$entries = $criteria->find();

This is what I have for AND but not sure how best to go about achieving the same query for OR.

An equivalent SQL query would be something along lines of WHERE (X IS NOT NULL && X <> '') OR (Y IS NOT NULL && Y <> '')

1 Answer 1

2

Okay, doesn't seem to be the most elegant but the only way I could achieve this was by carrying out the entries criteria query within my $fields loop. So, for example:

$entries = [];

foreach ($fields AS $field) {
    $criteria = craft()->elements->getCriteria(ElementType::Entry);
    $criteria->limit = NULL;
    $criteria->$field = ':notempty:';
    $tmpEntries = $criteria->find();

    $entries = array_merge($entries,$tmpEntries);
}

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.