# How Can search in translation fields

**URL:** <https://discourse.cakephp.org/t/how-can-search-in-translation-fields/11057>\
**Category:** Need Help\
**Created:** [March 16, 2023, 5:33pm UTC](https://discourse.cakephp.org/t/how-can-search-in-translation-fields/11057 "2023-03-16T17:33:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghaly](https://avatars.discourse-cdn.com/v4/letter/g/b9bd4f/32.png) [@ghaly](https://discourse.cakephp.org/u/ghaly)\
**Post date:** [March 16, 2023, 5:33pm UTC](https://discourse.cakephp.org/t/how-can-search-in-translation-fields/11057/1 "2023-03-16T17:33:27Z")

</div>

I have table “Items”  
columns:  
id + name+ content

and the translation table is:  
items\_translation  
columns:  
id + locale + name + content

so I need to search in name column by current locale

---

<div class="post-metadata">

**Author:** ![yousuo](https://avatars.discourse-cdn.com/v4/letter/y/898d66/32.png) [@yousuo](https://discourse.cakephp.org/u/yousuo)\
**Post date:** [March 18, 2023, 3:29am UTC](https://discourse.cakephp.org/t/how-can-search-in-translation-fields/11057/2 "2023-03-18T03:29:14Z")

</div>

> [@ghaly](#):
>
> column

I think it would be better if you have a foreign-key to link both tables together (`Items` and `ItemsTranslation` table). See [Associations](https://book.cakephp.org/4/en/orm/associations.html) on how to do it.

---

<div class="post-metadata">

**Author:** ![ghaly](https://avatars.discourse-cdn.com/v4/letter/g/b9bd4f/32.png) [@ghaly](https://discourse.cakephp.org/u/ghaly)\
**Post date:** [March 18, 2023, 6:10am UTC](https://discourse.cakephp.org/t/how-can-search-in-translation-fields/11057/3 "2023-03-18T06:10:19Z")

</div>

Do you think the best association is **HasMany Associations**  
and I do this association how can search in it ?

---

<div class="post-metadata">

**Author:** ![ndm](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/ndm/32/2234_2.png) [@ndm](https://discourse.cakephp.org/u/ndm)\
**Post date:** [March 18, 2023, 1:53pm UTC](https://discourse.cakephp.org/t/how-can-search-in-translation-fields/11057/4 "2023-03-18T13:53:41Z")

</div>

The translated field in the current locale is being joined in automatically by the translate behavior, that’s what you can filter on. The behavior’s `translationField()` method will give you the proper name of the field to use in your conditions:

```php
$query = $this->Items
    ->find()
    ->where([
        $this->Items->translationField('name') => 'John Doe',
    ]);

```

[https://book.cakephp.org/4/en/orm/behaviors/translate.html#querying-translated-fields](https://book.cakephp.org/4/en/orm/behaviors/translate.html#querying-translated-fields)

---

<div class="post-metadata">

**Author:** ![ghaly](https://avatars.discourse-cdn.com/v4/letter/g/b9bd4f/32.png) [@ghaly](https://discourse.cakephp.org/u/ghaly)\
**Post date:** [March 18, 2023, 7:57pm UTC](https://discourse.cakephp.org/t/how-can-search-in-translation-fields/11057/6 "2023-03-18T19:57:36Z")

</div>

That’s work find, but what I want to find in multi columns with “OR” conditions

---

<div class="post-metadata">

**Author:** ![ndm](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/ndm/32/2234_2.png) [@ndm](https://discourse.cakephp.org/u/ndm)\
**Post date:** [March 18, 2023, 8:11pm UTC](https://discourse.cakephp.org/t/how-can-search-in-translation-fields/11057/7 "2023-03-18T20:11:44Z")

</div>

You’d do it just like with any other field, there’s nothing special about translation fields, other than that you retrieve them dynamically instead of hardcoding them. Either use the special `OR` key, or the `IN` operator:

```php
[
    'OR' => [
        [$this->Items->translationField('name') => 'John Doe'],
        [$this->Items->translationField('name') => 'Jane Doe'],
    ],
]

```

[https://book.cakephp.org/4/en/orm/query-builder.html#advanced-conditions](https://book.cakephp.org/4/en/orm/query-builder.html#advanced-conditions)

```php
[
    $this->Items->translationField('name') . ' IN' => [
        'John Doe',
        'Jane Doe',
    ],
]

```

[https://book.cakephp.org/4/en/orm/query-builder.html#automatically-creating-in-clauses](https://book.cakephp.org/4/en/orm/query-builder.html#automatically-creating-in-clauses)

---

<div class="post-metadata">

**Author:** ![ghaly](https://avatars.discourse-cdn.com/v4/letter/g/b9bd4f/32.png) [@ghaly](https://discourse.cakephp.org/u/ghaly)\
**Post date:** [March 19, 2023, 7:42am UTC](https://discourse.cakephp.org/t/how-can-search-in-translation-fields/11057/8 "2023-03-19T07:42:54Z")

</div>

what about one of them translationField and the other **non translationField**  
how can search by **AND** or **OR**.

---

<div class="post-metadata">

**Author:** ![ndm](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/ndm/32/2234_2.png) [@ndm](https://discourse.cakephp.org/u/ndm)\
**Post date:** [March 19, 2023, 2:04pm UTC](https://discourse.cakephp.org/t/how-can-search-in-translation-fields/11057/9 "2023-03-19T14:04:49Z")

</div>

Just try using different field names and see what happens. Have you to read the linked documentation?
