/** * Creates a new COMPANIES model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { // $model = new COMPANIES(); // // if ($model->load(Yii::$app->request->post()) && $model->save()) { // return $this->redirect(['view', 'id' => $model->COMPANY_ID]); // } else { // return $this->render('create', [ // 'model' => $model, // ]); // } $model = new COMPANIES(); // Pull in second model $officeLocation = new OFFICELOCATIONS(); // This is how to handle multiple models in one Controller action if ($model->load(Yii::$app->request->post()) && $officeLocation->load(Yii::$app->request->post()) && $model->save()) { $model->save(false); // skip validation as model is already validated // Set the PK of the dependent table (officeLocation) with the PK of the record you just inserted ($model->COMPANY_ID) $officeLocation->COMPANY_ID = $model->COMPANY_ID; // no need for validation rule on Primary Key as you set it yourself $officeLocation->save(false); return $this->redirect(['view', 'id' => $model->COMPANY_ID]); } else { return $this->render('create', ['model' => $model, 'officeLocation' => $officeLocation]); } }