In a plugin I am trying to get Users related to other Users:

$criteria = craft()->elements->getCriteria(ElementType::User);
$criteria->group = 'partners';
$criteria->relatedTo = [
  'sourceElement' => $someUserId

$result = $criteria->find();

I can see data in the relations table where sourceId is set to the value of $someUserId but I get nil results from $criteria->find().

I've tried passing in a UserModel, the user ID (as shown above) and changing sourceElement to targetElement and element all with no success.

What have I missed here?


It seems I can get what I need by doing this:

$user = craft()->users->getUserById(someUserId);
$relatedUsers = $user->partners;

This works but I'd like to understand what's wrong with the original code above. partners is the name of the relationship field on the source element as well as the name of the user group that that field targets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.