2

My users have a relationship field called Organisation.

In my plugin, I am trying to fetch all users that are related to the organisation I pass it.

BUT I would like ALL users including Pending, so those who have not verified their email and set a password yet.

I am using this code but it only fetches the verified users, not pending.

$entries = craft\elements\User::find();

$relatedEntries = $entries->relatedTo(
    array(
        'targetElement' => $organisationId, 
        'field'         => 'organisation'
    )
);

// Fetch all entries
$relatedEntries = $relatedEntries->all(); 

I have also tried using combinations of ->status(null), e.g.

$entries = craft\elements\User::find();

    $relatedEntries = $entries->relatedTo(
        array(
            'targetElement' => $organisationId, 
            'field'         => 'organisation'
        )
    );

    // Fetch all entries
    $relatedEntries = $relatedEntries->status(null)->all();

or

$entries = craft\elements\User::find()->status(null);

    $relatedEntries = $entries->relatedTo(
        array(
            'targetElement' => $organisationId, 
            'field'         => 'organisation'
        )
    );

    // Fetch all entries
    $relatedEntries = $relatedEntries->all();

But they always just show all verified users.

Thanks

Edit

I have changed my logic and database setup a bit, and so decided to try using just template tags now. And so I got it working OK using .status(null with template tags.

{% set organisations = craft.entries().section('suppliers').authorId(currentUser.id) %}

{% for organisation in organisations %}

    {% set users = craft.users().relatedTo(organisation).status(null %}
    {% for user in users %}
        {{user.firstName}} {{user.lastName}} - {{user.status}}<br/>
    {% endfor%}

{% endfor%}

1 Answer 1

1

The correct syntax or finding all users regardless of status is status(null), which you've tried, but the issue isn't your status parameter, it's the targetElement parameter of your relatedTo query.

It only accepts the actual element you're looking to query, not its ID.

If you already have the target element object, you can just pass that in. If you don't, you can retrieve it with:

$targetElement = Craft::$app->getElements()->getElementById($organizationId);

Then use:

array(
    'targetElement' => $targetElement, 
    'field'         => 'organisation'
)

Relevant docs here: https://docs.craftcms.com/v3/relations.html#the-relatedto-parameter

2
  • I have actually switched to using template tags now, as I figured out how to still retrieve the entries without the plugin. In my template tags I am passing the element object organisation not its ID, but still cant get it. Will update my code to show the template code. Commented Nov 20, 2018 at 18:35
  • I got it working OK using .status(null) in the template tags. But good to know its because of passing an ID and not an object when using PHP. I am just unable to test due to changing logic. Commented Nov 20, 2018 at 18:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.