Answer the question
In order to leave comments, you need to log in
Yii2 How to delete all records with a specific id?
I create an action
public function actionUpdate($id){
$model = $this->findModel($id);
$postData = $this->findDataModel($id);
if (
$model->load(Yii::$app->request->post()) &&
$postData->load(Yii::$app->request->post()) &&
$model->save() &&
$postData->save()
){
$newData = PostData::find()->where(['post_id' => $id])->all();
$postData = new PostData();
$postData->post_id = $id;
$rows[] = [];
foreach($_POST['PostData'] as $key => $value){
$str = strpos($key, "_");
$key = substr($key, 0, $str);
$postData->data_type = $key;
$postData->post_data = $value;
$postData->post_id;
$rows[] = [
'post_id' => $postData->post_id,
'data_type' => $postData->data_type,
'post_data' => $postData->post_data
];
}
array_shift($rows);
Yii::$app->db->createCommand()->batchInsert(
$postData::tableName(),
['post_id', 'data_type', 'post_data'],
$rows
)->execute();
if ( isset($_POST['save']) || isset($_POST['update'])) {
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
}
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