<p>If your <code>craft_searchindex</code> table (assume your table prefix is "craft_") isn't too big (I think 100k rows is big), and your field in "relatedMerchant" is the 'title' field.</p>

<p>You can do it in one step.</p>

<pre><code>$locationsRelatedToCategories = Entry::find()
   -&gt;section('locations')
   -&gt;limit(100000)
   -&gt;relatedTo([
     'targetElement' =&gt; $categoryElement, 'field' =&gt; 'locationOffers.category'
   ])
   -&gt;search('-relatedMerchant:*') 
   -&gt;all();
</code></pre>

<p>Otherwise, I don't think its possible. You will need to query the relatedMerchant entry to get ids().
And relate the id array to locationsRelatedToCategories.</p>

<pre><code>$relatedMerchantQuery = Entry::find()-&gt;section('relatedMerchangt');
$relatedMerchantQuery-&gt;field = ':notempty:';
$relatedMerchantIds = $relatedMerchantQuery-&gt;ids();

$locationsRelatedToCategories = Entry::find()
   -&gt;section('locations')
   -&gt;limit(100000)
   -&gt;relatedTo($relatedMerchantIds)
   -&gt;all();

</code></pre>

<p>Craft only store element relations via <code>craft_relations</code> table. The elementQuery->relatedTo() method is the only way to access it, without creating custom query or tables.</p>

CC BY-SA 4.0
2019-07-09T02:31:22.807Z
2019-07-09T02:31:22.807Z
Parry Huang
<p>Trying to figure out if this is possible in 1 step. I want to return locations that have a related entry in the relatedMerchant field, but I'd also want to make sure that a field in the relatedMerchant entry has a value... </p>

<pre><code>$locationsRelatedToCategories = Entry::find()
   -&gt;section('locations')
   -&gt;limit(100000)
   -&gt;relatedTo([
     // somewhat unrelated, but this entry should be related to a specific category
     'targetElement' =&gt; $categoryElement, 'field' =&gt; 'locationOffers.category'
   ])
   -&gt;relatedMerchant(':notempty:') // has to have a related merchant
   // somewhere here, how can I return only relatedMerchant entries where relatedMerchant.someField :notempty:
   -&gt;all();
</code></pre>

<relations><plugin-elementapi><elementquery>
relatedTo - return only entries that have a related entry where a field in the related entry has a value
# relatedTo - return only entries that have a related entry where a field in the related entry has a value

(Asked by: username_0 on 2019-06-25)

Trying to figure out if this is possible in 1 step. I want to return locations that have a related entry in the relatedMerchant field, but I'd also want to make sure that a field in the relatedMerchant entry has a value... 
$locationsRelatedToCategories = Entry::find()
   ->section('locations')
   ->limit(100000)
   ->relatedTo([
     // somewhat unrelated, but this entry should be related to a specific category
     'targetElement' => $categoryElement, 'field' => 'locationOffers.category'
   ])
   ->relatedMerchant(':notempty:') // has to have a related merchant
   // somewhere here, how can I return only relatedMerchant entries where relatedMerchant.someField :notempty:
   ->all();

**Tags:** relations, plugin-elementapi, elementquery


---

## 1 answers

### Answer by username_1 on 2019-07-09. Score: 1 | Accepted answer

If your craft_searchindex table (assume your table prefix is "craft_") isn't too big (I think 100k rows is big), and your field in "relatedMerchant" is the 'title' field.
You can do it in one step.
$locationsRelatedToCategories = Entry::find()
   ->section('locations')
   ->limit(100000)
   ->relatedTo([
     'targetElement' => $categoryElement, 'field' => 'locationOffers.category'
   ])
   ->search('-relatedMerchant:*') 
   ->all();

Otherwise, I don't think its possible. You will need to query the relatedMerchant entry to get ids().
And relate the id array to locationsRelatedToCategories.
$relatedMerchantQuery = Entry::find()->section('relatedMerchangt');
$relatedMerchantQuery->field = ':notempty:';
$relatedMerchantIds = $relatedMerchantQuery->ids();

$locationsRelatedToCategories = Entry::find()
   ->section('locations')
   ->limit(100000)
   ->relatedTo($relatedMerchantIds)
   ->all();


Craft only store element relations via craft_relations table. The elementQuery->relatedTo() method is the only way to access it, without creating custom query or tables.


---

