1

Element API, query entries where relationship field ':empty:' AND the same relationship field with specified entry ID.

Can't seem to figure out how to do this without performing two separate queries and merging the objects together. However I don't see a way to perform this within the Element API.

The answer to this other question seems like an approach? Option 1 or Option 2? Not quite sure as this looks all foreign to me with sparse documentation, and it's not specific to my issue. How can I query entries based on a Matrix Block Type?

Seeing as you can't use ':empty:' with relatedTo, I'm stuck here.

return [
    'criteria' => [
        'section' => 'mySection',
        /* this is the myRelatedField entries field relating to other Section Entry ID */
        'relatedTo' => (['targetElement' => $otherSectionEntryId]),
        /* unable to use :empty: with the relatedTo, incorporating myRelatedField :empty: below will return zero results */
        /* cannot use this below */
        'myRelatedField' => ':empty:'
    ],
    'transformer' => function(Entry $entry) {
        return [
            'title' => $entry->title
        ];
    }
];

UPDATE with further testing. Various tests used below in trying to use simple relations with :empty:. It looks as though ':empty:' is ignored when utilized in an array.

// results returned where relationship field is empty
'relatedDrinks' => ':empty:'

// no results returned where relationship field is empty
'relatedDrinks' => [':empty:']

// results related to drink ID returned
'relatedDrinks' => [$myDrinkId]

// results returned for drink ID, no results returned with empty
'relatedDrinks' => [':empty:', $myDrinkId]
1

Craft will include this functionality in their next release. https://github.com/craftcms/cms/issues/7256#issuecomment-744644568

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.