<p>Splitting the criteria up like this works.</p>

<pre><code>$criteria-&gt;relatedTo = [
    'targetElement' =&gt; anElementId,
    'field'         =&gt; 'relationshipOne'
];
$criteria-&gt;relationshipTwo = ':empty:';
</code></pre>

<p>Thanks to @carlcs in the Craft Slack channel.</p>

CC BY-SA 3.0
2017-09-01T20:33:19.477Z
2017-09-01T20:33:19.477Z
Benjamin Kohl
<p>I have two relationship fields. I am trying to query all entries where the first field is related to a particular element and the second field is related to nothing. Here is how I <em>thought</em> it would work but it doesn't.</p>

<pre><code>$criteria-&gt;relatedTo = ['and',
[
    'targetElement' =&gt; anElementId,
    'field'         =&gt; 'relationshipOne'
],
[
    'targetElement' =&gt; null, // also tried ":empty:"
    'field'         =&gt; 'relationshipTwo'
]];
</code></pre>

<plugin-development><relations><elementcriteriamodel>
How do I query two relationship fields where one relationship MUST be empty?
# How do I query two relationship fields where one relationship MUST be empty?

(Asked by: username_0 on 2017-09-01)

I have two relationship fields. I am trying to query all entries where the first field is related to a particular element and the second field is related to nothing. Here is how I thought it would work but it doesn't.
$criteria->relatedTo = ['and',
[
    'targetElement' => anElementId,
    'field'         => 'relationshipOne'
],
[
    'targetElement' => null, // also tried ":empty:"
    'field'         => 'relationshipTwo'
]];

**Tags:** plugin-development, relations, elementcriteriamodel


---

## 1 answers

### Answer by username_0 on 2017-09-01. Score: 4 | Accepted answer

Splitting the criteria up like this works.
$criteria->relatedTo = [
    'targetElement' => anElementId,
    'field'         => 'relationshipOne'
];
$criteria->relationshipTwo = ':empty:';

Thanks to @carlcs in the Craft Slack channel.


---

