<p>I ended up fixing this with a variable and querying it myself.</p>

<p>In this case I needed the related entry if it was linked and a <code>false</code> if it wasn't.</p>

<pre><code>$neoElement = craft()-&gt;db-&gt;createCommand()
    -&gt;select('craft_relations.sourceId')
    -&gt;from('relations')
    -&gt;where('craft_relations.fieldId = 110')
    -&gt;andWhere('craft_relations.targetId = :entryId', ['entryId' =&gt; $entry-&gt;id])
    -&gt;queryColumn();

if(!empty($neoElement)) {
    $parents = craft()-&gt;db-&gt;createCommand()
        -&gt;select('craft_neoblocks.ownerId')
        -&gt;from('neoblocks')
        -&gt;where('craft_neoblocks.id = :neoElementId', ['neoElementId' =&gt; $neoElement[0]])
        -&gt;queryColumn();

    if(!empty($parents)) {
        $entry = craft()-&gt;entries-&gt;getEntryById($parents[0]);
        return $parent;
    }
}

return false;
</code></pre>

CC BY-SA 3.0
2017-12-22T10:40:48.707Z
2017-12-22T10:40:48.707Z
Jan Henckens
<p>In channel A, we have a Neo field with a couple of different block types, in 1 of those types we have an entries field with channel B entries.</p>

<p>On the channel B entry, I'd like to find out if it is related to a channel A entry through that Neo field.</p>

<p>Going by <a href="https://craftcms.com/docs/relations" rel="nofollow noreferrer">https://craftcms.com/docs/relations</a> and how it works for a Matrix field I have this:</p>

<pre><code>      {% set partOfChannelA = craft.entries.section('channelA').relatedTo({
        targetElement: entryChannelB,
        field: 'neoField.blockType.fieldHandle'
    }) %}
</code></pre>

<p>But that doesn't work :)
Am I missing something or does this simply not work with a Neo field?
Thanks for helping out!</p>

CC BY-SA 3.0
2017-12-15T16:44:44.840Z
Try to add the field id or the field object instead of this string. It's easier in my opinion and has less room for errors.. And of course it's faster
Robin Schambach
CC BY-SA 3.0
2017-12-20T08:10:51.477Z
Could you extend that answer a bit? I've gotten the field with craft.fields.getFieldByHandle() but that doens't seem to work either.
Jan Henckens
CC BY-SA 3.0
2017-12-20T08:24:33.157Z
That's strange, you should actually not be able to get the field by handle since matrix fields are unable to be fetched by handle directly That's why I said you should use the id instead. You could use something like `{% set partOfChannelA = craft.entries.section('channelA').relatedTo({
        targetElement: entryChannelB,
        field: 'insertYourFieldIdHere'
    }) %}` or leave the field blank if your structure allows it
Robin Schambach
CC BY-SA 3.0
2017-12-20T08:43:04.203Z
The problem is probably that I'm trying to do this in a Neo field, not in a Matrix field...
Jan Henckens
CC BY-SA 3.0
2017-12-20T09:11:14.007Z
Ahh now I see.. sorry for my last comment. I did not understand you are going to revert search. This is not possible like stated here https://github.com/benjamminf/craft-neo/issues/72 I did something similar few weeks ago in this thread https://craftcms.stackexchange.com/questions/23117/how-to-get-all-images-from-entries-including-matrix-field it works the same way. You would have to select the entry ids by yourself with php with the relation table
Robin Schambach
CC BY-SA 3.0
2017-12-20T10:18:07.237Z
Would you mind posting your code as an answer when you are ready and accept it for future reference if someone has the same problem?
Robin Schambach
<relations><relatedto><plugin-neo>
Get relatedTo from Neo field
# Get relatedTo from Neo field

(Asked by: username_0 on 2017-12-15)

In channel A, we have a Neo field with a couple of different block types, in 1 of those types we have an entries field with channel B entries.
On the channel B entry, I'd like to find out if it is related to a channel A entry through that Neo field.
Going by https://craftcms.com/docs/relations and how it works for a Matrix field I have this:
      {% set partOfChannelA = craft.entries.section('channelA').relatedTo({
        targetElement: entryChannelB,
        field: 'neoField.blockType.fieldHandle'
    }) %}

But that doesn't work :)
Am I missing something or does this simply not work with a Neo field?
Thanks for helping out!

**Tags:** relations, relatedto, plugin-neo

> **Comments:**
> **username_1**: Try to add the field id or the field object instead of this string. It's easier in my opinion and has less room for errors.. And of course it's faster  
> **username_0**: Could you extend that answer a bit? I've gotten the field with craft.fields.getFieldByHandle() but that doens't seem to work either.  
> **username_1**: That's strange, you should actually not be able to get the field by handle since matrix fields are unable to be fetched by handle directly That's why I said you should use the id instead. You could use something like `{% set partOfChannelA = craft.entries.section('channelA').relatedTo({         targetElement: entryChannelB,         field: 'insertYourFieldIdHere'     }) %}` or leave the field blank if your structure allows it  
> **username_0**: The problem is probably that I'm trying to do this in a Neo field, not in a Matrix field...  
> **username_1**: Ahh now I see.. sorry for my last comment. I did not understand you are going to revert search. This is not possible like stated here https://github.com/benjamminf/craft-neo/issues/72 I did something similar few weeks ago in this thread https://craftcms.stackexchange.com/questions/23117/how-to-get-all-images-from-entries-including-matrix-field it works the same way. You would have to select the entry ids by yourself with php with the relation table  
> **username_1**: Would you mind posting your code as an answer when you are ready and accept it for future reference if someone has the same problem?  

---

## 1 answers

### Answer by username_0 on 2017-12-22. Score: 2 | Accepted answer

I ended up fixing this with a variable and querying it myself.
In this case I needed the related entry if it was linked and a false if it wasn't.
$neoElement = craft()->db->createCommand()
    ->select('craft_relations.sourceId')
    ->from('relations')
    ->where('craft_relations.fieldId = 110')
    ->andWhere('craft_relations.targetId = :entryId', ['entryId' => $entry->id])
    ->queryColumn();

if(!empty($neoElement)) {
    $parents = craft()->db->createCommand()
        ->select('craft_neoblocks.ownerId')
        ->from('neoblocks')
        ->where('craft_neoblocks.id = :neoElementId', ['neoElementId' => $neoElement[0]])
        ->queryColumn();

    if(!empty($parents)) {
        $entry = craft()->entries->getEntryById($parents[0]);
        return $parent;
    }
}

return false;


---

