Answer the question
In order to leave comments, you need to log in
How to update a record in an intermediate table?
There are tables of news, projects and an intermediate table of news connection with the project and news. It is not entirely clear how this link () works, I would like an example specifically in my case. Now, tables:
news
id | title
projects
id | title
project_news
project_id | news_id
I create a post like this:
$model = new News(['scenario' => News::SCENARIO_CREATE]);
$project = new ProjectNews(); // Модель таблица project_news
$projectsList = Project::find()->all(); // Для вывода списка проектов
....
if($project->load(\Yii::$app->request->post())) {
$project->news_id = $model->id;
$project->save();
}
// Всё ок, запись создаются, в проекте новости выводятся и т.д
$model = News::findOne(['id' => $id]);
$model->scenario = News::SCENARIO_UPDATE;
$project = ProjectNews::findOne(['news_id' => $id]); // Поиск записи в БД
$projectsList = Project::find()->all(); // Для вывода списка проектов
// Если
$projectId = \Yii::$app->request->post('ProjectNews');
$project->project_id = $projectId['project_id'];
$project->update(); // Ругается на отсутсвие PK
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question