<p>Used an two step workaround by splitting it in 2 queries, merging the ids and searching by id in a 3rd query.</p>
<pre><code>$searchUsersIds = User::find()-&gt;search($search['rawSearch'])-&gt;ids();

$searchUserResultIds = [];
if(!empty($searchUsersIds)) {
    $searchUserQuery =  clone $searchQuery;
    $searchUserQuery-&gt;relatedTo(
        ['or',
            ['targetElement' =&gt; $searchUsersIds, 'field' =&gt; 'field1'],
            ['targetElement' =&gt; $searchUsersIds, 'field' =&gt; 'field2']
        ]
    );

    $searchUserResultIds = $searchUserQuery-&gt;ids();
}

$searchCompanyQuery = clone $searchQuery;
$searchCompanyResultIds = $searchCompanyQuery-&gt;search($search['rawSearch'])-&gt;ids();

$companyEntryIds = array_merge($searchUserResultIds, $searchCompanyResultIds);

if(!empty($companyEntryIds)) {
   $searchQuery-&gt;id($companyEntryIds);
}
</code></pre>

CC BY-SA 4.0
2023-06-16T11:01:44.763Z
2023-06-16T11:01:44.763Z
bartpulse
<p>I think <code>relatedTo</code> should not be used in that place, but should be used on the <code>$searchQuery</code> object instead:</p>
<pre><code>$searchQuery
  -&gt;relatedTo([
    ['field' =&gt; 'field1', 'targetElement' =&gt; $ids],
    ['field' =&gt; 'field2', 'targetElement' =&gt; $ids],
  ])
  -&gt;search('title:' ~ $search);
</code></pre>
<p>That being said, this would not give you the results that I think you're looking for (elements related to the $ids, OR with the title you searched for). To be honest, I don't think this can be done with one query, but maybe someone else knows the trick?</p>

CC BY-SA 4.0
2023-06-16T10:59:35.760Z
Yeah thanks for the reply but that solution is idd an AND not an OR. I used a workaround to get it to work. I posted it below.
bartpulse
CC BY-SA 4.0
2023-06-15T13:33:30.953Z
2023-06-15T13:33:30.953Z
Paul
<p>I'm trying to create an or query with 2 related fields and 1 title field.
My version so far is this:</p>
<pre><code>$searchQuery-&gt;where([
    'or',
    ['relatedTo', 'targetElement' =&gt; $ids, 'field' =&gt; 'field1'],
    ['relatedTo', 'targetElement' =&gt; $ids, 'field' =&gt; 'field2'],
    ['like', 'title', $search]
]);
</code></pre>
<p>But with that query I keep getting the error:
<code>Undefined array key 0</code></p>
<p>$ids is not empty.</p>
<p>Any suggestions?</p>

<craft3><craft4><yii>
Help building a query with related to
# Help building a query with related to

(Asked by: username_0 on 2023-06-15)

I'm trying to create an or query with 2 related fields and 1 title field.
My version so far is this:
$searchQuery->where([
    'or',
    ['relatedTo', 'targetElement' => $ids, 'field' => 'field1'],
    ['relatedTo', 'targetElement' => $ids, 'field' => 'field2'],
    ['like', 'title', $search]
]);

But with that query I keep getting the error:
Undefined array key 0
$ids is not empty.
Any suggestions?

**Tags:** craft3, craft4, yii

---

## 2 answers

### Answer by username_0 on 2023-06-16. Score: 1

Used an two step workaround by splitting it in 2 queries, merging the ids and searching by id in a 3rd query.
$searchUsersIds = User::find()->search($search['rawSearch'])->ids();

$searchUserResultIds = [];
if(!empty($searchUsersIds)) {
    $searchUserQuery =  clone $searchQuery;
    $searchUserQuery->relatedTo(
        ['or',
            ['targetElement' => $searchUsersIds, 'field' => 'field1'],
            ['targetElement' => $searchUsersIds, 'field' => 'field2']
        ]
    );

    $searchUserResultIds = $searchUserQuery->ids();
}

$searchCompanyQuery = clone $searchQuery;
$searchCompanyResultIds = $searchCompanyQuery->search($search['rawSearch'])->ids();

$companyEntryIds = array_merge($searchUserResultIds, $searchCompanyResultIds);

if(!empty($companyEntryIds)) {
   $searchQuery->id($companyEntryIds);
}


---

### Answer by username_1 on 2023-06-15. Score: 1

I think relatedTo should not be used in that place, but should be used on the $searchQuery object instead:
$searchQuery
  ->relatedTo([
    ['field' => 'field1', 'targetElement' => $ids],
    ['field' => 'field2', 'targetElement' => $ids],
  ])
  ->search('title:' ~ $search);

That being said, this would not give you the results that I think you're looking for (elements related to the $ids, OR with the title you searched for). To be honest, I don't think this can be done with one query, but maybe someone else knows the trick?

> **Comments:**
> **username_0**: Yeah thanks for the reply but that solution is idd an AND not an OR. I used a workaround to get it to work. I posted it below.  

---

