How Can search in translation fields

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

I think it would be better if you have a foreign-key to link both tables together (Items and ItemsTranslation table). See Associations on how to do it.

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

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:

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

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

1 Like

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

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:

[
    '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

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

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

1 Like

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

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