Answer the question
In order to leave comments, you need to log in
How to write data to Yii2 intermediate table?
Hello! Unable to write data to intermediate table.
Database schema:
Added a checkboxlist to the form for adding an entry (area), which displays a list of workers from the workers table:
<?= $form->field($model, 'workAreas')->checkboxList(ArrayHelper::map(Workers::find()->all(), 'id', 'name')) ?>
public function actionCreate()
{
$model = new Area();
$workarea = new WorkArea();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$workarea->area_id = $model->id;
$workarea->worker_id = $model->workAreas;
//$workarea->save();
var_dump($workarea);
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
public function getWorkAreas()
{
return $this->hasMany(WorkArea::className(), ['area_id' => 'id']);
}
Answer the question
In order to leave comments, you need to log in
Does the Area model have a property and rules for workAreas? How are they loaded from post into the model?
In the rules workAreas should be safe, then the array will be loaded and available. And yet, it must be translated into a string in order to save it to the database. worker_id must be of the appropriate type.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question