<p>I've got a pretty complex relatedTo situation and I can't figure out if it's possible in Craft.</p>

<p>Admin users have a field called "Admin Location" which is a Category field type, and may have more than one Category selected.</p>

<p>Regular users have a Matrix field called "Property Applications" which contains a sub-field of "Property" which is an Entry type. That Entry itself has a "Property Location" field - which is a Category field type with the same source categories as the Admin Location field.</p>

<p>For a given admin user, who has their Admin Location field set to one or more locations, I want to list all Users who have a Property in that same location.</p>

<p>This involves going through a Matrix, to an Entry, and then using the Entry's Category field.</p>

<p>I had tried this, but I am not getting results that I should if it were working as I wanted</p>

<pre><code>{% set adminLocation = currentUser.adminLocation %}
{% set users = craft.users.relatedTo({
    element: adminLocation,
    field: 'propertyApplications.property.propertyLocation'
}) %}
</code></pre>

<p>Can anyone help?</p>

CC BY-SA 3.0
2018-02-06T14:06:47.457Z
Your `adminLocation` is an object of type `ElementCriteriaModel` not an element. You need to do `currentUser.adminLocation.ids()` to fetch the elements. Tell me of it works then... If not remove the field variable
Robin Schambach
CC BY-SA 3.0
2018-02-06T14:51:11.783Z
It does not, and I can't just remove the field variable as it's important to the relation - it need to be related from that Matrix field's Entry field. 

I think the issue is I'm not looking for a relation on a field in the Matrix itself, but on a field from the Entry that's referenced in the Matrix.
Matt Wilcox
CC BY-SA 3.0
2018-02-06T15:02:22.023Z
Ahh yes of course.. sry I forgot about that. You can't revert search those fields like you are going to do it. You'll need php or a really messy twig solution for it because your countries are not related to the user but to the matrix block. You can see a  good example here https://craftcms.stackexchange.com/questions/23117/how-to-get-all-images-from-entries-including-matrix-field a user wants exactly the same like you, just with assets and not with categories
Robin Schambach
CC BY-SA 3.0
2018-02-06T15:08:34.437Z
That's unfortunately what I was worried was the case. I'm currently trying to write a plugin that uses the onBeforeSaveUser event to populate a new field on the Matrix which is an Entry type, with the same data as I need. It's not going so well! :/ Thanks though.
Matt Wilcox
CC BY-SA 3.0
2018-02-06T16:20:10.583Z
It's actually not that complicated you just have to understand Crafts database and it's relations but in order to help you we'll need your exact field layout in a clear way. Yii's query builder is really awesome, so you can create all relations by your own
Robin Schambach
CC BY-SA 3.0
2018-02-06T16:54:12.253Z
![Matrix Field Details](https://dev1.viewcreative.co.uk/example.png)

I'm trying to get the Property Location you can see there populated with the same content as is found in the Property Applied For's entry's own "Property Location" field - purely so I can do the relatedTo thing.
Matt Wilcox
CC BY-SA 3.0
2018-02-06T16:55:00.363Z
Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/72774/discussion-between-matt-wilcox-and-robin-schambach).
Matt Wilcox
<relations>
Complex Relations
# Complex Relations

(Asked by: username_0 on 2018-02-06)

I've got a pretty complex relatedTo situation and I can't figure out if it's possible in Craft.
Admin users have a field called "Admin Location" which is a Category field type, and may have more than one Category selected.
Regular users have a Matrix field called "Property Applications" which contains a sub-field of "Property" which is an Entry type. That Entry itself has a "Property Location" field - which is a Category field type with the same source categories as the Admin Location field.
For a given admin user, who has their Admin Location field set to one or more locations, I want to list all Users who have a Property in that same location.
This involves going through a Matrix, to an Entry, and then using the Entry's Category field.
I had tried this, but I am not getting results that I should if it were working as I wanted
{% set adminLocation = currentUser.adminLocation %}
{% set users = craft.users.relatedTo({
    element: adminLocation,
    field: 'propertyApplications.property.propertyLocation'
}) %}

Can anyone help?

**Tags:** relations
> **Comments:**
> **username_1**: Your `adminLocation` is an object of type `ElementCriteriaModel` not an element. You need to do `currentUser.adminLocation.ids()` to fetch the elements. Tell me of it works then... If not remove the field variable  
> **username_0**: It does not, and I can't just remove the field variable as it's important to the relation - it need to be related from that Matrix field's Entry field.   I think the issue is I'm not looking for a relation on a field in the Matrix itself, but on a field from the Entry that's referenced in the Matrix.  
> **username_1**: Ahh yes of course.. sry I forgot about that. You can't revert search those fields like you are going to do it. You'll need php or a really messy twig solution for it because your countries are not related to the user but to the matrix block. You can see a  good example here https://craftcms.stackexchange.com/questions/23117/how-to-get-all-images-from-entries-including-matrix-field a user wants exactly the same like you, just with assets and not with categories  
> **username_0**: That's unfortunately what I was worried was the case. I'm currently trying to write a plugin that uses the onBeforeSaveUser event to populate a new field on the Matrix which is an Entry type, with the same data as I need. It's not going so well! :/ Thanks though.  
> **username_1**: It's actually not that complicated you just have to understand Crafts database and it's relations but in order to help you we'll need your exact field layout in a clear way. Yii's query builder is really awesome, so you can create all relations by your own  
> **username_0**: ![Matrix Field Details](https://dev1.viewcreative.co.uk/example.png)  I'm trying to get the Property Location you can see there populated with the same content as is found in the Property Applied For's entry's own "Property Location" field - purely so I can do the relatedTo thing.  
> **username_0**: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/72774/discussion-between-matt-wilcox-and-robin-schambach).  

---

## 0 answers

