<p>It's actually <a href="https://github.com/craftcms/cms/issues/6922#issuecomment-908871118" rel="noreferrer">OK to hard code the complete field column name</a> (i.e. <code>field_nationality_gkpaccfs</code>) – it won't change between environments, or if the field is resaved (unless you change the handle, of course).</p>
<p>If you don't want to do that, you can do this:</p>
<pre class="lang-php prettyprint-override"><code>use craft\helpers\ElementHelper;

$nationalityField = Craft::$app-&gt;getFields()-&gt;getFieldByHandle('nationality');
$nationalityFieldColumn = ElementHelper::fieldColumnFromField($nationalityField);

...
$query-&gt;orWhere(['like', '`content`.`' . $nationalityFieldColumn . '`', $param]);
</code></pre>

CC BY-SA 4.0
2022-03-24T07:47:04.810Z
Thanks, this was exactly what I was looking for. It just seems wrong to write such a generated column name directly into the code, I much prefer using the ElementHelper Class.
Aram Loosman
CC BY-SA 4.0
2022-03-24T08:07:41.517Z
Yeah I agree :)
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2022-03-22T17:30:30.517Z
2022-03-22T17:30:30.517Z
Mats Mikkel Rummelhoff
<p>I want to query some entries in a section.</p>
<p>I am able to write a query using a GET Param as a filter on the title field.
If I want to query a custom field however I need to know the precise column otherwise craft yells at me.</p>
<p>How do I get to this information?</p>
<p>The following code is a very basic example of what I mean.</p>
<pre><code>$request = Craft::$app-&gt;request;

$query = Entry::find()-&gt;section('profiles');
        
if($request-&gt;getParam('name')){
    $param = $request-&gt;getParam('name');
            
    $query-&gt;orWhere(['like', '`content`.`title`', $param]);
    $query-&gt;orWhere(['like', '`content`.`field_nationality_gkpaccfs`', $param]);
}

return $query-&gt;all();
</code></pre>
<p>Or is there a better alternative on how to query entries on multiple properties?
I know that $query-&gt;title(...) would work, but I do not know how to chain multiple of those together using OR.</p>

<craft3><entries><field><query><entries-field>
How to get column name of an entrie's field
# How to get column name of an entrie's field

(Asked by: username_0 on 2022-03-22)

I want to query some entries in a section.
I am able to write a query using a GET Param as a filter on the title field.
If I want to query a custom field however I need to know the precise column otherwise craft yells at me.
How do I get to this information?
The following code is a very basic example of what I mean.
$request = Craft::$app->request;

$query = Entry::find()->section('profiles');
        
if($request->getParam('name')){
    $param = $request->getParam('name');
            
    $query->orWhere(['like', '`content`.`title`', $param]);
    $query->orWhere(['like', '`content`.`field_nationality_gkpaccfs`', $param]);
}

return $query->all();

Or is there a better alternative on how to query entries on multiple properties?
I know that $query->title(...) would work, but I do not know how to chain multiple of those together using OR.

**Tags:** craft3, entries, field, query, entries-field

---

## 1 answers

### Answer by username_1 on 2022-03-22. Score: 5 | Accepted answer

It's actually OK to hard code the complete field column name (i.e. field_nationality_gkpaccfs) – it won't change between environments, or if the field is resaved (unless you change the handle, of course).
If you don't want to do that, you can do this:
use craft\helpers\ElementHelper;

$nationalityField = Craft::$app->getFields()->getFieldByHandle('nationality');
$nationalityFieldColumn = ElementHelper::fieldColumnFromField($nationalityField);

...
$query->orWhere(['like', '`content`.`' . $nationalityFieldColumn . '`', $param]);

> **Comments:**
> **username_0**: Thanks, this was exactly what I was looking for. It just seems wrong to write such a generated column name directly into the code, I much prefer using the ElementHelper Class.  
> **username_1**: Yeah I agree :)  

---

