L
L
Lander2017-12-26 11:29:35
WordPress
Lander, 2017-12-26 11:29:35

Explain how to make a correct relation in Yii2?

Good afternoon.
We need language support for the site (So that the names of objects are displayed in the user's language, which is stored in the session ['eng', 'rus']). I made the following database architecture:
City table:

id |name      |description
1  |Москва    |Описание Москвы
2  |Волгоград |Описание Волгограда

lang table:
id |entity  |entity_id |lang |param        |value
1  |city    |1         |rus  |name         |Москва
2  |city    |1         |eng  |name         |Moscow
3  |city    |2         |rus  |name         |Волгоград
4  |city    |2         |eng  |name         |Volgograd
5  |city    |1         |rus  |description  |Описание Москвы
6  |city    |1         |eng  |description  |About Moscow
...

Then he registered the corresponding relationship in the City model:
public function getName()
{
    return $this->hasOne(Lang::className(), ['entity_id' => 'id'])
                ->where(['lang' => $this->getLang(), 'param' => 'name', 'entity' => 'city']);
}
//$this->getLang() - просто возвращает из сессии значение языка

I'm trying to organize a city search by the name field with the value of the current language:
City::find()->joinWith('name')->where([ 'like', 'name', 'Волг' ]);
//Формирует запрос: SELECT `city`.* FROM `city` LEFT JOIN `lang` ON `city`.`id` = `lang`.`entity_id` WHERE (`name` LIKE '%Волг%') AND ((`lang`='rus') AND (`param`='name') AND (`entity`='city'))

How can I force a request to form a model like:
SELECT `city`.* FROM `city` LEFT JOIN `lang` ON `city`.`id` = `lang`.`entity_id` WHERE (`value` LIKE '%Волг%') AND ((`lang`='rus') AND (`param`='name') AND (`entity`='city'))

( `value` LIKE '%Volg%') instead of ( `name` LIKE '%Volg%')?
Or did I go the wrong way in the first place? Nowhere can I find an example of just such a cunning connection. Mostly one-to-one or one-to-many.
I will be grateful for your help!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey Sanych, 2019-10-17
@mountpoint

add method="post"

<form action="#" method="post" class="contact__form">

I
its2easyy, 2019-10-17
@its2easyy

After sending, the same page is loaded, because the action is not specified in the form, the data from the fields is in the line because the form type is not specified, and by default it is GET and the information in the url is encoded. In general, all this happens because the ajax script does not work, which cancels the default behavior of the form and submits it itself. And the script may not work due to errors, due to the fact that it is not loaded on the page, or because it is in the wrong place. So first of all, you need to look at the errors in the console after submitting the form

M
Maxim Timofeev, 2017-12-26
@usdglander

there is even a ready-made behavior https://github.com/OmgDef/yii2-multilingual-behavior

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question