1

I have a simple query:

$paidFeatures = Entry::find()
            ->section('paidFeatures')
            ->type(['simpleFeature', 'customFeature'])
            ->with(['relatedEntries'])
            ->status(['expired', 'disabled'])
            ->limit(10);

Some of the entries do not have relatedEntries linked/relationship setup.

I only want paidFeatures with linked/related relatedEntries ? So basically any entry in my paidFeatures list will always show a relatedEntries. No blanks.

I'm struggling to get my query to further filter linked/related relatedEntries.

UPDATE 1:

I've also tried this but doesnt give me what I want either:

$paidFeatures = Entry::find()
            ->section('paidFeatures')
            ->type(['simpleFeature', 'customFeature'])
            //->with(['relatedEntries'])
            //->relatedEntries('status:live')
            ->status(['expired', 'disabled'])
            ->all();
    $paidFeaturesWithRelatedEntries = Entry::find()
        ->section('ideas')
        ->relatedTo($paidFeatures)
        ->all();

How can I do this?

Sean Delaney
  • 720
  • 6
  • 15

1 Answers1

1

The with parameter is used for eager-loading to reduce the amount of queries your template needs to execute, but it doesn't apply any filtering. If you want to limit your query to entries that have at least one related entry in the relatedEntries field, you can use the :notempty: syntax:

$paidFeatures = Entry::find()
            ->section('paidFeatures')
            ->type(['simpleFeature', 'customFeature'])
            ->with(['relatedEntries'])
            ->relatedEntries(':notempty:')
            ->status(['expired', 'disabled'])
            ->limit(10)
            ->all();

If you want to limit the query to entries related to a specific set of other entries, you can pass in an entry query, array of entries or entry IDs (see the documentation linked above for supported formats):

$awesomeFeatures = Entry::find()
            ->section('paidFeatures')
            // add whatever conditions you want here
            ->type('awesome_features');

$paidFeatures = Entry::find() ->section('paidFeatures') ->type(['simpleFeature', 'customFeature']) ->with(['relatedEntries']) ->relatedEntries($awesomeFeatures) ->status(['expired', 'disabled']) ->limit(10) ->all();

MoritzLost
  • 11,215
  • 5
  • 22
  • 1
    I think the ->relatedEntries(':notempty:') is what I needed. Giving it a go now. Will report back. – Sean Delaney Feb 02 '22 at 15:20
  • 1
    Thanks @MoritzLost, that worked with a few tweaks. Now that you pointed me to the correct query setup, I remember seeing :notempty: in the docs but it didn't click. TBH I probably spent too long looking at the code, trying to resolve it myself that I started to overlook stuff. Thanks again! – Sean Delaney Feb 02 '22 at 16:05
  • @SeanDelaney Yeah, some of those methods are really well hidden in the docs. Glad you got it working! – MoritzLost Feb 02 '22 at 16:14