Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Allow reverse :empty: / :notempty: relation queries #3538

Closed
benface opened this issue Dec 7, 2018 · 1 comment
Closed
Labels
enhancement improvements to existing features site development 👩‍💻 features related to website/API development

Comments

@benface
Copy link
Contributor

benface commented Dec 7, 2018

Not sure if the title makes sense, because this is somewhat complicated, but I'll explain it with an example. Let's say I have two sections: articles and authors, and an article entry has a author field (relation field). I can easily list all the articles that have at least one author:

{% set articlesWithAnAuthor = craft.entries.section('articles').author(':notempty:').all() %}

Or all the articles that have no author:

{% set articlesWithoutAnAuthor = craft.entries.section('articles').author(':empty:').all() %}

But if I want to list all the authors that have at least one article, it's more complicated. I can do it with two queries:

{% set articleIds = craft.entries.section('articles').ids() %}
{% set authorsWithArticles = craft.entries.section('authors').relatedTo(articleIds).all() %}

But it would be nice to do it with just one:

{% set authorsWithArticles = craft.entries.section('authors').relatedTo({ section: 'articles', field: 'author', element: ':any:' }) %

(not sure at all if that API makes sense, just a suggestion)

If I want to list all the authors with no article, it's even more complicated:

{% set articleIds = craft.entries.section('articles').ids() %}
{% set idsOfAuthorsWithArticles = craft.entries.section('authors').relatedTo(articleIds).ids() %}
{% set authors = craft.entries.section('authors').all() %}
{% set authorsWithoutArticles = [] %}
{% for author in authors %}
    {% if author.id not in idsOfAuthorsWithArticles %}
        {% set authorsWithoutArticles = authorsWithoutArticles|merge([author]) %}
    {% endif %}
{% endfor %}

But it could be supported in a similar manner:

{% set authorsWithoutArticles = craft.entries.section('authors').notRelatedTo({ section: 'articles', field: 'author', element: ':any:' }) %

Thoughts?

@brandonkelly brandonkelly added enhancement improvements to existing features site development 👩‍💻 features related to website/API development labels Dec 10, 2018
@brandonkelly
Copy link
Member

Related: #1640

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
enhancement improvements to existing features site development 👩‍💻 features related to website/API development
Projects
None yet
Development

No branches or pull requests

2 participants