If a value is there it will be updated else new data insert in yii2

can anyone help me in this code if the end date of an employee is null then new enddate will be inserted else the enddate will be updated.

My code is like this

if (isset(Yii::$app->request->post()['PostingHistory'])) {
                // PostingHistory::deleteAll(['papl_id'=>$url_papl_id]);
                $start_date = Yii::$app->request->post()['PostingHistory']['start_date'];
                $posting_datas = PostingHistory::find()->where(['papl_id' => $url_papl_id])->all();
                $enrollment_data->plant_id = $plant_id;
                // if (isset($url_papl_id)) {
                            
                //             PostingHistory::deleteAll(['papl_id'=>$url_papl_id,'end_date'=> null]);
                //         }
                foreach ($posting_datas as $posting_data) {
                    
                    if ($posting_data->end_date != '') {
                        // echo 'string';die();
                       // $posting_data->end_date = Yii::$app->request->post()['PostingHistory']['start_date'];
                       // $posting_data->update(false); 
                        
                        $model = new PostingHistory();
                        $model->start_date = $start_date;
                        $model->state_id = $purchaseorder_data->state_id;
                        $model->location_id = $purchaseorder_data->location_id;
                        $model->purchase_orderid = Yii::$app->request->post()['PostingHistory']['purchase_orderid'];
                        $model->section_id = Yii::$app->request->post()['PostingHistory']['section_id'];
                        $model->plant_id = $plant_id;
                        $model->papl_id = $url_papl_id;
                        
                        $model->save();


                    }else{
                        // PostingHistory::deleteAll(['papl_id'=>$url_papl_id,'end_date'=> null]);
                        $end_date = date_create($start_date)->modify('-1 days')->format('d-m-Y');
                        // PostingHistory::updateAll(['end_date' => $end_date], [['like', 'papl_id', $url_papl_id] ]);
                        Yii::$app->db->createCommand()
                            ->update('posting_history', ['end_date' => $end_date], ['papl_id' => $url_papl_id])
                            ->execute();
                       // $posting_data->end_date = date_create($start_date)->modify('-1 days')->format('d-m-Y'); 
                       // $model = new PostingHistory();
                       //  $model->start_date = $start_date;
                       //  $model->state_id = $purchaseorder_data->state_id;
                       //  $model->location_id = $purchaseorder_data->location_id;
                       //  $model->purchase_orderid = Yii::$app->request->post()['PostingHistory']['purchase_orderid'];
                       //  $model->section_id = Yii::$app->request->post()['PostingHistory']['section_id'];
                       //  $model->plant_id = $plant_id;
                         
                       //   $model->papl_id = $url_papl_id;
                       //  $model->save();
                       
                    }
                    
                }
                
                // echo '<pre>';print_r($posting_data);exit();
                // $model->status = 1;//In case of transfer
                
            }

Have you looked into yii\db\Command - upsert()?

yes it is done