C
C
Chrome Toaster2019-11-23 18:06:08
MySQL
Chrome Toaster, 2019-11-23 18:06:08

Get a record from the database, if not found, return with other parameters?

There is a list of departments, with translations into different languages:
sections_i18n:
id, section_id, name, lang_id
there are entries like this:

1, 1, development, NULL (NULL - стандартный перевод)
2, 1, разработка, 12 (12 - id русского языка)

I need to try to get the translation for the Russian language, if it's not there, get the standard translation (with lang_id NULL).
How to implement this specifically in yii2 ?
in sql I know that something like this:
WITH cte AS (
 select ...
)
COALESCE( SELECT cte ..)

Please tell me, I will be very grateful

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2019-11-25
@hromtoster

Why make conditions in SQL? Make two requests. If the first request results in NULL , then send a second request for the Default value.

$translation = Translation::find()->where(...)->one;
//если не нашли перевод, то по-умолчанию
if(!$translation) {
      $translation = Translation::find()->where(...)->one
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question