Answer the question
In order to leave comments, you need to log in
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 |Волгоград |Описание Волгограда
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
...
public function getName()
{
return $this->hasOne(Lang::className(), ['entity_id' => 'id'])
->where(['lang' => $this->getLang(), 'param' => 'name', 'entity' => 'city']);
}
//$this->getLang() - просто возвращает из сессии значение языка
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'))
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'))
Answer the question
In order to leave comments, you need to log in
add method="post"
<form action="#" method="post" class="contact__form">
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
there is even a ready-made behavior https://github.com/OmgDef/yii2-multilingual-behavior
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question