con322
(Connor Brough)
December 21, 2015, 7:08pm
1
So I following database setup, i have created a form when creating a blog post with a dropdown for tags however on submit I want it to populate the join table.
The below only works if I manually set the tag ID.
if ($model->load(Yii::$app->request->post())) {
$model->save(false);
$tags->blog_id = $model->id;
//$tags->tag_id = 1;
$tags->save(false);
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
'tags' => $brands,
]);
}
Form Field Setup
<?= Html::activeDropDownList($tags, 'tag_id',
ArrayHelper::map(tags::find()->all(), 'id', 'title')) ?>
Table/Database Structure below:
Blog -id (PK) -title -content
Blog_category -id (PK) -blog_id (FK) -tag_id (FK)
Tags -id -title
con322
(Connor Brough)
December 21, 2015, 9:31pm
2
So I following database setup, i have created a form when creating a blog post with a dropdown for tags however on submit I want it to populate the join table.
Mysql error when trying to insert data set by form and not manually
Cannot add or update a child row: a foreign key constraint fails
The below only works if I manually set the tag ID.
if ($model->load(Yii::$app->request->post())) {
$model->save(false);
$tags->blog_id = $model->id;
//$tags->tag_id = 1;
$tags->save(false);
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
'tags' => $brands,
]);
}
Form Field Setup
<?= Html::activeDropDownList($tags, 'tag_id',
ArrayHelper::map(tags::find()->all(), 'id', 'title')) ?>
Table/Database Structure below:
Blog -id (PK) -title -content
Blog_category -id (PK) -blog_id (FK) -tag_id (FK)
Tags -id -title
amir.duran
(Amir Duran)
December 22, 2015, 2:24pm
3
con322:
So I following database setup, i have created a form when creating a blog post with a dropdown for tags however on submit I want it to populate the join table.
Mysql error when trying to insert data set by form and not manually
Cannot add or update a child row: a foreign key constraint fails
The below only works if I manually set the tag ID.
if ($model->load(Yii::$app->request->post())) {
$model->save(false);
$tags->blog_id = $model->id;
//$tags->tag_id = 1;
$tags->save(false);
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
'tags' => $brands,
]);
}
Form Field Setup
<?= Html::activeDropDownList($tags, 'tag_id',
ArrayHelper::map(tags::find()->all(), 'id', 'title')) ?>
Table/Database Structure below:
Blog -id (PK) -title -content
Blog_category -id (PK) -blog_id (FK) -tag_id (FK)
Tags -id -title
Check weather your primary key in Tags table is defined as auto increment,
Question: Why you need tag_id in blog_category table?
softark
(Softark)
December 23, 2015, 12:09am
4
[color="#006400 "]/* topics merged. */[/color]